fix: TestPendingUpdatesMetric flake (#13944)

Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
Danny Kopping
2024-07-19 12:54:15 +02:00
committed by GitHub
parent 492ab1cc7e
commit c88e4162d8
+9 -12
View File
@@ -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)
}