@@ -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
- // Gainsa new agent
61
+ // Gainsagent2
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,22 @@ 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
+ defer sub .Close ()
101
109
102
- update ,ok := <- ch
103
- require .True (t ,ok )
110
+ update := testutil .RequireRecvCtx (ctx ,t ,sub .Updates ())
104
111
slices .SortFunc (update .UpsertedWorkspaces ,func (a ,b * proto.Workspace )int {
105
112
return strings .Compare (a .Name ,b .Name )
106
113
})
114
+ slices .SortFunc (update .UpsertedAgents ,func (a ,b * proto.Agent )int {
115
+ return strings .Compare (a .Name ,b .Name )
116
+ })
107
117
require .Equal (t ,& proto.WorkspaceUpdate {
108
118
UpsertedWorkspaces : []* proto.Workspace {
109
119
{
@@ -128,6 +138,11 @@ func TestWorkspaceUpdates(t *testing.T) {
128
138
Name :"agent1" ,
129
139
WorkspaceId :ws1IDSlice ,
130
140
},
141
+ {
142
+ Id :agent3IDSlice ,
143
+ Name :"agent3" ,
144
+ WorkspaceId :ws3IDSlice ,
145
+ },
131
146
},
132
147
DeletedWorkspaces : []* proto.Workspace {},
133
148
DeletedAgents : []* proto.Agent {},
@@ -169,8 +184,7 @@ func TestWorkspaceUpdates(t *testing.T) {
169
184
WorkspaceID :ws1ID ,
170
185
})
171
186
172
- update ,ok = <- ch
173
- require .True (t ,ok )
187
+ update = testutil .RequireRecvCtx (ctx ,t ,sub .Updates ())
174
188
slices .SortFunc (update .UpsertedWorkspaces ,func (a ,b * proto.Workspace )int {
175
189
return strings .Compare (a .Name ,b .Name )
176
190
})
@@ -203,13 +217,21 @@ func TestWorkspaceUpdates(t *testing.T) {
203
217
Status :proto .Workspace_STOPPED ,
204
218
},
205
219
},
206
- DeletedAgents : []* proto.Agent {},
220
+ DeletedAgents : []* proto.Agent {
221
+ {
222
+ Id :agent3IDSlice ,
223
+ Name :"agent3" ,
224
+ WorkspaceId :ws3IDSlice ,
225
+ },
226
+ },
207
227
},update )
208
228
})
209
229
210
230
t .Run ("Resubscribe" ,func (t * testing.T ) {
211
231
t .Parallel ()
212
232
233
+ ctx := testutil .Context (t ,testutil .WaitShort )
234
+
213
235
db := & mockWorkspaceStore {
214
236
orderedRows : []database.GetWorkspacesAndAgentsByOwnerIDRow {
215
237
{
@@ -231,8 +253,7 @@ func TestWorkspaceUpdates(t *testing.T) {
231
253
cbs :map [string ]pubsub.ListenerWithErr {},
232
254
}
233
255
234
- updateProvider ,err := coderd .NewUpdatesProvider (slogtest .Make (t ,nil ),ps ,db ,& mockAuthorizer {})
235
- require .NoError (t ,err )
256
+ updateProvider := coderd .NewUpdatesProvider (slogtest .Make (t ,nil ),ps ,db ,& mockAuthorizer {})
236
257
t .Cleanup (func () {
237
258
_ = updateProvider .Close ()
238
259
})
@@ -290,7 +311,7 @@ type mockWorkspaceStore struct {
290
311
}
291
312
292
313
// GetAuthorizedWorkspacesAndAgentsByOwnerID implements coderd.UpdatesQuerier.
293
- func (m * mockWorkspaceStore )GetAuthorizedWorkspacesAndAgentsByOwnerID (context.Context , uuid.UUID , rbac. PreparedAuthorized ) ([]database.GetWorkspacesAndAgentsByOwnerIDRow ,error ) {
314
+ func (m * mockWorkspaceStore )GetWorkspacesAndAgentsByOwnerID (context.Context , uuid.UUID ) ([]database.GetWorkspacesAndAgentsByOwnerIDRow ,error ) {
294
315
return m .orderedRows ,nil
295
316
}
296
317