@@ -25,22 +25,23 @@ import (
2525
2626func TestWorkspaceUpdates (t * testing.T ) {
2727t .Parallel ()
28- ctx := context .Background ()
2928
30- ws1ID := uuid .New ()
29+ ws1ID := uuid.UUID { 0x01 }
3130ws1IDSlice := tailnet .UUIDToByteSlice (ws1ID )
32- agent1ID := uuid .New ()
31+ agent1ID := uuid.UUID { 0x02 }
3332agent1IDSlice := tailnet .UUIDToByteSlice (agent1ID )
34- ws2ID := uuid .New ()
33+ ws2ID := uuid.UUID { 0x03 }
3534ws2IDSlice := tailnet .UUIDToByteSlice (ws2ID )
36- ws3ID := uuid .New ()
35+ ws3ID := uuid.UUID { 0x04 }
3736ws3IDSlice := tailnet .UUIDToByteSlice (ws3ID )
38- agent2ID := uuid .New ()
37+ agent2ID := uuid.UUID { 0x05 }
3938agent2IDSlice := tailnet .UUIDToByteSlice (agent2ID )
40- ws4ID := uuid .New ()
39+ ws4ID := uuid.UUID { 0x06 }
4140ws4IDSlice := tailnet .UUIDToByteSlice (ws4ID )
41+ agent3ID := uuid.UUID {0x07 }
42+ agent3IDSlice := tailnet .UUIDToByteSlice (agent3ID )
4243
43- ownerID := uuid .New ()
44+ ownerID := uuid.UUID { 0x08 }
4445memberRole ,err := rbac .RoleByName (rbac .RoleMember ())
4546require .NoError (t ,err )
4647ownerSubject := rbac.Subject {
@@ -53,9 +54,11 @@ func TestWorkspaceUpdates(t *testing.T) {
5354t .Run ("Basic" ,func (t * testing.T ) {
5455t .Parallel ()
5556
57+ ctx := testutil .Context (t ,testutil .WaitShort )
58+
5659db := & mockWorkspaceStore {
5760orderedRows : []database.GetWorkspacesAndAgentsByOwnerIDRow {
58- // Gainsa new agent
61+ // Gainsagent2
5962{
6063ID :ws1ID ,
6164Name :"ws1" ,
@@ -81,6 +84,12 @@ func TestWorkspaceUpdates(t *testing.T) {
8184Name :"ws3" ,
8285JobStatus :database .ProvisionerJobStatusSucceeded ,
8386Transition :database .WorkspaceTransitionStop ,
87+ Agents : []database.AgentIDNamePair {
88+ {
89+ ID :agent3ID ,
90+ Name :"agent3" ,
91+ },
92+ },
8493},
8594},
8695}
@@ -89,21 +98,22 @@ func TestWorkspaceUpdates(t *testing.T) {
8998cbs :map [string ]pubsub.ListenerWithErr {},
9099}
91100
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 {})
94102t .Cleanup (func () {
95103_ = updateProvider .Close ()
96104})
97105
98106sub ,err := updateProvider .Subscribe (dbauthz .As (ctx ,ownerSubject ),ownerID )
99107require .NoError (t ,err )
100- ch := sub .Updates ()
108+ defer sub .Close ()
101109
102- update ,ok := <- ch
103- require .True (t ,ok )
110+ update := testutil .RequireRecvCtx (ctx ,t ,sub .Updates ())
104111slices .SortFunc (update .UpsertedWorkspaces ,func (a ,b * proto.Workspace )int {
105112return strings .Compare (a .Name ,b .Name )
106113})
114+ slices .SortFunc (update .UpsertedAgents ,func (a ,b * proto.Agent )int {
115+ return strings .Compare (a .Name ,b .Name )
116+ })
107117require .Equal (t ,& proto.WorkspaceUpdate {
108118UpsertedWorkspaces : []* proto.Workspace {
109119{
@@ -128,6 +138,11 @@ func TestWorkspaceUpdates(t *testing.T) {
128138Name :"agent1" ,
129139WorkspaceId :ws1IDSlice ,
130140},
141+ {
142+ Id :agent3IDSlice ,
143+ Name :"agent3" ,
144+ WorkspaceId :ws3IDSlice ,
145+ },
131146},
132147DeletedWorkspaces : []* proto.Workspace {},
133148DeletedAgents : []* proto.Agent {},
@@ -169,8 +184,7 @@ func TestWorkspaceUpdates(t *testing.T) {
169184WorkspaceID :ws1ID ,
170185})
171186
172- update ,ok = <- ch
173- require .True (t ,ok )
187+ update = testutil .RequireRecvCtx (ctx ,t ,sub .Updates ())
174188slices .SortFunc (update .UpsertedWorkspaces ,func (a ,b * proto.Workspace )int {
175189return strings .Compare (a .Name ,b .Name )
176190})
@@ -203,13 +217,21 @@ func TestWorkspaceUpdates(t *testing.T) {
203217Status :proto .Workspace_STOPPED ,
204218},
205219},
206- DeletedAgents : []* proto.Agent {},
220+ DeletedAgents : []* proto.Agent {
221+ {
222+ Id :agent3IDSlice ,
223+ Name :"agent3" ,
224+ WorkspaceId :ws3IDSlice ,
225+ },
226+ },
207227},update )
208228})
209229
210230t .Run ("Resubscribe" ,func (t * testing.T ) {
211231t .Parallel ()
212232
233+ ctx := testutil .Context (t ,testutil .WaitShort )
234+
213235db := & mockWorkspaceStore {
214236orderedRows : []database.GetWorkspacesAndAgentsByOwnerIDRow {
215237{
@@ -231,8 +253,7 @@ func TestWorkspaceUpdates(t *testing.T) {
231253cbs :map [string ]pubsub.ListenerWithErr {},
232254}
233255
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 {})
236257t .Cleanup (func () {
237258_ = updateProvider .Close ()
238259})
@@ -290,7 +311,7 @@ type mockWorkspaceStore struct {
290311}
291312
292313// 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 ) {
294315return m .orderedRows ,nil
295316}
296317