Skip to content

Commit 49b2163

Browse files
committed
Correct TestBufferedUpdates to count updated entries, use real db again
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent 33b0eda commit 49b2163

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

coderd/notifications/manager_test.go

+30-10
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"testing"
88
"time"
99

10-
"github.com/coder/coder/v2/coderd/database/pubsub"
10+
"github.com/coder/coder/v2/coderd/database/dbtestutil"
1111
"github.com/google/uuid"
12+
"github.com/stretchr/testify/assert"
1213
"github.com/stretchr/testify/require"
1314
"golang.org/x/xerrors"
1415

@@ -50,9 +51,10 @@ func TestBufferedUpdates(t *testing.T) {
5051
t.Parallel()
5152

5253
// setup
53-
ctx := context.Background()
54-
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true, IgnoredErrorIs: []error{}}).Leveled(slog.LevelDebug)
55-
db := dbmem.New()
54+
if !dbtestutil.WillUsePostgres() {
55+
t.Skip("This test requires postgres")
56+
}
57+
ctx, logger, db, ps := setup(t)
5658
interceptor := &bulkUpdateInterceptor{Store: db}
5759

5860
santa := &santaHandler{}
@@ -62,7 +64,7 @@ func TestBufferedUpdates(t *testing.T) {
6264
require.NoError(t, err)
6365
mgr.WithHandlers(handlers)
6466

65-
client := coderdtest.New(t, &coderdtest.Options{Database: db, Pubsub: pubsub.NewInMemory()})
67+
client := coderdtest.New(t, &coderdtest.Options{Database: db, Pubsub: ps})
6668
user := coderdtest.CreateFirstUser(t, client)
6769

6870
// given
@@ -88,7 +90,16 @@ func TestBufferedUpdates(t *testing.T) {
8890
require.NoError(t, mgr.Stop(ctx))
8991

9092
// Wait until both success & failure updates have been sent to the store.
91-
require.Eventually(t, func() bool { return interceptor.failed.Load() == 1 && interceptor.sent.Load() == 2 }, testutil.WaitMedium, testutil.IntervalFast)
93+
require.EventuallyWithT(t, func(ct *assert.CollectT) {
94+
if err := interceptor.err.Load(); err != nil {
95+
ct.Errorf("bulk update encountered error: %s", err)
96+
// Panic when an unexpected error occurs.
97+
ct.FailNow()
98+
}
99+
100+
assert.EqualValues(ct, 1, interceptor.failed.Load())
101+
assert.EqualValues(ct, 2, interceptor.sent.Load())
102+
}, testutil.WaitMedium, testutil.IntervalFast)
92103
}
93104

94105
func TestBuildPayload(t *testing.T) {
@@ -150,16 +161,25 @@ type bulkUpdateInterceptor struct {
150161

151162
sent atomic.Int32
152163
failed atomic.Int32
164+
err atomic.Value
153165
}
154166

155167
func (b *bulkUpdateInterceptor) BulkMarkNotificationMessagesSent(ctx context.Context, arg database.BulkMarkNotificationMessagesSentParams) (int64, error) {
156-
b.sent.Add(int32(len(arg.IDs)))
157-
return b.Store.BulkMarkNotificationMessagesSent(ctx, arg)
168+
updated, err := b.Store.BulkMarkNotificationMessagesSent(ctx, arg)
169+
b.sent.Add(int32(updated))
170+
if err != nil {
171+
b.err.Store(err)
172+
}
173+
return updated, err
158174
}
159175

160176
func (b *bulkUpdateInterceptor) BulkMarkNotificationMessagesFailed(ctx context.Context, arg database.BulkMarkNotificationMessagesFailedParams) (int64, error) {
161-
b.failed.Add(int32(len(arg.IDs)))
162-
return b.Store.BulkMarkNotificationMessagesFailed(ctx, arg)
177+
updated, err := b.Store.BulkMarkNotificationMessagesFailed(ctx, arg)
178+
b.failed.Add(int32(updated))
179+
if err != nil {
180+
b.err.Store(err)
181+
}
182+
return updated, err
163183
}
164184

165185
// santaHandler only dispatches nice messages.

0 commit comments

Comments
 (0)