How I reduced the build execution time for email connectors using mock email servers.

2 minute read

I checked in my changes, time to wait for the build to be completed in jenkins. It was time to pick up coffee and play the waiting game, as it would take another hour before I can proceed with deployment of the code. We obviously had to do something !!

Mapping the wastage

This micro service was responsible for sending/receiving mails from different configured mail servers. We were doing unit and integration level testing, where in integration testing we were connecting to a local exchange server with pre-configured test users for sending/receiving mail.

The best way to start optimization - find gaps & wastage. We broke down the total build execution into build, deployment on staging environment & acceptance.

We noticed the build & deployment time was fair, however the acceptance tests were taking around 40-45 min to complete. Now that we have narrowed our focus. It was time to do a breakdown analysis of test.

Challenges

We often have been observing slowness of builds, sometimes intermittent failures as well. The reason were obvious, tests were using same set of users, so during parallel execution data gets corrupted. We needed a clean way test our api’s. Also after regular intervals, we had to clean the mail accounts.

To put simply we needed

  1. Unique set of user for each test.
  2. Local server to reduce network delays.
  3. Ability to automatically flush the existing data.
  4. Behave like a real world mail server
  5. Supports the standards protocols - SMTP, IMAP & POP3

GreenMail to rescue

Time for us to switch to mock email server, there are good number of servers available. Some are free to use and some are paid.

  1. MailTrap
  2. Mailspons
  3. GreenMail

GreenMail is open source while other solutions are paid. As stated on the main page, the benefits include:

  • Supports SMTP, POP3 and IMAP including SSL
  • Prevents accidental email leaking to real mail servers
  • Provides different deployment models, such as a simple standalone JVM process, as a WAR module or as a docker image
  • Easily embeddable in JUnit tests for integration testing
  • Lightweight

This library supports both receiving and retrieving mails from java.

Java Mail Service

We will create a simple method to mimic a real world functionality - to send a mail.

So now we need to test this functionality, as its clear we will need a email server to validate if our code is working fine.

Set up GreenMail

We will embed a mock server in our test, first we will add a test dependency for greenmail

Next, we are going to write a simple test to send message and validate. In this example, we will start our mock server at 9000 port and connect to it via our api, and send the message.

With every test I can create unique set of users, and run my test against it and finally purge the server.

GreenMail also gives utility to access the received messages. You can also use the standard way by connecting via IMAP/POP3 implementations.

Its a great way to avoid message spill over, cleanup managed exchange server and improves reliability.

Complete code

Categories:

Updated:

Kumar Rohit
WRITTEN BY

Kumar Rohit

I like long drives, bike trip & good food. I have passion for coding, especially for Clean-Code.

Leave a comment