Skip to content

Commit

Permalink
Fix data race issues in EphemeralGarbageCollector tests (#2023)
Browse files Browse the repository at this point in the history
* Fix data race issues in EphemeralGarbageCollector tests

* Add defer for mutex unlock in TestEphemeralGarbageCollectorOrder

* Fix mutex unlock order in closure by updating defer placement
  • Loading branch information
nadongjun authored Jul 22, 2024
1 parent db7a435 commit 4ad3f3c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion hscontrol/db/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
}
Expand All @@ -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)
Expand All @@ -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))
}
Expand Down

0 comments on commit 4ad3f3c

Please sign in to comment.