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

Commit78c4a32

Browse files
committed
fix: prevent test flakiness
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent839918c commit78c4a32

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

‎coderd/notifications/notifications_test.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ func TestNotifierPaused(t *testing.T) {
576576
ctx:=dbauthz.AsSystemRestricted(testutil.Context(t,testutil.WaitSuperLong))
577577
_,_,api:=coderdtest.NewWithAPI(t,nil)
578578

579-
// Prepare the test
579+
// Prepare the test.
580580
handler:=&fakeHandler{}
581581
method:=database.NotificationMethodSmtp
582582
user:=createSampleUser(t,api.Database)
@@ -593,32 +593,38 @@ func TestNotifierPaused(t *testing.T) {
593593
enq,err:=notifications.NewStoreEnqueuer(cfg,api.Database,defaultHelpers(),api.Logger.Named("enqueuer"),quartz.NewReal())
594594
require.NoError(t,err)
595595

596-
mgr.Run(ctx)
597-
598596
// Pause the notifier.
599597
settingsJSON,err:=json.Marshal(&codersdk.NotificationsSettings{NotifierPaused:true})
600598
require.NoError(t,err)
601599
err=api.Database.UpsertNotificationsSettings(ctx,string(settingsJSON))
602600
require.NoError(t,err)
603601

602+
// Start the manager so that notifications are processed, except it will be paused at this point.
603+
// If it is started before pausing, there's a TOCTOU possibility between checking whether the notifier is paused or
604+
// not, and processing the messages (see notifier.run).
605+
mgr.Run(ctx)
606+
604607
// Notifier is paused, enqueue the next message.
605608
sid,err:=enq.Enqueue(ctx,user.ID,notifications.TemplateWorkspaceDeleted,map[string]string{"type":"success","i":"1"},"test")
606609
require.NoError(t,err)
607610

608-
// Sleep for a few fetch intervals to be sure we aren't getting false-positives in the next step.
609-
// TODO: use quartz instead.
610-
time.Sleep(fetchInterval*5)
611-
612611
// Ensure we have a pending message and it's the expected one.
612+
pendingMessages,err:=api.Database.GetNotificationMessagesByStatus(ctx, database.GetNotificationMessagesByStatusParams{
613+
Status:database.NotificationMessageStatusPending,
614+
Limit:10,
615+
})
616+
require.NoError(t,err)
617+
require.Len(t,pendingMessages,1)
618+
require.Equal(t,pendingMessages[0].ID.String(),sid.String())
619+
620+
// Wait a few fetch intervals to be sure that no new notifications are being sent.
621+
// TODO: use quartz instead.
613622
require.Eventually(t,func()bool {
614-
pendingMessages,err:=api.Database.GetNotificationMessagesByStatus(ctx, database.GetNotificationMessagesByStatusParams{
615-
Status:database.NotificationMessageStatusPending,
616-
Limit:10,
617-
})
618-
assert.NoError(t,err)
619-
returnlen(pendingMessages)==1&&
620-
pendingMessages[0].ID.String()==sid.String()
621-
},testutil.WaitShort,testutil.IntervalFast)
623+
handler.mu.RLock()
624+
deferhandler.mu.RUnlock()
625+
626+
returnlen(handler.succeeded)+len(handler.failed)==0
627+
},fetchInterval*5,testutil.IntervalFast)
622628

623629
// Unpause the notifier.
624630
settingsJSON,err=json.Marshal(&codersdk.NotificationsSettings{NotifierPaused:false})
@@ -631,7 +637,7 @@ func TestNotifierPaused(t *testing.T) {
631637
handler.mu.RLock()
632638
deferhandler.mu.RUnlock()
633639
returnslices.Contains(handler.succeeded,sid.String())
634-
},testutil.WaitShort,testutil.IntervalFast)
640+
},fetchInterval*5,testutil.IntervalFast)
635641
}
636642

637643
//go:embed events.go

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp