Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PostOffice more idiomatic to use as a library #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mpalmer
Copy link

@mpalmer mpalmer commented Dec 31, 2024

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:

  • Move all top-level identifiers into a PostOffice module, to give a common namespace and avoid collisions (which includes moving the files, for include purposes);
  • Remove the global $log variable, and instead pass a logger where its needed;
  • Separate the initialization and operation of the 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.
  • Received messages are now stored in the server object, as well as in the global singleton 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.
  • I taught the 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant