@@ -25,22 +25,23 @@ import (
25
25
26
26
func TestWorkspaceUpdates (t * testing.T ) {
27
27
t .Parallel ()
28
- ctx := context .Background ()
29
28
30
- ws1ID := uuid .New ()
29
+ ws1ID := uuid.UUID { 0x01 }
31
30
ws1IDSlice := tailnet .UUIDToByteSlice (ws1ID )
32
- agent1ID := uuid .New ()
31
+ agent1ID := uuid.UUID { 0x02 }
33
32
agent1IDSlice := tailnet .UUIDToByteSlice (agent1ID )
34
- ws2ID := uuid .New ()
33
+ ws2ID := uuid.UUID { 0x03 }
35
34
ws2IDSlice := tailnet .UUIDToByteSlice (ws2ID )
36
- ws3ID := uuid .New ()
35
+ ws3ID := uuid.UUID { 0x04 }
37
36
ws3IDSlice := tailnet .UUIDToByteSlice (ws3ID )
38
- agent2ID := uuid .New ()
37
+ agent2ID := uuid.UUID { 0x05 }
39
38
agent2IDSlice := tailnet .UUIDToByteSlice (agent2ID )
40
- ws4ID := uuid .New ()
39
+ ws4ID := uuid.UUID { 0x06 }
41
40
ws4IDSlice := tailnet .UUIDToByteSlice (ws4ID )
41
+ agent3ID := uuid.UUID {0x07 }
42
+ agent3IDSlice := tailnet .UUIDToByteSlice (agent3ID )
42
43
43
- ownerID := uuid .New ()
44
+ ownerID := uuid.UUID { 0x08 }
44
45
memberRole , err := rbac .RoleByName (rbac .RoleMember ())
45
46
require .NoError (t , err )
46
47
ownerSubject := rbac.Subject {
@@ -53,9 +54,11 @@ func TestWorkspaceUpdates(t *testing.T) {
53
54
t .Run ("Basic" , func (t * testing.T ) {
54
55
t .Parallel ()
55
56
57
+ ctx := testutil .Context (t , testutil .WaitShort )
58
+
56
59
db := & mockWorkspaceStore {
57
60
orderedRows : []database.GetWorkspacesAndAgentsByOwnerIDRow {
58
- // Gains a new agent
61
+ // Gains agent2
59
62
{
60
63
ID : ws1ID ,
61
64
Name : "ws1" ,
@@ -81,6 +84,12 @@ func TestWorkspaceUpdates(t *testing.T) {
81
84
Name : "ws3" ,
82
85
JobStatus : database .ProvisionerJobStatusSucceeded ,
83
86
Transition : database .WorkspaceTransitionStop ,
87
+ Agents : []database.AgentIDNamePair {
88
+ {
89
+ ID : agent3ID ,
90
+ Name : "agent3" ,
91
+ },
92
+ },
84
93
},
85
94
},
86
95
}
@@ -89,21 +98,24 @@ func TestWorkspaceUpdates(t *testing.T) {
89
98
cbs : map [string ]pubsub.ListenerWithErr {},
90
99
}
91
100
92
- updateProvider , err := coderd .NewUpdatesProvider (slogtest .Make (t , nil ), ps , db , & mockAuthorizer {})
93
- require .NoError (t , err )
101
+ updateProvider := coderd .NewUpdatesProvider (slogtest .Make (t , nil ), ps , db , & mockAuthorizer {})
94
102
t .Cleanup (func () {
95
103
_ = updateProvider .Close ()
96
104
})
97
105
98
106
sub , err := updateProvider .Subscribe (dbauthz .As (ctx , ownerSubject ), ownerID )
99
107
require .NoError (t , err )
100
- ch := sub .Updates ()
108
+ t .Cleanup (func () {
109
+ _ = sub .Close ()
110
+ })
101
111
102
- update , ok := <- ch
103
- require .True (t , ok )
112
+ update := testutil .RequireRecvCtx (ctx , t , sub .Updates ())
104
113
slices .SortFunc (update .UpsertedWorkspaces , func (a , b * proto.Workspace ) int {
105
114
return strings .Compare (a .Name , b .Name )
106
115
})
116
+ slices .SortFunc (update .UpsertedAgents , func (a , b * proto.Agent ) int {
117
+ return strings .Compare (a .Name , b .Name )
118
+ })
107
119
require .Equal (t , & proto.WorkspaceUpdate {
108
120
UpsertedWorkspaces : []* proto.Workspace {
109
121
{
@@ -128,6 +140,11 @@ func TestWorkspaceUpdates(t *testing.T) {
128
140
Name : "agent1" ,
129
141
WorkspaceId : ws1IDSlice ,
130
142
},
143
+ {
144
+ Id : agent3IDSlice ,
145
+ Name : "agent3" ,
146
+ WorkspaceId : ws3IDSlice ,
147
+ },
131
148
},
132
149
DeletedWorkspaces : []* proto.Workspace {},
133
150
DeletedAgents : []* proto.Agent {},
@@ -169,8 +186,7 @@ func TestWorkspaceUpdates(t *testing.T) {
169
186
WorkspaceID : ws1ID ,
170
187
})
171
188
172
- update , ok = <- ch
173
- require .True (t , ok )
189
+ update = testutil .RequireRecvCtx (ctx , t , sub .Updates ())
174
190
slices .SortFunc (update .UpsertedWorkspaces , func (a , b * proto.Workspace ) int {
175
191
return strings .Compare (a .Name , b .Name )
176
192
})
@@ -203,13 +219,21 @@ func TestWorkspaceUpdates(t *testing.T) {
203
219
Status : proto .Workspace_STOPPED ,
204
220
},
205
221
},
206
- DeletedAgents : []* proto.Agent {},
222
+ DeletedAgents : []* proto.Agent {
223
+ {
224
+ Id : agent3IDSlice ,
225
+ Name : "agent3" ,
226
+ WorkspaceId : ws3IDSlice ,
227
+ },
228
+ },
207
229
}, update )
208
230
})
209
231
210
232
t .Run ("Resubscribe" , func (t * testing.T ) {
211
233
t .Parallel ()
212
234
235
+ ctx := testutil .Context (t , testutil .WaitShort )
236
+
213
237
db := & mockWorkspaceStore {
214
238
orderedRows : []database.GetWorkspacesAndAgentsByOwnerIDRow {
215
239
{
@@ -231,15 +255,16 @@ func TestWorkspaceUpdates(t *testing.T) {
231
255
cbs : map [string ]pubsub.ListenerWithErr {},
232
256
}
233
257
234
- updateProvider , err := coderd .NewUpdatesProvider (slogtest .Make (t , nil ), ps , db , & mockAuthorizer {})
235
- require .NoError (t , err )
258
+ updateProvider := coderd .NewUpdatesProvider (slogtest .Make (t , nil ), ps , db , & mockAuthorizer {})
236
259
t .Cleanup (func () {
237
260
_ = updateProvider .Close ()
238
261
})
239
262
240
263
sub , err := updateProvider .Subscribe (dbauthz .As (ctx , ownerSubject ), ownerID )
241
264
require .NoError (t , err )
242
- ch := sub .Updates ()
265
+ t .Cleanup (func () {
266
+ _ = sub .Close ()
267
+ })
243
268
244
269
expected := & proto.WorkspaceUpdate {
245
270
UpsertedWorkspaces : []* proto.Workspace {
@@ -260,18 +285,19 @@ func TestWorkspaceUpdates(t *testing.T) {
260
285
DeletedAgents : []* proto.Agent {},
261
286
}
262
287
263
- update := testutil .RequireRecvCtx (ctx , t , ch )
288
+ update := testutil .RequireRecvCtx (ctx , t , sub . Updates () )
264
289
slices .SortFunc (update .UpsertedWorkspaces , func (a , b * proto.Workspace ) int {
265
290
return strings .Compare (a .Name , b .Name )
266
291
})
267
292
require .Equal (t , expected , update )
268
293
294
+ resub , err := updateProvider .Subscribe (dbauthz .As (ctx , ownerSubject ), ownerID )
269
295
require .NoError (t , err )
270
- sub , err = updateProvider . Subscribe ( dbauthz . As ( ctx , ownerSubject ), ownerID )
271
- require . NoError ( t , err )
272
- ch = sub . Updates ( )
296
+ t . Cleanup ( func () {
297
+ _ = resub . Close ( )
298
+ } )
273
299
274
- update = testutil .RequireRecvCtx (ctx , t , ch )
300
+ update = testutil .RequireRecvCtx (ctx , t , resub . Updates () )
275
301
slices .SortFunc (update .UpsertedWorkspaces , func (a , b * proto.Workspace ) int {
276
302
return strings .Compare (a .Name , b .Name )
277
303
})
@@ -290,7 +316,7 @@ type mockWorkspaceStore struct {
290
316
}
291
317
292
318
// GetAuthorizedWorkspacesAndAgentsByOwnerID implements coderd.UpdatesQuerier.
293
- func (m * mockWorkspaceStore ) GetAuthorizedWorkspacesAndAgentsByOwnerID (context.Context , uuid.UUID , rbac. PreparedAuthorized ) ([]database.GetWorkspacesAndAgentsByOwnerIDRow , error ) {
319
+ func (m * mockWorkspaceStore ) GetWorkspacesAndAgentsByOwnerID (context.Context , uuid.UUID ) ([]database.GetWorkspacesAndAgentsByOwnerIDRow , error ) {
294
320
return m .orderedRows , nil
295
321
}
296
322
0 commit comments