Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6e54bd9

Browse files
authored
test(coderd/notifications): fix data race in tests and smpttest (#15304)
1 parent591cefa commit6e54bd9

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

‎coderd/notifications/dispatch/smtptest/server.go

Lines changed: 19 additions & 1 deletion
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-
returnb.lastMsg
60+
b.mu.Lock()
61+
deferb.mu.Unlock()
62+
ifb.lastMsg==nil {
63+
returnnil
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+
deferb.mu.Unlock()
6173
b.lastMsg=nil
6274
}
6375

@@ -84,6 +96,9 @@ func (s *Session) Auth(mech string) (sasl.Server, error) {
8496
switchmech {
8597
casesasl.Plain:
8698
returnsasl.NewPlainServer(func(identity,username,passwordstring)error {
99+
s.backend.mu.Lock()
100+
defers.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
casesasl.Login:
104119
returnsasl.NewLoginServer(func(username,passwordstring)error {
120+
s.backend.mu.Lock()
121+
defers.backend.mu.Unlock()
122+
105123
s.backend.lastMsg.Username=username
106124
s.backend.lastMsg.Password=password
107125

‎coderd/notifications/notifications_test.go

Lines changed: 7 additions & 8 deletions
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
varbody []byte
12551255
varreadErrerror
1256-
varwebhookReceivedbool
1256+
webhookReceived:=make(chanstruct{})
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-
returnwebhookReceived
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)

[8]ページ先頭

©2009-2025 Movatter.jp