Skip to content

Commit a4b3d8e

Browse files
authored
refactor(mailer): add mutex to testing mailer, to avoid race-conditions (#27)
1 parent 304505a commit a4b3d8e

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

internal/mailer/testing_mailer.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
package mailer
22

3-
import "context"
3+
import (
4+
"context"
5+
"sync"
6+
)
47

58
var _ Mailer = (*TestMailer)(nil)
69

710
type TestMailer struct {
11+
mu sync.Mutex
12+
813
emails map[string]string
914
}
1015

1116
// NewTestMailer create a mailer for tests
1217
// that implementation of Mailer stores all sent email in memory
1318
// to get the last email sent to a specific email use GetLastSentEmailToEmail
1419
func NewTestMailer() *TestMailer {
15-
return &TestMailer{
20+
return &TestMailer{ //nolint:exhaustruct
1621
emails: make(map[string]string),
1722
}
1823
}
1924

2025
func (t *TestMailer) Send(_ context.Context, to, _, content string) error {
26+
t.mu.Lock()
27+
defer t.mu.Unlock()
28+
2129
t.emails[to] = content
30+
2231
return nil
2332
}
2433

2534
// GetLastSentEmailToEmail returns the last email sent to a specific email
2635
func (t *TestMailer) GetLastSentEmailToEmail(email string) string {
27-
return t.emails[email]
36+
t.mu.Lock()
37+
defer t.mu.Unlock()
38+
39+
e := t.emails[email]
40+
41+
return e
2842
}

0 commit comments

Comments
 (0)