Skip to content

Commit

Permalink
unit test for head change coalescer
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Nov 4, 2020
1 parent 0194dcd commit af682a3
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions chain/store/coalescer_test.go
Original file line number Diff line number Diff line change
@@ -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")
}

}

0 comments on commit af682a3

Please sign in to comment.