From bf3c87c8649ce6f6e4797c85346f5123fb635e9c Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Wed, 8 Jul 2020 15:33:50 +0200 Subject: [PATCH] test: deflake TestAddAndRemoveListenerConcurrency and TestSyncer_SyncAny (#5101) Fixes #5094. --- libs/events/events_test.go | 9 +++++---- statesync/syncer_test.go | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/libs/events/events_test.go b/libs/events/events_test.go index c7abfc01d..5ca289497 100644 --- a/libs/events/events_test.go +++ b/libs/events/events_test.go @@ -193,24 +193,25 @@ func TestAddAndRemoveListenerConcurrency(t *testing.T) { // Must be executed concurrently to uncover the data race. // 1. RemoveListener go func() { + defer close(done1) for i := 0; i < roundCount; i++ { evsw.RemoveListener("listener") } - close(done1) }() // 2. AddListenerForEvent go func() { + defer close(done2) for i := 0; i < roundCount; i++ { index := i - err = evsw.AddListenerForEvent("listener", fmt.Sprintf("event%d", index), + // we explicitly ignore errors here, since the listener will sometimes be removed + // (that's what we're testing) + _ = evsw.AddListenerForEvent("listener", fmt.Sprintf("event%d", index), func(data EventData) { t.Errorf("should not run callback for %d.\n", index) stopInputEvent = true }) - require.NoError(t, err) } - close(done2) }() <-done1 diff --git a/statesync/syncer_test.go b/statesync/syncer_test.go index 5012ddbec..e9043141c 100644 --- a/statesync/syncer_test.go +++ b/statesync/syncer_test.go @@ -189,6 +189,8 @@ func TestSyncer_SyncAny(t *testing.T) { newState, lastCommit, err := syncer.SyncAny(0) require.NoError(t, err) + time.Sleep(50 * time.Millisecond) // wait for peers to receive requests + chunkRequestsMtx.Lock() assert.Equal(t, map[uint32]int{0: 1, 1: 2, 2: 1}, chunkRequests) chunkRequestsMtx.Unlock()