From d261034af689e7aec1b00912ab18020e6400f1cc Mon Sep 17 00:00:00 2001 From: aoiasd <45024769+aoiasd@users.noreply.github.com> Date: Wed, 31 Dec 2025 15:59:21 +0800 Subject: [PATCH] enhance: fix unstable config util unit test (#46702) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Core invariant: config refresh events must reliably propagate updated values and evict cached entries within a bounded time window; tests must observe this deterministically without relying on fixed sleeps. - Logic simplified: brittle fixed time.Sleep delays and separate error assertions were replaced by assert.Eventually polling blocks that combine value checks and cache-eviction verification, and consolidated checks to reduce redundant assertions. - Why no data loss / no behavior regression: only test synchronization and assertions were changed—production config manager code paths (value propagation, KV puts, cache eviction) are untouched; tests now wait for the same outcomes more robustly, so no mutation of runtime behavior or storage occurs. - Enhancement scope: this is a test-stability improvement (no new runtime capability); it fixes flaky unit tests (root cause: timing assumptions) by replacing fixed waits with bounded polling and by using t.Context for KV puts to align test context usage. Signed-off-by: aoiasd --- pkg/config/manager_test.go | 46 +++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/pkg/config/manager_test.go b/pkg/config/manager_test.go index df3d953ece..5d230f8d3c 100644 --- a/pkg/config/manager_test.go +++ b/pkg/config/manager_test.go @@ -150,8 +150,7 @@ func TestOnEvent(t *testing.T) { os.WriteFile(yamlFile, []byte("a.b: aaa"), 0o600) assert.Eventually(t, func() bool { _, value, err := mgr.GetConfig("a.b") - assert.NoError(t, err) - return value == "aaa" + return err == nil && value == "aaa" }, time.Second*5, time.Second) ctx := context.Background() @@ -159,29 +158,25 @@ func TestOnEvent(t *testing.T) { assert.Eventually(t, func() bool { _, value, err := mgr.GetConfig("a.b") - assert.NoError(t, err) - return value == "bbb" + return err == nil && value == "bbb" }, time.Second*5, time.Second) client.KV.Put(ctx, "test/config/a/b", "ccc") assert.Eventually(t, func() bool { _, value, err := mgr.GetConfig("a.b") - assert.NoError(t, err) - return value == "ccc" + return err == nil && value == "ccc" }, time.Second*5, time.Second) os.WriteFile(yamlFile, []byte("a.b: ddd"), 0o600) assert.Eventually(t, func() bool { _, value, err := mgr.GetConfig("a.b") - assert.NoError(t, err) - return value == "ccc" + return err == nil && value == "ccc" }, time.Second*5, time.Second) client.KV.Delete(ctx, "test/config/a/b") assert.Eventually(t, func() bool { _, value, err := mgr.GetConfig("a.b") - assert.NoError(t, err) - return value == "ddd" + return err == nil && value == "ddd" }, time.Second*5, time.Second) } @@ -269,9 +264,17 @@ func TestCachedConfig(t *testing.T) { // after refresh, the cached value should be reset os.WriteFile(yamlFile, []byte("a.b: xxx"), 0o600) - time.Sleep(time.Second) - _, exist = mgr.GetCachedValue("a.b") - assert.False(t, exist) + assert.Eventually(t, func() bool { + // make sure the config is refreshed + _, value, err := mgr.GetConfig("a.b") + if err != nil || value != "xxx" { + return false + } + + // make sure the cached value is evicted + _, exist := mgr.GetCachedValue("a.b") + return !exist + }, time.Second*5, 500*time.Millisecond) } client := v3client.New(e.Server) { @@ -282,11 +285,18 @@ func TestCachedConfig(t *testing.T) { assert.True(t, exist) // after refresh, the cached value should be reset - ctx := context.Background() - client.KV.Put(ctx, "test/config/c/d", "www") - time.Sleep(time.Second) - _, exist = mgr.GetCachedValue("cd") - assert.False(t, exist) + client.KV.Put(t.Context(), "test/config/c/d", "www") + assert.Eventually(t, func() bool { + // make sure the config is refreshed + _, value, err := mgr.GetConfig("cd") + if err != nil || value != "www" { + return false + } + + // make sure the cached value is evicted + _, exist := mgr.GetCachedValue("cd") + return !exist + }, time.Second*5, 500*time.Millisecond) } }