From c9566171ce870ee2661afbcca2efd2ba97d6fd82 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Fri, 19 Jul 2024 11:38:29 +0200 Subject: [PATCH] fix: TestPendingUpdatesMetric flake Signed-off-by: Danny Kopping --- coderd/notifications/metrics_test.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/coderd/notifications/metrics_test.go b/coderd/notifications/metrics_test.go index 53fb8279789e2..89078425ef2fc 100644 --- a/coderd/notifications/metrics_test.go +++ b/coderd/notifications/metrics_test.go @@ -2,7 +2,6 @@ package notifications_test import ( "context" - "sync" "testing" "time" @@ -260,7 +259,7 @@ func TestPendingUpdatesMetric(t *testing.T) { require.EqualValues(t, pending, success+failure) // Unpause the interceptor so the updates can proceed. - interceptor.proceed.Broadcast() + interceptor.unpause() // Validate that the store synced the expected number of updates. require.Eventually(t, func() bool { @@ -376,7 +375,7 @@ func fingerprintLabels(lbs ...string) model.Fingerprint { type updateSignallingInterceptor struct { notifications.Store - proceed *sync.Cond + pause chan any updateSuccess chan int updateFailure chan int @@ -386,21 +385,22 @@ func newUpdateSignallingInterceptor(interceptor notifications.Store) *updateSign return &updateSignallingInterceptor{ Store: interceptor, - proceed: sync.NewCond(&sync.Mutex{}), + pause: make(chan any, 1), updateSuccess: make(chan int, 1), updateFailure: make(chan int, 1), } } +func (u *updateSignallingInterceptor) unpause() { + close(u.pause) +} + func (u *updateSignallingInterceptor) BulkMarkNotificationMessagesSent(ctx context.Context, arg database.BulkMarkNotificationMessagesSentParams) (int64, error) { u.updateSuccess <- len(arg.IDs) - u.proceed.L.Lock() - defer u.proceed.L.Unlock() - // Wait until signaled so we have a chance to read the number of pending updates. - u.proceed.Wait() + <-u.pause return u.Store.BulkMarkNotificationMessagesSent(ctx, arg) } @@ -408,11 +408,8 @@ func (u *updateSignallingInterceptor) BulkMarkNotificationMessagesSent(ctx conte func (u *updateSignallingInterceptor) BulkMarkNotificationMessagesFailed(ctx context.Context, arg database.BulkMarkNotificationMessagesFailedParams) (int64, error) { u.updateFailure <- len(arg.IDs) - u.proceed.L.Lock() - defer u.proceed.L.Unlock() - // Wait until signaled so we have a chance to read the number of pending updates. - u.proceed.Wait() + <-u.pause return u.Store.BulkMarkNotificationMessagesFailed(ctx, arg) }