Streamlined code

This commit is contained in:
Shivam Sharma 2022-01-21 09:49:01 +00:00
parent 8f7174fb92
commit 2d4ef67849

View file

@ -53,71 +53,78 @@ func TestChain2HeadEvent(t *testing.T) {
t.Fatalf("failed to insert chain: %v", err) t.Fatalf("failed to insert chain: %v", err)
} }
type eventTest struct {
Type string
Added []common.Hash
Removed []common.Hash
}
// To use in fatal log, indicates the readEvent which failed the test.
i := 0 i := 0
readEvent := func() *Chain2HeadEvent {
readEvent := func(expect *eventTest) {
select { select {
case ev := <-chain2HeadCh: case ev := <-chain2HeadCh:
i++ i++
return &ev if ev.Type != expect.Type {
t.Fatalf("%d : type mismatch", i)
}
for j := 0; j < len(ev.OldChain); j++ {
if ev.OldChain[j].Hash() != expect.Removed[j] {
t.Fatalf("%d : Oldchain hashes Does Not Match", i)
}
}
for j := 0; j < len(ev.NewChain); j++ {
if ev.NewChain[j].Hash() != expect.Added[j] {
t.Fatalf("%d : Newchain hashes Does Not Match", i)
}
}
case <-time.After(2 * time.Second): case <-time.After(2 * time.Second):
t.Fatal("timeout") t.Fatal("timeout")
} }
return nil
} }
// head event // head event
event1 := readEvent() readEvent(&eventTest{
if event1.Type != Chain2HeadCanonicalEvent { Type: Chain2HeadCanonicalEvent,
t.Fatal("it should be type head") Added: []common.Hash{
} chain[2].Hash(),
if event1.NewChain[0].Hash() != chain[2].Hash() { }})
t.Fatalf("%d : Hash Does Not Match", i)
}
// fork event // fork event
event2 := readEvent() readEvent(&eventTest{
if event2.Type != Chain2HeadForkEvent { Type: Chain2HeadForkEvent,
t.Fatal("it should be type fork") Added: []common.Hash{
} replacementBlocks[0].Hash(),
if event2.NewChain[0].Hash() != replacementBlocks[0].Hash() { }})
t.Fatalf("%d : Hash Does Not Match", i)
}
// fork event // fork event
event3 := readEvent() readEvent(&eventTest{
if event3.Type != Chain2HeadForkEvent { Type: Chain2HeadForkEvent,
t.Fatal("it should be type fork") Added: []common.Hash{
} replacementBlocks[1].Hash(),
if event3.NewChain[0].Hash() != replacementBlocks[1].Hash() { }})
t.Fatalf("%d : Hash Does Not Match", i)
}
// reorg event // reorg event
//In this event the channel recieves an array of Blocks in NewChain and OldChain //In this event the channel recieves an array of Blocks in NewChain and OldChain
expectedOldChainHashes := [3]common.Hash{0: chain[2].Hash(), 1: chain[1].Hash(), 2: chain[0].Hash()} readEvent(&eventTest{
expectedNewChainHashes := [3]common.Hash{0: replacementBlocks[2].Hash(), 1: replacementBlocks[1].Hash(), 2: replacementBlocks[0].Hash()} Type: Chain2HeadReorgEvent,
Added: []common.Hash{
event4 := readEvent() replacementBlocks[2].Hash(),
if event4.Type != Chain2HeadReorgEvent { replacementBlocks[1].Hash(),
t.Fatal("it should be type reorg") replacementBlocks[0].Hash(),
} },
for j := 0; j < len(event4.OldChain); j++ { Removed: []common.Hash{
if event4.OldChain[j].Hash() != expectedOldChainHashes[j] { chain[2].Hash(),
t.Fatalf("%d : Oldchain hashes Does Not Match", i) chain[1].Hash(),
} chain[0].Hash(),
} },
for j := 0; j < len(event4.NewChain); j++ { })
if event4.NewChain[j].Hash() != expectedNewChainHashes[j] {
t.Fatalf("%d : Newchain hashes Does Not Match", i)
}
}
// head event // head event
event5 := readEvent() readEvent(&eventTest{
if event5.Type != Chain2HeadCanonicalEvent { Type: Chain2HeadCanonicalEvent,
t.Fatal("it should be type head") Added: []common.Hash{
} replacementBlocks[3].Hash(),
if event5.NewChain[0].Hash() != replacementBlocks[3].Hash() { }})
t.Fatalf("%d : Hash Does Not Match", i)
}
} }