@@ -14,12 +14,9 @@ import (
14
14
"github.com/stretchr/testify/assert"
15
15
"github.com/stretchr/testify/require"
16
16
17
- "cdr.dev/slog"
18
- "cdr.dev/slog/sloggers/slogtest"
19
17
"github.com/coder/serpent"
20
18
21
19
"github.com/coder/coder/v2/coderd/database"
22
- "github.com/coder/coder/v2/coderd/database/dbmem"
23
20
"github.com/coder/coder/v2/coderd/database/dbtestutil"
24
21
"github.com/coder/coder/v2/coderd/notifications"
25
22
"github.com/coder/coder/v2/coderd/notifications/dispatch"
@@ -206,9 +203,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
206
203
t .Parallel ()
207
204
208
205
// SETUP
209
- ctx := context .Background ()
210
- store := dbmem .New ()
211
- logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true }).Leveled (slog .LevelDebug )
206
+ ctx , logger , store := setupInMemory (t )
212
207
213
208
reg := prometheus .NewRegistry ()
214
209
metrics := notifications .NewMetrics (reg )
@@ -282,9 +277,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
282
277
t .Parallel ()
283
278
284
279
// SETUP
285
- ctx := context .Background ()
286
- store := dbmem .New ()
287
- logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true }).Leveled (slog .LevelDebug )
280
+ ctx , logger , store := setupInMemory (t )
288
281
289
282
reg := prometheus .NewRegistry ()
290
283
metrics := notifications .NewMetrics (reg )
@@ -304,8 +297,8 @@ func TestInflightDispatchesMetric(t *testing.T) {
304
297
t .Cleanup (func () {
305
298
assert .NoError (t , mgr .Stop (ctx ))
306
299
})
307
- handler := & fakeHandler {}
308
300
301
+ handler := & fakeHandler {}
309
302
// Delayer will delay all dispatches by 2x fetch intervals to ensure we catch the requests inflight.
310
303
delayer := newDelayingHandler (cfg .FetchInterval .Value ()* 2 , handler )
311
304
mgr .WithHandlers (map [database.NotificationMethod ]notifications.Handler {
@@ -317,26 +310,27 @@ func TestInflightDispatchesMetric(t *testing.T) {
317
310
318
311
user := createSampleUser (t , store )
319
312
320
- // WHEN: 2 notifications are enqueued which will both succeed
321
- _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test" )
322
- require .NoError (t , err )
323
- _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test2" )
324
- require .NoError (t , err )
313
+ // WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
314
+ const msgCount = 2
315
+ for i := 0 ; i < msgCount ; i ++ {
316
+ _ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test" )
317
+ require .NoError (t , err )
318
+ }
325
319
326
320
mgr .Run (ctx )
327
321
328
322
// THEN:
329
- // Ensure we see the dispatches of the two messages inflight.
323
+ // Ensure we see the dispatches of the messages inflight.
330
324
require .Eventually (t , func () bool {
331
- return promtest .ToFloat64 (metrics .InflightDispatches .WithLabelValues (string (method ), template .String ())) == 2
325
+ return promtest .ToFloat64 (metrics .InflightDispatches .WithLabelValues (string (method ), template .String ())) == msgCount
332
326
}, testutil .WaitShort , testutil .IntervalFast )
333
327
334
328
// Wait until the handler has dispatched the given notifications.
335
329
require .Eventually (t , func () bool {
336
330
handler .mu .RLock ()
337
331
defer handler .mu .RUnlock ()
338
332
339
- return len (handler .succeeded ) == 2
333
+ return len (handler .succeeded ) == msgCount
340
334
}, testutil .WaitShort , testutil .IntervalFast )
341
335
342
336
// Wait for the updates to be synced and the metric to reflect that.
0 commit comments