From 9a76c648f1225c6349b345ee096dd4278ad47e4e Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 4 Nov 2020 18:20:25 +0200 Subject: [PATCH] unit test for head change coalescer --- chain/store/coalescer_test.go | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 chain/store/coalescer_test.go diff --git a/chain/store/coalescer_test.go b/chain/store/coalescer_test.go new file mode 100644 index 000000000..cb99690c5 --- /dev/null +++ b/chain/store/coalescer_test.go @@ -0,0 +1,68 @@ +package store + +import ( + "testing" + "time" + + "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/lotus/chain/types/mock" +) + +func TestHeadChangeCoalescer(t *testing.T) { + notif := make(chan headChange, 1) + c := NewHeadChangeCoalescer(func(revert, apply []*types.TipSet) error { + notif <- headChange{apply: apply, revert: revert} + return nil + }, 100*time.Millisecond) + defer c.Close() + + b0 := mock.MkBlock(nil, 0, 0) + root := mock.TipSet(b0) + bA := mock.MkBlock(root, 1, 1) + tA := mock.TipSet(bA) + bB := mock.MkBlock(root, 1, 2) + tB := mock.TipSet(bB) + tAB := mock.TipSet(bA, bB) + bC := mock.MkBlock(root, 1, 3) + tABC := mock.TipSet(bA, bB, bC) + bD := mock.MkBlock(root, 1, 4) + tABCD := mock.TipSet(bA, bB, bC, bD) + bE := mock.MkBlock(root, 1, 5) + tABCDE := mock.TipSet(bA, bB, bC, bD, bE) + + c.HeadChange(nil, []*types.TipSet{tA}) + c.HeadChange(nil, []*types.TipSet{tB}) + c.HeadChange([]*types.TipSet{tA, tB}, []*types.TipSet{tAB}) + c.HeadChange([]*types.TipSet{tAB}, []*types.TipSet{tABC}) + + change := <-notif + + if len(change.revert) != 0 { + t.Fatalf("expected empty revert set but got %d elements", len(change.revert)) + } + if len(change.apply) != 1 { + t.Fatalf("expected single element apply set but got %d elements", len(change.apply)) + } + if change.apply[0] != tABC { + t.Fatalf("expected to apply tABC") + } + + c.HeadChange([]*types.TipSet{tABC}, []*types.TipSet{tABCD}) + c.HeadChange([]*types.TipSet{tABCD}, []*types.TipSet{tABCDE}) + + change = <-notif + + if len(change.revert) != 1 { + t.Fatalf("expected single element revert set but got %d elements", len(change.revert)) + } + if change.revert[0] != tABC { + t.Fatalf("expected to revert tABC") + } + if len(change.apply) != 1 { + t.Fatalf("expected single element apply set but got %d elements", len(change.apply)) + } + if change.apply[0] != tABCDE { + t.Fatalf("expected to revert tABC") + } + +}