Make PostOffice more idiomatic to use as a library #8
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
My use case is that I've got a Ruby program that does end-to-end testing on a system that (amongst many other things) sends email. So for each test, I want to spin up an SMTP server (on a random high port), configure the system under test to use said SMTP server, and then validate the emails sent.
PostOffice is basically perfect for this -- it's small, easy-to-understand code, no massive external dependencies, quick and easy to spin up a SMTP server in-process, receive mail, grab the message out of the server, and assert the needful. I only needed to make a few tiny tweaks to be perfect for my use-case. I can't easily verify that I haven't broken anything (other than the bits I use), but I did try to keep the changes small and self-contained.
In detail, the changes I made are:
PostOffice
module, to give a common namespace and avoid collisions (which includes moving the files, for include purposes);$log
variable, and instead pass a logger where its needed;SMTPServer
class, because -- since I'm using the SMTP server in tests -- I need random-free-port allocation, and I need to be able to find out what port was assigned (so I can configure the system under test to use that port). So as its done now, I can setup the server on the main thread, get the port number, and then set the server running in a separate thread.Store
. This is needed because I'm running multiple tests in parallel, and so having a single place where mails are received into would end badly.RSET
command handler to send a response, because it turns out the SMTP client library my system is using actually uses that command, and expects a response before completing the mail sending, which took a few minutes to figure out...I think that's about it. Hopefully the changes are appropriate for inclusion in the official gem.