all repos

onasty @ a4b3d8e

a one-time notes service
1 files changed, 17 insertions(+), 3 deletions(-)
refactor(mailer): add mutex to testing mailer, to avoid race-conditions (#27)

Author: Smirnov Oleksandr ss2316544@gmail.com
Committed by: GitHub noreply@github.com
Committed at: 2024-10-03 22:31:39 +0300
Parent: 304505a
M internal/mailer/testing_mailer.go

@@ -1,10 +1,15 @@

package mailer -import "context" +import ( + "context" + "sync" +) var _ Mailer = (*TestMailer)(nil) type TestMailer struct { + mu sync.Mutex + emails map[string]string }

@@ -12,17 +17,26 @@ // NewTestMailer create a mailer for tests

// that implementation of Mailer stores all sent email in memory // to get the last email sent to a specific email use GetLastSentEmailToEmail func NewTestMailer() *TestMailer { - return &TestMailer{ + return &TestMailer{ //nolint:exhaustruct emails: make(map[string]string), } } func (t *TestMailer) Send(_ context.Context, to, _, content string) error { + t.mu.Lock() + defer t.mu.Unlock() + t.emails[to] = content + return nil } // GetLastSentEmailToEmail returns the last email sent to a specific email func (t *TestMailer) GetLastSentEmailToEmail(email string) string { - return t.emails[email] + t.mu.Lock() + defer t.mu.Unlock() + + e := t.emails[email] + + return e }