From 26c86ec2213c70b768fddc7ed1eddd6cd79701d8 Mon Sep 17 00:00:00 2001 From: aoiasd <45024769+aoiasd@users.noreply.github.com> Date: Tue, 30 Dec 2025 17:05:21 +0800 Subject: [PATCH] enhance: fix unstable unit test (#46626) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Core invariant: TestWait must deterministically verify that FixedSizeAllocator.Wait() is notified when virtual resources are released, so an allocation blocked due to exhausted virtual capacity eventually succeeds after explicit deallocations. - Removed/simplified logic: replaced the previous flaky pattern that spawned 100 concurrent goroutines performing Reallocate with an explicit channel-synchronized release goroutine that performs 100 sequential negative Reallocate calls only after the test blocks on allocation. This eliminates timing-dependent concurrency and the nondeterministic i-based assertion. - Why no data loss or behavior regression: only the test changed — allocator implementation (Allocate/Reallocate/Release/Wait/notify) and public APIs are unchanged. The test now exercises the same code paths (Allocate fails, Wait blocks on cond, Reallocate/Release call notify), but in a deterministic order, so the allocator semantics and resource accounting (used, allocs map) remain unaffected. - Change type and intent: Enhancement/refactor of unit test stability — it tightens test synchronization to remove race-dependent assertions and ensure the Wait/notify mechanism is reliably exercised without modifying allocator logic. Signed-off-by: aoiasd --- pkg/util/vralloc/alloc_test.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/pkg/util/vralloc/alloc_test.go b/pkg/util/vralloc/alloc_test.go index 6e2e039211..05ae408b83 100644 --- a/pkg/util/vralloc/alloc_test.go +++ b/pkg/util/vralloc/alloc_test.go @@ -96,22 +96,27 @@ func TestWait(t *testing.T) { a := NewFixedSizeAllocator[string](&Resource{100, 100, 100}) allocated, _ := a.Allocate("a1", &Resource{100, 100, 100}) assert.True(t, allocated) - for i := 0; i < 100; i++ { - go func(index int) { + + waitCh := make(chan struct{}) + release := func() { + <-waitCh + for i := 0; i < 100; i++ { allocated, _ := a.Reallocate("a1", &Resource{-1, -1, -1}) assert.Equal(t, true, allocated) - }(i) + } } + go release() + allocated, _ = a.Allocate("a2", &Resource{100, 100, 100}) - i := 1 + assert.False(t, allocated) + close(waitCh) // start release a1 + for !allocated { - a.Wait() + a.Wait() // alloc after wait allocated, _ = a.Allocate("a2", &Resource{100, 100, 100}) - i++ } assert.True(t, allocated) - assert.True(t, i < 100 && i > 1) } func TestPhysicalAwareFixedSizeAllocator(t *testing.T) {