@@ -234,6 +234,8 @@ func (w *fakeWatcher) sendEventWaitNextCalled(ctx context.Context, event fsnotif
234234// fakeSubAgentClient implements SubAgentClient for testing purposes.
235235type fakeSubAgentClient struct {
236236 logger slog.Logger
237+
238+ mu sync.Mutex // Protects following.
237239 agents map [uuid.UUID ]agentcontainers.SubAgent
238240
239241 listErrC chan error // If set, send to return error, close to return nil.
@@ -254,6 +256,8 @@ func (m *fakeSubAgentClient) List(ctx context.Context) ([]agentcontainers.SubAge
254256 }
255257 }
256258 }
259+ m .mu .Lock ()
260+ defer m .mu .Unlock ()
257261 var agents []agentcontainers.SubAgent
258262 for _ , agent := range m .agents {
259263 agents = append (agents , agent )
@@ -283,6 +287,9 @@ func (m *fakeSubAgentClient) Create(ctx context.Context, agent agentcontainers.S
283287 return agentcontainers.SubAgent {}, xerrors .New ("operating system must be set" )
284288 }
285289
290+ m .mu .Lock ()
291+ defer m .mu .Unlock ()
292+
286293 for _ , a := range m .agents {
287294 if a .Name == agent .Name {
288295 return agentcontainers.SubAgent {}, & pq.Error {
@@ -314,6 +321,8 @@ func (m *fakeSubAgentClient) Delete(ctx context.Context, id uuid.UUID) error {
314321 }
315322 }
316323 }
324+ m .mu .Lock ()
325+ defer m .mu .Unlock ()
317326 if m .agents == nil {
318327 m .agents = make (map [uuid.UUID ]agentcontainers.SubAgent )
319328 }
@@ -2162,6 +2171,10 @@ func TestAPI(t *testing.T) {
21622171 nowRecreateSuccessTrap .MustWait (ctx ).MustRelease (ctx )
21632172 nowRecreateSuccessTrap .Close ()
21642173
2174+ // Advance the clock to run the devcontainer state update routine.
2175+ _ , aw := mClock .AdvanceNext ()
2176+ aw .MustWait (ctx )
2177+
21652178 req = httptest .NewRequest (http .MethodGet , "/" , nil )
21662179 rec = httptest .NewRecorder ()
21672180 r .ServeHTTP (rec , req )
@@ -2178,7 +2191,7 @@ func TestAPI(t *testing.T) {
21782191 // available for use.
21792192 require .Len (t , response .Devcontainers , 1 )
21802193 assert .Equal (t , codersdk .WorkspaceAgentDevcontainerStatusRunning , response .Devcontainers [0 ].Status )
2181- assert .NotNil (t , response .Devcontainers [0 ].Container )
2194+ require .NotNil (t , response .Devcontainers [0 ].Container )
21822195 assert .Equal (t , testContainer .ID , response .Devcontainers [0 ].Container .ID )
21832196 })
21842197
0 commit comments