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

Skip to content

Commit f2926a3

Browse files
committed
Test metric collection when dispatching with custom methods
Signed-off-by: Danny Kopping <[email protected]>
1 parent 531128b commit f2926a3

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

coderd/notifications/metrics_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,81 @@ func TestInflightDispatchesMetric(t *testing.T) {
339339
}, testutil.WaitShort, testutil.IntervalFast)
340340
}
341341

342+
func TestCustomMethodMetricCollection(t *testing.T) {
343+
t.Parallel()
344+
345+
// SETUP
346+
if !dbtestutil.WillUsePostgres() {
347+
// UpdateNotificationTemplateMethodByID only makes sense with a real database.
348+
t.Skip("This test requires postgres; it relies on business-logic only implemented in the database")
349+
}
350+
ctx, logger, store := setup(t)
351+
352+
var (
353+
reg = prometheus.NewRegistry()
354+
metrics = notifications.NewMetrics(reg)
355+
template = notifications.TemplateWorkspaceDeleted
356+
anotherTemplate = notifications.TemplateWorkspaceDormant
357+
)
358+
359+
const (
360+
customMethod = database.NotificationMethodWebhook
361+
defaultMethod = database.NotificationMethodSmtp
362+
)
363+
364+
// GIVEN: a template whose notification method differs from the default.
365+
out, err := store.UpdateNotificationTemplateMethodByID(ctx, database.UpdateNotificationTemplateMethodByIDParams{
366+
ID: template,
367+
Method: database.NullNotificationMethod{NotificationMethod: customMethod, Valid: true},
368+
})
369+
require.NoError(t, err)
370+
require.Equal(t, customMethod, out.Method.NotificationMethod)
371+
372+
// WHEN: two notifications (each with different templates) are enqueued.
373+
cfg := defaultNotificationsConfig(defaultMethod)
374+
mgr, err := notifications.NewManager(cfg, store, metrics, logger.Named("manager"))
375+
require.NoError(t, err)
376+
t.Cleanup(func() {
377+
assert.NoError(t, mgr.Stop(ctx))
378+
})
379+
380+
smtpHandler := &fakeHandler{}
381+
webhookHandler := &fakeHandler{}
382+
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
383+
defaultMethod: smtpHandler,
384+
customMethod: webhookHandler,
385+
})
386+
387+
enq, err := notifications.NewStoreEnqueuer(cfg, store, defaultHelpers(), logger.Named("enqueuer"))
388+
require.NoError(t, err)
389+
390+
user := createSampleUser(t, store)
391+
392+
_, err = enq.Enqueue(ctx, user.ID, template, map[string]string{"type": "success"}, "test")
393+
require.NoError(t, err)
394+
_, err = enq.Enqueue(ctx, user.ID, anotherTemplate, map[string]string{"type": "success"}, "test")
395+
require.NoError(t, err)
396+
397+
mgr.Run(ctx)
398+
399+
// THEN: the fake handlers to "dispatch" the notifications.
400+
require.Eventually(t, func() bool {
401+
smtpHandler.mu.RLock()
402+
webhookHandler.mu.RLock()
403+
defer smtpHandler.mu.RUnlock()
404+
defer webhookHandler.mu.RUnlock()
405+
406+
return len(smtpHandler.succeeded) == 1 && len(smtpHandler.failed) == 0 &&
407+
len(webhookHandler.succeeded) == 1 && len(webhookHandler.failed) == 0
408+
}, testutil.WaitShort, testutil.IntervalFast)
409+
410+
// THEN: we should have metric series for both the default and custom notification methods.
411+
require.Eventually(t, func() bool {
412+
return promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(defaultMethod), anotherTemplate.String(), notifications.ResultSuccess)) > 0 &&
413+
promtest.ToFloat64(metrics.DispatchAttempts.WithLabelValues(string(customMethod), template.String(), notifications.ResultSuccess)) > 0
414+
}, testutil.WaitShort, testutil.IntervalFast)
415+
}
416+
342417
// hasMatchingFingerprint checks if the given metric's series fingerprint matches the reference fingerprint.
343418
func hasMatchingFingerprint(metric *dto.Metric, fp model.Fingerprint) bool {
344419
return fingerprintLabelPairs(metric.Label) == fp

0 commit comments

Comments
 (0)