go-smtp-mock icon indicating copy to clipboard operation
go-smtp-mock copied to clipboard

[FEATURE] Optional message purge when retrieving messages

Open dandare100 opened this issue 2 years ago • 1 comments

I understand that this is not a fully fledged mail server and it is meant and optimized for mocking.

From my side it would be cool to have a method func (server *Server) MessagesAndPurge() []*Message

Would you entertain such a request ?

Describe the solution you'd like. A clear and concise description of what you want to happen.

Add method func (server *Server) MessagesAndPurge() []*Message which returns all the messages that are no longer being handled in a session, and removes them from the slice.

We need to consider messages that are currently being modified in active sessions. We would need a way of telling if a message is busy being handled in a session because it should not be removed. Perhaps a marker variable that gets updated when the the session ends ?

Even "inconsistent" messages should be returned and purged.

Just an idea, please forgive brevity

dandare100 avatar Sep 09 '22 17:09 dandare100

@dandare100 Hi, Mark! Sorry for delay. I agree, it's good point to have an interface that can purge all messages except active sessions. But client can terminate session not following using RFC flow. And as a consequence we need handle list of active sessions and bind current session with current message. Also I have an idea to assign for each session an id or uuid. I'll decompose it as independent tasks.

bestwebua avatar Sep 17 '22 18:09 bestwebua

This is effectively the same as doing a "pop" on the stack of messages, wouldn't you say?

@bestwebua

Also I have an idea to assign for each session an id or uuid.

This is a very sensible place to start, are you happy for me to go ahead and do so?

benjamin-rood avatar Mar 03 '23 08:03 benjamin-rood

@benjamin-rood Yep, thank you Benjamin! It would be great, but after https://github.com/mocktools/go-smtp-mock/issues/150.

bestwebua avatar Mar 03 '23 08:03 bestwebua

I came across this issue while considering a feature request for adding a reset capability to the server between individual tests to enhance the testing process by allowing a clear state without a full restart. My idea is inspired by the functionality in the httpmock package, which allows for resetting the mock transport, effectively clearing the setup for a new test scenario.

While my initial thought was to open a new issue, I see potential synergy with the MessagesAndPurge functionality discussed here. Implementing a reset feature in go-smtp-mock could complement the message purging by not only removing messages but also resetting the server's internal state for a fresh test environment, similar to how httpmock operates.

For instance, consider a scenario where multiple tests send emails with varying content and recipients. Currently, each test might have to set up and tear down the server, which is inefficient. With a reset method, after each test, we could simply reset the server, ensuring that the next test starts with a clean slate without the overhead of a full restart. Here's a simplistic example of how this could look:

func TestEmailSending(t *testing.T) {
    server := smtpmock.New(smtpmock.ConfigurationAttr{...})
    if err := server.Start(); err != nil {
	t.Fatalf("smtpmock.Start() error = %v", err)
    }
    defer server.Stop()

    t.Run("Test1", func(t *testing.T) {
        // ... send email and perform assertions ...
    })

    server.Reset() // Reset server state here

    t.Run("Test2", func(t *testing.T) {
        // ... send a different email and perform other assertions ...
    })

    // The server is reset between tests, ensuring no residual data affects the next test.
}

I believe integrating such a reset capability could significantly streamline and improve the efficiency of using smtpmock in tests, especially in complex test suites where multiple aspects of email sending and receiving are being verified.

lvlcn-t avatar Dec 28 '23 13:12 lvlcn-t

I made https://github.com/mocktools/go-smtp-mock/pull/174 to address this.

mitar avatar Feb 28 '24 10:02 mitar

Thanks @mitar for your involvement! In the latest release already!

bestwebua avatar Mar 03 '24 14:03 bestwebua