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

Commit416d67b

Browse files
authored
test: use barrier inTestInflightDispatchesMetric (#15107)
Fixes:coder/internal#109
1 parent21feb42 commit416d67b

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

‎coderd/notifications/metrics_test.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package notifications_test
33
import (
44
"context"
55
"strconv"
6+
"sync"
67
"testing"
78
"time"
89

@@ -317,10 +318,12 @@ func TestInflightDispatchesMetric(t *testing.T) {
317318
})
318319

319320
handler:=&fakeHandler{}
320-
// Delayer will delay all dispatches by 2x fetch intervals to ensure we catch the requests inflight.
321-
delayer:=newDelayingHandler(cfg.FetchInterval.Value()*2,handler)
321+
constmsgCount=2
322+
323+
// Barrier handler will wait until all notification messages are in-flight.
324+
barrier:=newBarrierHandler(msgCount,handler)
322325
mgr.WithHandlers(map[database.NotificationMethod]notifications.Handler{
323-
method:delayer,
326+
method:barrier,
324327
})
325328

326329
enq,err:=notifications.NewStoreEnqueuer(cfg,api.Database,defaultHelpers(),api.Logger.Named("enqueuer"),quartz.NewReal())
@@ -329,7 +332,6 @@ func TestInflightDispatchesMetric(t *testing.T) {
329332
user:=createSampleUser(t,api.Database)
330333

331334
// WHEN: notifications are enqueued which will succeed (and be delayed during dispatch)
332-
constmsgCount=2
333335
fori:=0;i<msgCount;i++ {
334336
_,err=enq.Enqueue(ctx,user.ID,template,map[string]string{"type":"success","i":strconv.Itoa(i)},"test")
335337
require.NoError(t,err)
@@ -343,6 +345,10 @@ func TestInflightDispatchesMetric(t *testing.T) {
343345
returnpromtest.ToFloat64(metrics.InflightDispatches.WithLabelValues(string(method),template.String()))==msgCount
344346
},testutil.WaitShort,testutil.IntervalFast)
345347

348+
fori:=0;i<msgCount;i++ {
349+
barrier.wg.Done()
350+
}
351+
346352
// Wait until the handler has dispatched the given notifications.
347353
require.Eventually(t,func()bool {
348354
handler.mu.RLock()
@@ -493,27 +499,30 @@ func (u *updateSignallingInterceptor) BulkMarkNotificationMessagesFailed(ctx con
493499
returnu.Store.BulkMarkNotificationMessagesFailed(ctx,arg)
494500
}
495501

496-
typedelayingHandlerstruct {
502+
typebarrierHandlerstruct {
497503
h notifications.Handler
498504

499-
delay time.Duration
505+
wg*sync.WaitGroup
500506
}
501507

502-
funcnewDelayingHandler(delay time.Duration,handler notifications.Handler)*delayingHandler {
503-
return&delayingHandler{
504-
delay:delay,
505-
h:handler,
508+
funcnewBarrierHandler(totalint,handler notifications.Handler)*barrierHandler {
509+
varwg sync.WaitGroup
510+
wg.Add(total)
511+
512+
return&barrierHandler{
513+
h:handler,
514+
wg:&wg,
506515
}
507516
}
508517

509-
func (d*delayingHandler)Dispatcher(payload types.MessagePayload,title,bodystring) (dispatch.DeliveryFunc,error) {
510-
deliverFn,err:=d.h.Dispatcher(payload,title,body)
518+
func (bh*barrierHandler)Dispatcher(payload types.MessagePayload,title,bodystring) (dispatch.DeliveryFunc,error) {
519+
deliverFn,err:=bh.h.Dispatcher(payload,title,body)
511520
iferr!=nil {
512521
returnnil,err
513522
}
514523

515524
returnfunc(ctx context.Context,msgID uuid.UUID) (retryablebool,errerror) {
516-
time.Sleep(d.delay)
525+
bh.wg.Wait()
517526

518527
returndeliverFn(ctx,msgID)
519528
},nil

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp