From 1373cb04a9d77471778cc2e3130c43642c32b021 Mon Sep 17 00:00:00 2001 From: georgehao Date: Sat, 22 Oct 2022 23:38:23 +0800 Subject: [PATCH] feat: fix fanout test data race --- pkg/util/fanout/fanout_test.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pkg/util/fanout/fanout_test.go b/pkg/util/fanout/fanout_test.go index 040358e06..322f8c5ad 100644 --- a/pkg/util/fanout/fanout_test.go +++ b/pkg/util/fanout/fanout_test.go @@ -19,6 +19,7 @@ package fanout import ( "context" + "sync" "testing" "time" ) @@ -26,12 +27,19 @@ import ( func TestFanout_Do(t *testing.T) { ca := New("cache", WithWorker(1), WithBuffer(1024)) var run bool + var mtx sync.Mutex + ca.Do(context.Background(), func(c context.Context) { + mtx.Lock() run = true + mtx.Unlock() panic("error") }) + time.Sleep(time.Millisecond * 50) t.Log("not panic") + mtx.Lock() + defer mtx.Unlock() if !run { t.Fatal("expect run be true") }