enhance: fix unstable config util unit test (#46702)

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
- 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.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

Signed-off-by: aoiasd <zhicheng.yue@zilliz.com>
This commit is contained in:
aoiasd 2025-12-31 15:59:21 +08:00 committed by GitHub
parent ca8740c7c0
commit d261034af6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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)
}
}