@@ -12,12 +12,8 @@ import (
12
12
"github.com/stretchr/testify/require"
13
13
"golang.org/x/xerrors"
14
14
15
- "cdr.dev/slog"
16
- "cdr.dev/slog/sloggers/slogtest"
17
15
"github.com/coder/coder/v2/coderd/database"
18
16
"github.com/coder/coder/v2/coderd/database/dbgen"
19
- "github.com/coder/coder/v2/coderd/database/dbmem"
20
- "github.com/coder/coder/v2/coderd/database/dbtestutil"
21
17
"github.com/coder/coder/v2/coderd/notifications"
22
18
"github.com/coder/coder/v2/coderd/notifications/dispatch"
23
19
"github.com/coder/coder/v2/coderd/notifications/types"
@@ -29,17 +25,15 @@ func TestBufferedUpdates(t *testing.T) {
29
25
t .Parallel ()
30
26
31
27
// setup
32
- if ! dbtestutil .WillUsePostgres () {
33
- t .Skip ("This test requires postgres" )
34
- }
28
+ ctx , logger , db := setupInMemory (t )
35
29
36
- ctx , logger , db := setup (t )
37
30
interceptor := & bulkUpdateInterceptor {Store : db }
38
31
santa := & santaHandler {}
39
32
40
33
cfg := defaultNotificationsConfig (database .NotificationMethodSmtp )
41
34
cfg .StoreSyncInterval = serpent .Duration (time .Hour ) // Ensure we don't sync the store automatically.
42
35
36
+ // GIVEN: a manager which will pass or fail notifications based on their "nice" labels
43
37
mgr , err := notifications .NewManager (cfg , interceptor , logger .Named ("notifications-manager" ))
44
38
require .NoError (t , err )
45
39
mgr .WithHandlers (map [database.NotificationMethod ]notifications.Handler {
@@ -50,18 +44,17 @@ func TestBufferedUpdates(t *testing.T) {
50
44
51
45
user := dbgen .User (t , db , database.User {})
52
46
53
- // given
47
+ // WHEN: notifications are enqueued which should succeed and fail
54
48
_ , err = enq .Enqueue (ctx , user .ID , notifications .TemplateWorkspaceDeleted , map [string ]string {"nice" : "true" }, "" ) // Will succeed.
55
49
require .NoError (t , err )
56
50
_ , err = enq .Enqueue (ctx , user .ID , notifications .TemplateWorkspaceDeleted , map [string ]string {"nice" : "true" }, "" ) // Will succeed.
57
51
require .NoError (t , err )
58
52
_ , err = enq .Enqueue (ctx , user .ID , notifications .TemplateWorkspaceDeleted , map [string ]string {"nice" : "false" }, "" ) // Will fail.
59
53
require .NoError (t , err )
60
54
61
- // when
62
55
mgr .Run (ctx )
63
56
64
- // then
57
+ // THEN:
65
58
66
59
const (
67
60
expectedSuccess = 2
@@ -99,15 +92,18 @@ func TestBufferedUpdates(t *testing.T) {
99
92
func TestBuildPayload (t * testing.T ) {
100
93
t .Parallel ()
101
94
102
- // given
95
+ // SETUP
96
+ ctx , logger , db := setupInMemory (t )
97
+
98
+ // GIVEN: a set of helpers to be injected into the templates
103
99
const label = "Click here!"
104
100
const url = "http://xyz.com/"
105
101
helpers := map [string ]any {
106
102
"my_label" : func () string { return label },
107
103
"my_url" : func () string { return url },
108
104
}
109
105
110
- db := dbmem . New ()
106
+ // GIVEN: an enqueue interceptor which returns mock metadata
111
107
interceptor := newEnqueueInterceptor (db ,
112
108
// Inject custom message metadata to influence the payload construction.
113
109
func () database.FetchNewMessageMetadataRow {
@@ -130,17 +126,14 @@ func TestBuildPayload(t *testing.T) {
130
126
}
131
127
})
132
128
133
- logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true , IgnoredErrorIs : []error {}}).Leveled (slog .LevelDebug )
134
129
enq , err := notifications .NewStoreEnqueuer (defaultNotificationsConfig (database .NotificationMethodSmtp ), interceptor , helpers , logger .Named ("notifications-enqueuer" ))
135
130
require .NoError (t , err )
136
131
137
- ctx := testutil .Context (t , testutil .WaitShort )
138
-
139
- // when
132
+ // WHEN: a notification is enqueued
140
133
_ , err = enq .Enqueue (ctx , uuid .New (), notifications .TemplateWorkspaceDeleted , nil , "test" )
141
134
require .NoError (t , err )
142
135
143
- // then
136
+ // THEN: expect that a payload will be constructed and have the expected values
144
137
payload := testutil .RequireRecvCtx (ctx , t , interceptor .payload )
145
138
require .Len (t , payload .Actions , 1 )
146
139
require .Equal (t , label , payload .Actions [0 ].Label )
@@ -150,12 +143,14 @@ func TestBuildPayload(t *testing.T) {
150
143
func TestStopBeforeRun (t * testing.T ) {
151
144
t .Parallel ()
152
145
153
- ctx := context .Background ()
154
- logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true , IgnoredErrorIs : []error {}}).Leveled (slog .LevelDebug )
155
- mgr , err := notifications .NewManager (defaultNotificationsConfig (database .NotificationMethodSmtp ), dbmem .New (), logger .Named ("notifications-manager" ))
146
+ // SETUP
147
+ ctx , logger , db := setupInMemory (t )
148
+
149
+ // GIVEN: a standard manager
150
+ mgr , err := notifications .NewManager (defaultNotificationsConfig (database .NotificationMethodSmtp ), db , logger .Named ("notifications-manager" ))
156
151
require .NoError (t , err )
157
152
158
- // Call stop before notifier is started with Run().
153
+ // THEN: validate that the manager can be stopped safely without Run() having been called yet
159
154
require .Eventually (t , func () bool {
160
155
assert .NoError (t , mgr .Stop (ctx ))
161
156
return true
0 commit comments