Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4211c84

Browse files
committed
Comments
Signed-off-by: Danny Kopping <[email protected]>
1 parent 2b9eec3 commit 4211c84

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

coderd/notifications/metrics_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030
func TestMetrics(t *testing.T) {
3131
t.Parallel()
3232

33-
// setup
33+
// SETUP
3434
if !dbtestutil.WillUsePostgres() {
3535
t.Skip("This test requires postgres; it relies on business-logic only implemented in the database")
3636
}
@@ -205,7 +205,7 @@ func TestMetrics(t *testing.T) {
205205
func TestPendingUpdatesMetric(t *testing.T) {
206206
t.Parallel()
207207

208-
// setup
208+
// SETUP
209209
ctx := context.Background()
210210
store := dbmem.New()
211211
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
@@ -216,7 +216,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
216216

217217
const method = database.NotificationMethodSmtp
218218

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
220220
cfg := defaultNotificationsConfig(method)
221221
cfg.FetchInterval = serpent.Duration(time.Millisecond * 50)
222222
cfg.RetryInterval = serpent.Duration(time.Hour) // Delay retries so they don't interfere.
@@ -237,15 +237,17 @@ func TestPendingUpdatesMetric(t *testing.T) {
237237
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"))
238238
require.NoError(t, err)
239239

240-
// when
241240
user := createSampleUser(t, store)
241+
242+
// WHEN: 2 notifications are enqueued, one of which will fail and one which will succeed
242243
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test") // this will succeed
243244
require.NoError(t, err)
244245
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "failure"}, "test2") // this will fail and retry (maxAttempts - 1) times
245246
require.NoError(t, err)
246247

247248
mgr.Run(ctx)
248249

250+
// THEN:
249251
// Wait until the handler has dispatched the given notifications.
250252
require.Eventually(t, func() bool {
251253
handler.mu.RLock()
@@ -279,7 +281,7 @@ func TestPendingUpdatesMetric(t *testing.T) {
279281
func TestInflightDispatchesMetric(t *testing.T) {
280282
t.Parallel()
281283

282-
// setup
284+
// SETUP
283285
ctx := context.Background()
284286
store := dbmem.New()
285287
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
@@ -290,7 +292,7 @@ func TestInflightDispatchesMetric(t *testing.T) {
290292

291293
const method = database.NotificationMethodSmtp
292294

293-
// given
295+
// GIVEN: a notification manager whose dispatches are intercepted and delayed to measure the number of inflight requests
294296
cfg := defaultNotificationsConfig(method)
295297
cfg.LeaseCount = 10
296298
cfg.FetchInterval = serpent.Duration(time.Millisecond * 50)
@@ -313,15 +315,17 @@ func TestInflightDispatchesMetric(t *testing.T) {
313315
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"))
314316
require.NoError(t, err)
315317

316-
// when
317318
user := createSampleUser(t, store)
319+
320+
// WHEN: 2 notifications are enqueued which will both succeed
318321
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test")
319322
require.NoError(t, err)
320323
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test2")
321324
require.NoError(t, err)
322325

323326
mgr.Run(ctx)
324327

328+
// THEN:
325329
// Ensure we see the dispatches of the two messages inflight.
326330
require.Eventually(t, func() bool {
327331
return promtest.ToFloat64(metrics.InflightDispatches.WithLabelValues(string(method), template.String())) == 2

0 commit comments

Comments
 (0)