File tree 2 files changed +26
-9
lines changed
2 files changed +26
-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,11 +54,22 @@ 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
+ if b .lastMsg == nil {
63
+ return nil
64
+ }
65
+ clone := * b .lastMsg
66
+ clone .To = slices .Clone (b .lastMsg .To )
67
+ return & clone
58
68
}
59
69
60
70
func (b * Backend ) Reset () {
71
+ b .mu .Lock ()
72
+ defer b .mu .Unlock ()
61
73
b .lastMsg = nil
62
74
}
63
75
@@ -84,6 +96,9 @@ func (s *Session) Auth(mech string) (sasl.Server, error) {
84
96
switch mech {
85
97
case sasl .Plain :
86
98
return sasl .NewPlainServer (func (identity , username , password string ) error {
99
+ s .backend .mu .Lock ()
100
+ defer s .backend .mu .Unlock ()
101
+
87
102
s .backend .lastMsg .Identity = identity
88
103
s .backend .lastMsg .Username = username
89
104
s .backend .lastMsg .Password = password
@@ -102,6 +117,9 @@ func (s *Session) Auth(mech string) (sasl.Server, error) {
102
117
}), nil
103
118
case sasl .Login :
104
119
return sasl .NewLoginServer (func (username , password string ) error {
120
+ s .backend .mu .Lock ()
121
+ defer s .backend .mu .Unlock ()
122
+
105
123
s .backend .lastMsg .Username = username
106
124
s .backend .lastMsg .Password = password
107
125
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