7
7
"testing"
8
8
"time"
9
9
10
- "github.com/coder/coder/v2/coderd/database/pubsub "
10
+ "github.com/coder/coder/v2/coderd/database/dbtestutil "
11
11
"github.com/google/uuid"
12
+ "github.com/stretchr/testify/assert"
12
13
"github.com/stretchr/testify/require"
13
14
"golang.org/x/xerrors"
14
15
@@ -50,9 +51,10 @@ func TestBufferedUpdates(t *testing.T) {
50
51
t .Parallel ()
51
52
52
53
// 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 )
56
58
interceptor := & bulkUpdateInterceptor {Store : db }
57
59
58
60
santa := & santaHandler {}
@@ -62,7 +64,7 @@ func TestBufferedUpdates(t *testing.T) {
62
64
require .NoError (t , err )
63
65
mgr .WithHandlers (handlers )
64
66
65
- client := coderdtest .New (t , & coderdtest.Options {Database : db , Pubsub : pubsub . NewInMemory () })
67
+ client := coderdtest .New (t , & coderdtest.Options {Database : db , Pubsub : ps })
66
68
user := coderdtest .CreateFirstUser (t , client )
67
69
68
70
// given
@@ -88,7 +90,16 @@ func TestBufferedUpdates(t *testing.T) {
88
90
require .NoError (t , mgr .Stop (ctx ))
89
91
90
92
// 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 )
92
103
}
93
104
94
105
func TestBuildPayload (t * testing.T ) {
@@ -150,16 +161,25 @@ type bulkUpdateInterceptor struct {
150
161
151
162
sent atomic.Int32
152
163
failed atomic.Int32
164
+ err atomic.Value
153
165
}
154
166
155
167
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
158
174
}
159
175
160
176
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
163
183
}
164
184
165
185
// santaHandler only dispatches nice messages.
0 commit comments