From 4ad3f3c484c060b4bfb5d8762fa12a4e4529a672 Mon Sep 17 00:00:00 2001 From: nadongjun Date: Tue, 23 Jul 2024 00:11:57 +0900 Subject: [PATCH] Fix data race issues in EphemeralGarbageCollector tests (#2023) * Fix data race issues in EphemeralGarbageCollector tests * Add defer for mutex unlock in TestEphemeralGarbageCollectorOrder * Fix mutex unlock order in closure by updating defer placement --- hscontrol/db/node_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hscontrol/db/node_test.go b/hscontrol/db/node_test.go index 065e70b..ad94f06 100644 --- a/hscontrol/db/node_test.go +++ b/hscontrol/db/node_test.go @@ -600,8 +600,11 @@ func (s *Suite) TestAutoApproveRoutes(c *check.C) { func TestEphemeralGarbageCollectorOrder(t *testing.T) { want := []types.NodeID{1, 3} got := []types.NodeID{} + var mu sync.Mutex e := NewEphemeralGarbageCollector(func(ni types.NodeID) { + mu.Lock() + defer mu.Unlock() got = append(got, ni) }) go e.Start() @@ -617,6 +620,9 @@ func TestEphemeralGarbageCollectorOrder(t *testing.T) { e.Close() + mu.Lock() + defer mu.Unlock() + if diff := cmp.Diff(want, got); diff != "" { t.Errorf("wrong nodes deleted, unexpected result (-want +got):\n%s", diff) } @@ -629,8 +635,8 @@ func TestEphemeralGarbageCollectorLoads(t *testing.T) { want := 1000 e := NewEphemeralGarbageCollector(func(ni types.NodeID) { - defer mu.Unlock() mu.Lock() + defer mu.Unlock() time.Sleep(time.Duration(generateRandomNumber(t, 3)) * time.Millisecond) got = append(got, ni) @@ -644,6 +650,10 @@ func TestEphemeralGarbageCollectorLoads(t *testing.T) { time.Sleep(10 * time.Second) e.Close() + + mu.Lock() + defer mu.Unlock() + if len(got) != want { t.Errorf("expected %d, got %d", want, len(got)) }