Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6e54bd9

Browse files
authored
test(coderd/notifications): fix data race in tests and smpttest (#15304)
1 parent 591cefa commit 6e54bd9

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

coderd/notifications/dispatch/smtptest/server.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
_ "embed"
66
"io"
77
"net"
8+
"slices"
89
"sync"
910
"time"
1011

@@ -53,11 +54,22 @@ func (b *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
5354
return &Session{conn: c, backend: b}, nil
5455
}
5556

57+
// LastMessage returns a copy of the last message received by the
58+
// backend.
5659
func (b *Backend) LastMessage() *Message {
57-
return b.lastMsg
60+
b.mu.Lock()
61+
defer b.mu.Unlock()
62+
if b.lastMsg == nil {
63+
return nil
64+
}
65+
clone := *b.lastMsg
66+
clone.To = slices.Clone(b.lastMsg.To)
67+
return &clone
5868
}
5969

6070
func (b *Backend) Reset() {
71+
b.mu.Lock()
72+
defer b.mu.Unlock()
6173
b.lastMsg = nil
6274
}
6375

@@ -84,6 +96,9 @@ func (s *Session) Auth(mech string) (sasl.Server, error) {
8496
switch mech {
8597
case sasl.Plain:
8698
return sasl.NewPlainServer(func(identity, username, password string) error {
99+
s.backend.mu.Lock()
100+
defer s.backend.mu.Unlock()
101+
87102
s.backend.lastMsg.Identity = identity
88103
s.backend.lastMsg.Username = username
89104
s.backend.lastMsg.Password = password
@@ -102,6 +117,9 @@ func (s *Session) Auth(mech string) (sasl.Server, error) {
102117
}), nil
103118
case sasl.Login:
104119
return sasl.NewLoginServer(func(username, password string) error {
120+
s.backend.mu.Lock()
121+
defer s.backend.mu.Unlock()
122+
105123
s.backend.lastMsg.Username = username
106124
s.backend.lastMsg.Password = password
107125

coderd/notifications/notifications_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -1253,12 +1253,12 @@ func TestNotificationTemplates_Golden(t *testing.T) {
12531253
// Spin up the mock webhook server
12541254
var body []byte
12551255
var readErr error
1256-
var webhookReceived bool
1256+
webhookReceived := make(chan struct{})
12571257
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
12581258
w.WriteHeader(http.StatusOK)
12591259

12601260
body, readErr = io.ReadAll(r.Body)
1261-
webhookReceived = true
1261+
close(webhookReceived)
12621262
}))
12631263
t.Cleanup(server.Close)
12641264

@@ -1302,12 +1302,11 @@ func TestNotificationTemplates_Golden(t *testing.T) {
13021302
)
13031303
require.NoError(t, err)
13041304

1305-
require.Eventually(t, func() bool {
1306-
return webhookReceived
1307-
}, testutil.WaitShort, testutil.IntervalFast)
1308-
1309-
require.NoError(t, err)
1310-
1305+
select {
1306+
case <-time.After(testutil.WaitShort):
1307+
require.Fail(t, "timed out waiting for webhook to be received")
1308+
case <-webhookReceived:
1309+
}
13111310
// Handle the body that was read in the http server here.
13121311
// We need to do it here because we can't call require.* in a separate goroutine, such as the http server handler
13131312
require.NoError(t, readErr)

0 commit comments

Comments
 (0)