From 1786a2a82636054a2b049857ef8b011c7e539fb6 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Fri, 2 Aug 2024 17:46:52 +0200 Subject: vm/dispatcher: add TestPoolStress() The test should aid the Go race detector to detect bugs in the dispatcher.Pool code. --- vm/dispatcher/pool_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'vm/dispatcher') diff --git a/vm/dispatcher/pool_test.go b/vm/dispatcher/pool_test.go index d27e83205..f48534e56 100644 --- a/vm/dispatcher/pool_test.go +++ b/vm/dispatcher/pool_test.go @@ -124,6 +124,33 @@ func TestPoolSplit(t *testing.T) { <-done } +func TestPoolStress(t *testing.T) { + // The test to aid the race detector. + mgr := NewPool[*nilInstance]( + 10, + func(idx int) (*nilInstance, error) { + return &nilInstance{}, nil + }, + func(ctx context.Context, _ *nilInstance, _ UpdateInfo) { + <-ctx.Done() + }, + ) + done := make(chan bool) + ctx, cancel := context.WithCancel(context.Background()) + go func() { + mgr.Loop(ctx) + close(done) + }() + for i := 0; i < 128; i++ { + go mgr.Run(func(ctx context.Context, _ *nilInstance, _ UpdateInfo) { + }) + mgr.ReserveForRun(5 + i%5) + } + + cancel() + <-done +} + func makePool(count int) []testInstance { var ret []testInstance for i := 0; i < count; i++ { @@ -169,3 +196,10 @@ func (ti *testInstance) Index() int { func (ti *testInstance) Close() error { return nil } + +type nilInstance struct { +} + +func (ni *nilInstance) Close() error { + return nil +} -- cgit mrf-deployment