1
0
mirror of https://github.com/juanfont/headscale.git synced 2024-12-20 19:09:07 +01:00

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
This commit is contained in:
nadongjun 2024-07-23 00:11:57 +09:00 committed by GitHub
parent db7a4358e9
commit 4ad3f3c484
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -600,8 +600,11 @@ func (s *Suite) TestAutoApproveRoutes(c *check.C) {
func TestEphemeralGarbageCollectorOrder(t *testing.T) { func TestEphemeralGarbageCollectorOrder(t *testing.T) {
want := []types.NodeID{1, 3} want := []types.NodeID{1, 3}
got := []types.NodeID{} got := []types.NodeID{}
var mu sync.Mutex
e := NewEphemeralGarbageCollector(func(ni types.NodeID) { e := NewEphemeralGarbageCollector(func(ni types.NodeID) {
mu.Lock()
defer mu.Unlock()
got = append(got, ni) got = append(got, ni)
}) })
go e.Start() go e.Start()
@ -617,6 +620,9 @@ func TestEphemeralGarbageCollectorOrder(t *testing.T) {
e.Close() e.Close()
mu.Lock()
defer mu.Unlock()
if diff := cmp.Diff(want, got); diff != "" { if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("wrong nodes deleted, unexpected result (-want +got):\n%s", diff) t.Errorf("wrong nodes deleted, unexpected result (-want +got):\n%s", diff)
} }
@ -629,8 +635,8 @@ func TestEphemeralGarbageCollectorLoads(t *testing.T) {
want := 1000 want := 1000
e := NewEphemeralGarbageCollector(func(ni types.NodeID) { e := NewEphemeralGarbageCollector(func(ni types.NodeID) {
defer mu.Unlock()
mu.Lock() mu.Lock()
defer mu.Unlock()
time.Sleep(time.Duration(generateRandomNumber(t, 3)) * time.Millisecond) time.Sleep(time.Duration(generateRandomNumber(t, 3)) * time.Millisecond)
got = append(got, ni) got = append(got, ni)
@ -644,6 +650,10 @@ func TestEphemeralGarbageCollectorLoads(t *testing.T) {
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
e.Close() e.Close()
mu.Lock()
defer mu.Unlock()
if len(got) != want { if len(got) != want {
t.Errorf("expected %d, got %d", want, len(got)) t.Errorf("expected %d, got %d", want, len(got))
} }