File tree Expand file tree Collapse file tree 2 files changed +21
-9
lines changed Expand file tree Collapse file tree 2 files changed +21
-9
lines changed Original file line number Diff line number Diff line change 5
5
_ "embed"
6
6
"io"
7
7
"net"
8
+ "slices"
8
9
"sync"
9
10
"time"
10
11
@@ -53,8 +54,14 @@ func (b *Backend) NewSession(c *smtp.Conn) (smtp.Session, error) {
53
54
return & Session {conn : c , backend : b }, nil
54
55
}
55
56
57
+ // LastMessage returns a copy of the last message received by the
58
+ // backend.
56
59
func (b * Backend ) LastMessage () * Message {
57
- return b .lastMsg
60
+ b .mu .Lock ()
61
+ defer b .mu .Unlock ()
62
+ clone := * b .lastMsg
63
+ clone .To = slices .Clone (b .lastMsg .To )
64
+ return & clone
58
65
}
59
66
60
67
func (b * Backend ) Reset () {
@@ -84,6 +91,9 @@ func (s *Session) Auth(mech string) (sasl.Server, error) {
84
91
switch mech {
85
92
case sasl .Plain :
86
93
return sasl .NewPlainServer (func (identity , username , password string ) error {
94
+ s .backend .mu .Lock ()
95
+ defer s .backend .mu .Unlock ()
96
+
87
97
s .backend .lastMsg .Identity = identity
88
98
s .backend .lastMsg .Username = username
89
99
s .backend .lastMsg .Password = password
@@ -102,6 +112,9 @@ func (s *Session) Auth(mech string) (sasl.Server, error) {
102
112
}), nil
103
113
case sasl .Login :
104
114
return sasl .NewLoginServer (func (username , password string ) error {
115
+ s .backend .mu .Lock ()
116
+ defer s .backend .mu .Unlock ()
117
+
105
118
s .backend .lastMsg .Username = username
106
119
s .backend .lastMsg .Password = password
107
120
Original file line number Diff line number Diff line change @@ -1253,12 +1253,12 @@ func TestNotificationTemplates_Golden(t *testing.T) {
1253
1253
// Spin up the mock webhook server
1254
1254
var body []byte
1255
1255
var readErr error
1256
- var webhookReceived bool
1256
+ webhookReceived := make ( chan struct {})
1257
1257
server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1258
1258
w .WriteHeader (http .StatusOK )
1259
1259
1260
1260
body , readErr = io .ReadAll (r .Body )
1261
- webhookReceived = true
1261
+ close ( webhookReceived )
1262
1262
}))
1263
1263
t .Cleanup (server .Close )
1264
1264
@@ -1302,12 +1302,11 @@ func TestNotificationTemplates_Golden(t *testing.T) {
1302
1302
)
1303
1303
require .NoError (t , err )
1304
1304
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
+ }
1311
1310
// Handle the body that was read in the http server here.
1312
1311
// We need to do it here because we can't call require.* in a separate goroutine, such as the http server handler
1313
1312
require .NoError (t , readErr )
You can’t perform that action at this time.
0 commit comments