From af135725ce1680a8c5870c671eb23e00ca1f43bd Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 14 Jul 2025 22:07:01 +0000 Subject: [PATCH 1/3] fix(agent/agentcontainers): resolve flake --- agent/agentcontainers/api_test.go | 34 +++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/agent/agentcontainers/api_test.go b/agent/agentcontainers/api_test.go index 75b9342379a35..001b108c96e13 100644 --- a/agent/agentcontainers/api_test.go +++ b/agent/agentcontainers/api_test.go @@ -2883,8 +2883,21 @@ func TestAPI(t *testing.T) { Op: fsnotify.Write, }) - err = api.RefreshContainers(ctx) - require.NoError(t, err) + // Retry RefreshContainers until subagent injection succeeds + // This handles the race condition where the watcher loop may not have + // finished processing the config change yet + var subAgentCreated bool + for i := 0; i < 10; i++ { + err = api.RefreshContainers(ctx) + require.NoError(t, err) + + if len(fakeSAC.created) > 0 { + subAgentCreated = true + break + } + time.Sleep(10 * time.Millisecond) + } + require.True(t, subAgentCreated, "subagent should be created after config change") t.Log("Phase 2: Cont, waiting for sub agent to exit") exitSubAgentOnce.Do(func() { @@ -2919,8 +2932,21 @@ func TestAPI(t *testing.T) { Op: fsnotify.Write, }) - err = api.RefreshContainers(ctx) - require.NoError(t, err) + // Retry RefreshContainers until subagent injection succeeds + // This handles the race condition where the watcher loop may not have + // finished processing the config change yet + subAgentCreated = false + for i := 0; i < 10; i++ { + err = api.RefreshContainers(ctx) + require.NoError(t, err) + + if len(fakeSAC.created) > 0 { + subAgentCreated = true + break + } + time.Sleep(10 * time.Millisecond) + } + require.True(t, subAgentCreated, "subagent should be created after config change") req = httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx) rec = httptest.NewRecorder() From cd45e06a5a74fe1efb0dfffd7fe39008bb2ea7f5 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 14 Jul 2025 22:13:45 +0000 Subject: [PATCH 2/3] chore: we have eventually --- agent/agentcontainers/api_test.go | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/agent/agentcontainers/api_test.go b/agent/agentcontainers/api_test.go index 001b108c96e13..c8f51def6effd 100644 --- a/agent/agentcontainers/api_test.go +++ b/agent/agentcontainers/api_test.go @@ -2883,21 +2883,12 @@ func TestAPI(t *testing.T) { Op: fsnotify.Write, }) - // Retry RefreshContainers until subagent injection succeeds - // This handles the race condition where the watcher loop may not have - // finished processing the config change yet - var subAgentCreated bool - for i := 0; i < 10; i++ { + require.Eventuallyf(t, func() bool { err = api.RefreshContainers(ctx) require.NoError(t, err) - if len(fakeSAC.created) > 0 { - subAgentCreated = true - break - } - time.Sleep(10 * time.Millisecond) - } - require.True(t, subAgentCreated, "subagent should be created after config change") + return len(fakeSAC.created) > 0 + }, testutil.WaitShort, testutil.IntervalFast, "subagent should be created after config change") t.Log("Phase 2: Cont, waiting for sub agent to exit") exitSubAgentOnce.Do(func() { @@ -2932,21 +2923,12 @@ func TestAPI(t *testing.T) { Op: fsnotify.Write, }) - // Retry RefreshContainers until subagent injection succeeds - // This handles the race condition where the watcher loop may not have - // finished processing the config change yet - subAgentCreated = false - for i := 0; i < 10; i++ { + require.Eventuallyf(t, func() bool { err = api.RefreshContainers(ctx) require.NoError(t, err) - if len(fakeSAC.created) > 0 { - subAgentCreated = true - break - } - time.Sleep(10 * time.Millisecond) - } - require.True(t, subAgentCreated, "subagent should be created after config change") + return len(fakeSAC.created) > 0 + }, testutil.WaitShort, testutil.IntervalFast, "subagent should be created after config change") req = httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx) rec = httptest.NewRecorder() From d99373f9b1cfc569dc55bcb5876f38aea55b0963 Mon Sep 17 00:00:00 2001 From: Danielle Maywood Date: Mon, 14 Jul 2025 22:22:45 +0000 Subject: [PATCH 3/3] fix: thanks copilot --- agent/agentcontainers/api_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/agent/agentcontainers/api_test.go b/agent/agentcontainers/api_test.go index c8f51def6effd..9451461bb3215 100644 --- a/agent/agentcontainers/api_test.go +++ b/agent/agentcontainers/api_test.go @@ -2887,7 +2887,7 @@ func TestAPI(t *testing.T) { err = api.RefreshContainers(ctx) require.NoError(t, err) - return len(fakeSAC.created) > 0 + return len(fakeSAC.agents) == 1 }, testutil.WaitShort, testutil.IntervalFast, "subagent should be created after config change") t.Log("Phase 2: Cont, waiting for sub agent to exit") @@ -2927,8 +2927,8 @@ func TestAPI(t *testing.T) { err = api.RefreshContainers(ctx) require.NoError(t, err) - return len(fakeSAC.created) > 0 - }, testutil.WaitShort, testutil.IntervalFast, "subagent should be created after config change") + return len(fakeSAC.agents) == 0 + }, testutil.WaitShort, testutil.IntervalFast, "subagent should be deleted after config change") req = httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx) rec = httptest.NewRecorder()