@@ -19,8 +19,11 @@ import (
19
19
"github.com/coder/coder/v2/coderd/database"
20
20
"github.com/coder/coder/v2/coderd/database/dbmock"
21
21
"github.com/coder/coder/v2/coderd/database/dbtime"
22
+ "github.com/coder/coder/v2/coderd/database/pubsub"
22
23
"github.com/coder/coder/v2/coderd/prometheusmetrics"
23
24
"github.com/coder/coder/v2/coderd/schedule"
25
+ "github.com/coder/coder/v2/codersdk"
26
+ "github.com/coder/coder/v2/testutil"
24
27
)
25
28
26
29
type statsBatcher struct {
@@ -78,8 +81,10 @@ func TestUpdateStates(t *testing.T) {
78
81
t .Parallel ()
79
82
80
83
var (
81
- now = dbtime .Now ()
82
- dbM = dbmock .NewMockStore (gomock .NewController (t ))
84
+ now = dbtime .Now ()
85
+ dbM = dbmock .NewMockStore (gomock .NewController (t ))
86
+ ps = pubsub .NewInMemory ()
87
+
83
88
templateScheduleStore = schedule.MockTemplateScheduleStore {
84
89
GetFn : func (context.Context , database.Store , uuid.UUID ) (schedule.TemplateScheduleOptions , error ) {
85
90
panic ("should not be called" )
@@ -125,6 +130,7 @@ func TestUpdateStates(t *testing.T) {
125
130
return agent , nil
126
131
},
127
132
Database : dbM ,
133
+ Pubsub : ps ,
128
134
StatsBatcher : batcher ,
129
135
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
130
136
AgentStatsRefreshInterval : 10 * time .Second ,
@@ -164,6 +170,14 @@ func TestUpdateStates(t *testing.T) {
164
170
// User gets fetched to hit the UpdateAgentMetricsFn.
165
171
dbM .EXPECT ().GetUserByID (gomock .Any (), user .ID ).Return (user , nil )
166
172
173
+ // Ensure that pubsub notifications are sent.
174
+ notifyDescription := make (chan []byte )
175
+ ps .Subscribe (codersdk .WorkspaceNotifyChannel (workspace .ID ), func (_ context.Context , description []byte ) {
176
+ go func () {
177
+ notifyDescription <- description
178
+ }()
179
+ })
180
+
167
181
resp , err := api .UpdateStats (context .Background (), req )
168
182
require .NoError (t , err )
169
183
require .Equal (t , & agentproto.UpdateStatsResponse {
@@ -179,7 +193,13 @@ func TestUpdateStates(t *testing.T) {
179
193
require .Equal (t , user .ID , batcher .lastUserID )
180
194
require .Equal (t , workspace .ID , batcher .lastWorkspaceID )
181
195
require .Equal (t , req .Stats , batcher .lastStats )
182
-
196
+ ctx := testutil .Context (t , testutil .WaitShort )
197
+ select {
198
+ case <- ctx .Done ():
199
+ t .Error ("timed out while waiting for pubsub notification" )
200
+ case description := <- notifyDescription :
201
+ require .Equal (t , description , []byte {})
202
+ }
183
203
require .True (t , updateAgentMetricsFnCalled )
184
204
})
185
205
@@ -189,6 +209,7 @@ func TestUpdateStates(t *testing.T) {
189
209
var (
190
210
now = dbtime .Now ()
191
211
dbM = dbmock .NewMockStore (gomock .NewController (t ))
212
+ ps = pubsub .NewInMemory ()
192
213
templateScheduleStore = schedule.MockTemplateScheduleStore {
193
214
GetFn : func (context.Context , database.Store , uuid.UUID ) (schedule.TemplateScheduleOptions , error ) {
194
215
panic ("should not be called" )
@@ -214,6 +235,7 @@ func TestUpdateStates(t *testing.T) {
214
235
return agent , nil
215
236
},
216
237
Database : dbM ,
238
+ Pubsub : ps ,
217
239
StatsBatcher : batcher ,
218
240
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
219
241
AgentStatsRefreshInterval : 10 * time .Second ,
@@ -245,6 +267,7 @@ func TestUpdateStates(t *testing.T) {
245
267
246
268
var (
247
269
dbM = dbmock .NewMockStore (gomock .NewController (t ))
270
+ ps = pubsub .NewInMemory ()
248
271
req = & agentproto.UpdateStatsRequest {
249
272
Stats : & agentproto.Stats {
250
273
ConnectionsByProto : map [string ]int64 {}, // len() == 0
@@ -256,6 +279,7 @@ func TestUpdateStates(t *testing.T) {
256
279
return agent , nil
257
280
},
258
281
Database : dbM ,
282
+ Pubsub : ps ,
259
283
StatsBatcher : nil , // should not be called
260
284
TemplateScheduleStore : nil , // should not be called
261
285
AgentStatsRefreshInterval : 10 * time .Second ,
@@ -290,7 +314,9 @@ func TestUpdateStates(t *testing.T) {
290
314
nextAutostart := now .Add (30 * time .Minute ).UTC () // always sent to DB as UTC
291
315
292
316
var (
293
- dbM = dbmock .NewMockStore (gomock .NewController (t ))
317
+ dbM = dbmock .NewMockStore (gomock .NewController (t ))
318
+ ps = pubsub .NewInMemory ()
319
+
294
320
templateScheduleStore = schedule.MockTemplateScheduleStore {
295
321
GetFn : func (context.Context , database.Store , uuid.UUID ) (schedule.TemplateScheduleOptions , error ) {
296
322
return schedule.TemplateScheduleOptions {
@@ -322,6 +348,7 @@ func TestUpdateStates(t *testing.T) {
322
348
return agent , nil
323
349
},
324
350
Database : dbM ,
351
+ Pubsub : ps ,
325
352
StatsBatcher : batcher ,
326
353
TemplateScheduleStore : templateScheduleStorePtr (templateScheduleStore ),
327
354
AgentStatsRefreshInterval : 15 * time .Second ,
0 commit comments