Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb3679fe

Browse files
committed
Test metric collection when dispatching with custom methods
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent9d7761d commitb3679fe

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

‎coderd/notifications/metrics_test.go

Lines changed: 75 additions & 0 deletions
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+
funcTestCustomMethodMetricCollection(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+
defersmtpHandler.mu.RUnlock()
404+
deferwebhookHandler.mu.RUnlock()
405+
406+
returnlen(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+
returnpromtest.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
funchasMatchingFingerprint(metric*dto.Metric,fp model.Fingerprint)bool {
344419
returnfingerprintLabelPairs(metric.Label)==fp

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp