(t *testing.T)
| 28 | ) |
| 29 | |
| 30 | func TestMetrics(t *testing.T) { |
| 31 | t.Parallel() |
| 32 | |
| 33 | // SETUP |
| 34 | |
| 35 | store, pubsub := dbtestutil.NewDB(t) |
| 36 | logger := testutil.Logger(t) |
| 37 | |
| 38 | reg := prometheus.NewRegistry() |
| 39 | metrics := notifications.NewMetrics(reg) |
| 40 | tmpl := notifications.TemplateWorkspaceDeleted |
| 41 | |
| 42 | const ( |
| 43 | method = database.NotificationMethodSmtp |
| 44 | maxAttempts = 3 |
| 45 | debug = false |
| 46 | ) |
| 47 | |
| 48 | // GIVEN: a notification manager whose intervals are tuned low (for test speed) and whose dispatches are intercepted |
| 49 | cfg := defaultNotificationsConfig(method) |
| 50 | cfg.MaxSendAttempts = maxAttempts |
| 51 | // Tune the intervals low to increase test speed. |
| 52 | cfg.FetchInterval = serpent.Duration(time.Millisecond * 50) |
| 53 | cfg.RetryInterval = serpent.Duration(time.Millisecond * 50) |
| 54 | cfg.StoreSyncInterval = serpent.Duration(time.Millisecond * 100) // Twice as long as fetch interval to ensure we catch pending updates. |
| 55 | |
| 56 | mgr, err := notifications.NewManager(cfg, store, pubsub, defaultHelpers(), metrics, logger.Named("manager")) |
| 57 | require.NoError(t, err) |
| 58 | ctx := testutil.Context(t, testutil.WaitSuperLong) |
| 59 | t.Cleanup(func() { |
| 60 | assert.NoError(t, mgr.Stop(ctx)) |
| 61 | }) |
| 62 | handler := &fakeHandler{} |
| 63 | mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{ |
| 64 | method: handler, |
| 65 | database.NotificationMethodInbox: &fakeHandler{}, |
| 66 | }) |
| 67 | |
| 68 | enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal()) |
| 69 | require.NoError(t, err) |
| 70 | |
| 71 | user := createSampleUser(t, store) |
| 72 | |
| 73 | // Build fingerprints for the two different series we expect. |
| 74 | methodTemplateFP := fingerprintLabels(notifications.LabelMethod, string(method), notifications.LabelTemplateID, tmpl.String()) |
| 75 | methodTemplateFPWithInbox := fingerprintLabels(notifications.LabelMethod, string(database.NotificationMethodInbox), notifications.LabelTemplateID, tmpl.String()) |
| 76 | |
| 77 | methodFP := fingerprintLabels(notifications.LabelMethod, string(method)) |
| 78 | methodFPWithInbox := fingerprintLabels(notifications.LabelMethod, string(database.NotificationMethodInbox)) |
| 79 | |
| 80 | expected := map[string]func(metric *dto.Metric, series string) bool{ |
| 81 | "coderd_notifications_dispatch_attempts_total": func(metric *dto.Metric, series string) bool { |
| 82 | // This metric has 3 possible dispositions; find if any of them match first before we check the metric's value. |
| 83 | results := map[string]float64{ |
| 84 | notifications.ResultSuccess: 1, // Only 1 successful delivery. |
| 85 | notifications.ResultTempFail: maxAttempts - 1, // 2 temp failures, on the 3rd it'll be marked permanent failure. |
| 86 | notifications.ResultPermFail: 1, // 1 permanent failure after retries exhausted. |
| 87 | } |
nothing calls this directly
no test coverage detected