mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-16 00:43:46 +00:00
Test each event without for loop
This commit is contained in:
parent
6ab7515849
commit
06c9d5cca6
1 changed files with 36 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -30,6 +31,9 @@ func TestChain2HeadEvent(t *testing.T) {
|
||||||
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
|
blockchain, _ := NewBlockChain(db, nil, gspec.Config, ethash.NewFaker(), vm.Config{}, nil, nil)
|
||||||
defer blockchain.Stop()
|
defer blockchain.Stop()
|
||||||
|
|
||||||
|
chain2HeadCh := make(chan Chain2HeadEvent, 64)
|
||||||
|
blockchain.SubscribeChain2HeadEvent(chain2HeadCh)
|
||||||
|
|
||||||
chain, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 3, func(i int, gen *BlockGen) {})
|
chain, _ := GenerateChain(gspec.Config, genesis, ethash.NewFaker(), db, 3, func(i int, gen *BlockGen) {})
|
||||||
if _, err := blockchain.InsertChain(chain); err != nil {
|
if _, err := blockchain.InsertChain(chain); err != nil {
|
||||||
t.Fatalf("failed to insert chain: %v", err)
|
t.Fatalf("failed to insert chain: %v", err)
|
||||||
|
|
@ -45,12 +49,42 @@ func TestChain2HeadEvent(t *testing.T) {
|
||||||
}
|
}
|
||||||
gen.AddTx(tx)
|
gen.AddTx(tx)
|
||||||
})
|
})
|
||||||
chain2HeadCh := make(chan Chain2HeadEvent, 64)
|
|
||||||
blockchain.SubscribeChain2HeadEvent(chain2HeadCh)
|
|
||||||
if _, err := blockchain.InsertChain(replacementBlocks); err != nil {
|
if _, err := blockchain.InsertChain(replacementBlocks); err != nil {
|
||||||
t.Fatalf("failed to insert chain: %v", err)
|
t.Fatalf("failed to insert chain: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
readEvent := func() *Chain2HeadEvent {
|
||||||
|
select {
|
||||||
|
case evnt := <-chain2HeadCh:
|
||||||
|
return &evnt
|
||||||
|
case <-time.After(2 * time.Second):
|
||||||
|
t.Fatal("timeout")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// head event
|
||||||
|
evnt := readEvent()
|
||||||
|
fmt.Println(evnt.Type)
|
||||||
|
|
||||||
|
// fork event
|
||||||
|
evnt = readEvent()
|
||||||
|
fmt.Println(evnt.Type)
|
||||||
|
|
||||||
|
// fork event
|
||||||
|
evnt = readEvent()
|
||||||
|
fmt.Println(evnt.Type)
|
||||||
|
|
||||||
|
// reorg event
|
||||||
|
evnt = readEvent()
|
||||||
|
fmt.Println(evnt.Type)
|
||||||
|
|
||||||
|
// head event
|
||||||
|
evnt = readEvent()
|
||||||
|
fmt.Println(evnt.Type)
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
// first two block of the secondary chain are for a brief moment considered
|
// first two block of the secondary chain are for a brief moment considered
|
||||||
// side chains because up to that point the first one is considered the
|
// side chains because up to that point the first one is considered the
|
||||||
// heavier chain.
|
// heavier chain.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue