@@ -30,7 +30,7 @@ import (
30
30
func TestMetrics (t * testing.T ) {
31
31
t .Parallel ()
32
32
33
- // setup
33
+ // SETUP
34
34
if ! dbtestutil .WillUsePostgres () {
35
35
t .Skip ("This test requires postgres; it relies on business-logic only implemented in the database" )
36
36
}
@@ -205,7 +205,7 @@ func TestMetrics(t *testing.T) {
205
205
func TestPendingUpdatesMetric (t * testing.T ) {
206
206
t .Parallel ()
207
207
208
- // setup
208
+ // SETUP
209
209
ctx := context .Background ()
210
210
store := dbmem .New ()
211
211
logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true }).Leveled (slog .LevelDebug )
@@ -216,7 +216,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
216
216
217
217
const method = database .NotificationMethodSmtp
218
218
219
- // given
219
+ // GIVEN: a notification manager whose store updates are intercepted so we can read the number of pending updates set in the metric
220
220
cfg := defaultNotificationsConfig (method )
221
221
cfg .FetchInterval = serpent .Duration (time .Millisecond * 50 )
222
222
cfg .RetryInterval = serpent .Duration (time .Hour ) // Delay retries so they don't interfere.
@@ -237,15 +237,17 @@ func TestPendingUpdatesMetric(t *testing.T) {
237
237
enq , err := notifications .NewStoreEnqueuer (cfg , store , defaultHelpers (), logger .Named ("enqueuer" ))
238
238
require .NoError (t , err )
239
239
240
- // when
241
240
user := createSampleUser (t , store )
241
+
242
+ // WHEN: 2 notifications are enqueued, one of which will fail and one which will succeed
242
243
_ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test" ) // this will succeed
243
244
require .NoError (t , err )
244
245
_ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "failure" }, "test2" ) // this will fail and retry (maxAttempts - 1) times
245
246
require .NoError (t , err )
246
247
247
248
mgr .Run (ctx )
248
249
250
+ // THEN:
249
251
// Wait until the handler has dispatched the given notifications.
250
252
require .Eventually (t , func () bool {
251
253
handler .mu .RLock ()
@@ -279,7 +281,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
279
281
func TestInflightDispatchesMetric (t * testing.T ) {
280
282
t .Parallel ()
281
283
282
- // setup
284
+ // SETUP
283
285
ctx := context .Background ()
284
286
store := dbmem .New ()
285
287
logger := slogtest .Make (t , & slogtest.Options {IgnoreErrors : true }).Leveled (slog .LevelDebug )
@@ -290,7 +292,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
290
292
291
293
const method = database .NotificationMethodSmtp
292
294
293
- // given
295
+ // GIVEN: a notification manager whose dispatches are intercepted and delayed to measure the number of inflight requests
294
296
cfg := defaultNotificationsConfig (method )
295
297
cfg .LeaseCount = 10
296
298
cfg .FetchInterval = serpent .Duration (time .Millisecond * 50 )
@@ -313,15 +315,17 @@ func TestInflightDispatchesMetric(t *testing.T) {
313
315
enq , err := notifications .NewStoreEnqueuer (cfg , store , defaultHelpers (), logger .Named ("enqueuer" ))
314
316
require .NoError (t , err )
315
317
316
- // when
317
318
user := createSampleUser (t , store )
319
+
320
+ // WHEN: 2 notifications are enqueued which will both succeed
318
321
_ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test" )
319
322
require .NoError (t , err )
320
323
_ , err = enq .Enqueue (ctx , user .ID , template , map [string ]string {"type" : "success" }, "test2" )
321
324
require .NoError (t , err )
322
325
323
326
mgr .Run (ctx )
324
327
328
+ // THEN:
325
329
// Ensure we see the dispatches of the two messages inflight.
326
330
require .Eventually (t , func () bool {
327
331
return promtest .ToFloat64 (metrics .InflightDispatches .WithLabelValues (string (method ), template .String ())) == 2
0 commit comments