From f05568197275d4a6500400e5cc8648b1099473ec Mon Sep 17 00:00:00 2001 From: vyzo Date: Mon, 26 Oct 2020 15:19:40 +0200 Subject: [PATCH] fix test --- chain/sync_manager_test.go | 64 ++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/chain/sync_manager_test.go b/chain/sync_manager_test.go index 74fb77dc3..d58dc5f3d 100644 --- a/chain/sync_manager_test.go +++ b/chain/sync_manager_test.go @@ -92,30 +92,52 @@ func TestSyncManagerEdgeCase(t *testing.T) { runSyncMgrTest(t, "edgeCase", 1, func(t *testing.T, sm *syncManager, stc chan *syncOp) { sm.SetPeerHead(ctx, "peer1", a) - assertGetSyncOp(t, stc, a) sm.SetPeerHead(ctx, "peer1", b1) sm.SetPeerHead(ctx, "peer1", b2) - // b1 and b2 are being processed - b1op := <-stc - b2op := <-stc - if !b1op.ts.Equals(b1) { - b1op, b2op = b2op, b1op + assertGetSyncOp(t, stc, a) + + // b1 and b2 are in queue after a; the sync manager should pick the heaviest one which is b2 + bop := <-stc + if !bop.ts.Equals(b2) { + t.Fatalf("Expected tipset %s to sync, but got %s", b2, bop.ts) } - sm.SetPeerHead(ctx, "peer2", c2) // c2 is put into activeSyncTips at index 0 - sm.SetPeerHead(ctx, "peer2", c1) // c1 is put into activeSyncTips at index 1 - sm.SetPeerHead(ctx, "peer3", b2) // b2 is related to c2 and even though it is actively synced it is put into activeSyncTips index 0 - sm.SetPeerHead(ctx, "peer1", a) // a is related to b2 and is put into activeSyncTips index 0 + sm.SetPeerHead(ctx, "peer2", c2) + sm.SetPeerHead(ctx, "peer2", c1) + sm.SetPeerHead(ctx, "peer3", b2) + sm.SetPeerHead(ctx, "peer1", a) - b1op.done() // b1 completes first, is related to a, so it pops activeSyncTips index 0 - // even though correct one is index 1 + bop.done() - b2op.done() - // b2 completes and is not related to c1, so it leaves activeSyncTips as it is + // get the next sync target; it should be c1 as the heaviest tipset but added last (same weight as c2) + bop = <-stc + if !bop.ts.Equals(c1) { + t.Fatalf("Expected tipset %s to sync, but got %s", c1, bop.ts) + } - waitUntilAllWorkersAreDone(stc) + sm.SetPeerHead(ctx, "peer4", d1) + sm.SetPeerHead(ctx, "peer5", e1) + bop.done() + + // get the last sync target; it should be e1 + var last *types.TipSet + for i := 0; i < 10; { + select { + case bop = <-stc: + bop.done() + if last == nil || bop.ts.Height() > last.Height() { + last = bop.ts + } + default: + i++ + time.Sleep(10 * time.Millisecond) + } + } + if !last.Equals(e1) { + t.Fatalf("Expected tipset %s to sync, but got %s", e1, last) + } if len(sm.state) != 0 { t.Errorf("active syncs expected empty but got: %d", len(sm.state)) @@ -123,18 +145,6 @@ func TestSyncManagerEdgeCase(t *testing.T) { }) } -func waitUntilAllWorkersAreDone(stc chan *syncOp) { - for i := 0; i < 10; { - select { - case so := <-stc: - so.done() - default: - i++ - time.Sleep(10 * time.Millisecond) - } - } -} - func TestSyncManager(t *testing.T) { ctx := context.Background()