mocktools / go-smtp-mock

SMTP mock server written on Golang. Mimic any 📤 SMTP server behavior for your test environment with fake SMTP server.
MIT License
123 stars 17 forks source link

[BUG] 2.3.0 broke message reading... #181

Open nicolasparada opened 5 months ago

nicolasparada commented 5 months ago

New bug checklist

Bug description

Upgraded from v2.2.1 to v2.3.0 and our tests started failing. Our setup is quite simple. We use it as a mock during testing. We start the mock server on TestMain() and use it on the tests. The test consist on sending a mail using the standard net/smtp using the mock server address. Then reading the messages, finding the message we want and doing some assertions.

We couldn't find the expected messages using .Messages().

I suspects there is a race condition there. Since the tests do pass locally, but they fail during CI (probably due to a slower machine on CI). I added a quick time.Sleep(time.Millisecond) before accessing .Messages() and that caused the tests to pass again.

bestwebua commented 5 months ago

Hi, @nicolasparada! Please add usage examples of using smtpmock here. @mitar Do you have ideas how we can assist with it?

mitar commented 5 months ago

I think this is expected. This is another (also present before) race condition between sending and reading. Currently we do not have memory issues, but logical race condition is still present. We are just clearer about it because we do not expose any data until sending is finished. In my own tests, I also have sleep before reading .Messages.

I think we should add an assertion to this package itself, something similar to what #154, which blocks until expected number of messages arrive, or timeout.

So something like:

.ExpectMessages(number, timeout)

which return message and error.