From b45409262fdb620b787919a34602a54d272d4a6a Mon Sep 17 00:00:00 2001 From: Aayush Date: Thu, 4 May 2023 12:04:28 -0400 Subject: [PATCH] feat: deflake msgindex_test.go --- chain/index/msgindex_test.go | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/chain/index/msgindex_test.go b/chain/index/msgindex_test.go index 4ebdcfd35..bf4bc6190 100644 --- a/chain/index/msgindex_test.go +++ b/chain/index/msgindex_test.go @@ -39,10 +39,10 @@ func TestBasicMsgIndex(t *testing.T) { t.Logf("advance to epoch %d", i+1) err := cs.advance() require.NoError(t, err) - // wait for the coalescer to notify - time.Sleep(CoalesceMinDelay + 10*time.Millisecond) } + waitForCoalescerAfterLastEvent() + t.Log("verifying index") verifyIndex(t, cs, msgIndex) } @@ -51,7 +51,7 @@ func TestReorgMsgIndex(t *testing.T) { // slightly more nuanced test that includes reorgs // 1. Create an index with mock chain store // 2. Advance/Reorg the chain for a few tipsets - // 3. Verify that the index contains all messages with the correct tipst/epoch + // 3. Verify that the index contains all messages with the correct tipset/epoch cs := newMockChainStore() cs.genesis() @@ -67,10 +67,10 @@ func TestReorgMsgIndex(t *testing.T) { t.Logf("advance to epoch %d", i+1) err := cs.advance() require.NoError(t, err) - // wait for the coalescer to notify - time.Sleep(CoalesceMinDelay + 10*time.Millisecond) } + waitForCoalescerAfterLastEvent() + // a simple reorg t.Log("doing reorg") reorgme := cs.curTs @@ -80,7 +80,8 @@ func TestReorgMsgIndex(t *testing.T) { reorgmeChild := cs.makeBlk() err = cs.reorg([]*types.TipSet{reorgme}, []*types.TipSet{reorgmeChild}) require.NoError(t, err) - time.Sleep(CoalesceMinDelay + 10*time.Millisecond) + + waitForCoalescerAfterLastEvent() t.Log("verifying index") verifyIndex(t, cs, msgIndex) @@ -109,10 +110,10 @@ func TestReconcileMsgIndex(t *testing.T) { t.Logf("advance to epoch %d", i+1) err := cs.advance() require.NoError(t, err) - // wait for the coalescer to notify - time.Sleep(CoalesceMinDelay + 10*time.Millisecond) } + waitForCoalescerAfterLastEvent() + // Close it and reorg err = msgIndex.Close() require.NoError(t, err) @@ -296,3 +297,11 @@ func (cs *mockChainStore) GetTipSetFromKey(ctx context.Context, tsk types.TipSet } return ts, nil } + +func waitForCoalescerAfterLastEvent() { + // It can take up to CoalesceMinDelay for the coalescer timer to fire after the last event. + // When the timer fires, it can wait up to CoalesceMinDelay again for more events. + // Therefore the total wait is 2 * CoalesceMinDelay. + // Then we wait another second for the listener (the index) to actually process events. + time.Sleep(2*CoalesceMinDelay + time.Second) +}