MCPcopy Index your code
hub / github.com/coder/coder / TestCustomMethodMetricCollection

Function TestCustomMethodMetricCollection

coderd/notifications/metrics_test.go:386–457  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

384}
385
386func TestCustomMethodMetricCollection(t *testing.T) {
387 t.Parallel()
388 store, pubsub := dbtestutil.NewDB(t)
389 logger := testutil.Logger(t)
390
391 var (
392 reg = prometheus.NewRegistry()
393 metrics = notifications.NewMetrics(reg)
394 tmpl = notifications.TemplateWorkspaceDeleted
395 anotherTemplate = notifications.TemplateWorkspaceDormant
396 )
397
398 const (
399 customMethod = database.NotificationMethodWebhook
400 defaultMethod = database.NotificationMethodSmtp
401 )
402
403 ctx := testutil.Context(t, testutil.WaitSuperLong)
404
405 // GIVEN: a template whose notification method differs from the default.
406 out, err := store.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
407 ID: tmpl,
408 Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
409 })
410 require.NoError(t, err)
411 require.Equal(t, customMethod, out.Method.NotificationMethod)
412
413 // WHEN: two notifications (each with different templates) are enqueued.
414 cfg := defaultNotificationsConfig(defaultMethod)
415 mgr, err := notifications.NewManager(cfg, store, pubsub, defaultHelpers(), metrics, logger.Named("manager"))
416 require.NoError(t, err)
417 t.Cleanup(func() {
418 assert.NoError(t, mgr.Stop(ctx))
419 })
420
421 smtpHandler := &fakeHandler{}
422 webhookHandler := &fakeHandler{}
423 mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
424 defaultMethod: smtpHandler,
425 customMethod: webhookHandler,
426 database.NotificationMethodInbox: &fakeHandler{},
427 })
428
429 enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"), quartz.NewReal())
430 require.NoError(t, err)
431
432 user := createSampleUser(t, store)
433
434 _, err = enq.Enqueue(ctx, user.ID, tmpl, map[string]string{"type": "success"}, "test")
435 require.NoError(t, err)
436 _, err = enq.Enqueue(ctx, user.ID, anotherTemplate, map[string]string{"type": "success"}, "test")
437 require.NoError(t, err)
438
439 mgr.Run(ctx)
440
441 // THEN: the fake handlers to "dispatch" the notifications.
442 require.Eventually(t, func() bool {
443 smtpHandler.mu.RLock()

Callers

nothing calls this directly

Calls 15

StopMethod · 0.95
WithHandlersMethod · 0.95
EnqueueMethod · 0.95
RunMethod · 0.95
NewDBFunction · 0.92
LoggerFunction · 0.92
NewMetricsFunction · 0.92
ContextFunction · 0.92
NewManagerFunction · 0.92
NewStoreEnqueuerFunction · 0.92
defaultHelpersFunction · 0.85

Tested by

no test coverage detected