forked from cerc-io/plugeth
blockpool: wrap intermittent status test in a loop
This commit is contained in:
parent
c4ea921876
commit
859f1f08ca
@ -1,7 +1,7 @@
|
|||||||
package blockpool
|
package blockpool
|
||||||
|
|
||||||
import (
|
import (
|
||||||
// "fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -45,17 +45,15 @@ func getStatusValues(s *Status) []int {
|
|||||||
func checkStatus(t *testing.T, bp *BlockPool, syncing bool, expected []int) (err error) {
|
func checkStatus(t *testing.T, bp *BlockPool, syncing bool, expected []int) (err error) {
|
||||||
s := bp.Status()
|
s := bp.Status()
|
||||||
if s.Syncing != syncing {
|
if s.Syncing != syncing {
|
||||||
t.Errorf("status for Syncing incorrect. expected %v, got %v", syncing, s.Syncing)
|
err = fmt.Errorf("status for Syncing incorrect. expected %v, got %v", syncing, s.Syncing)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
got := getStatusValues(s)
|
got := getStatusValues(s)
|
||||||
for i, v := range expected {
|
for i, v := range expected {
|
||||||
if i == 0 || i == 7 {
|
|
||||||
continue //hack
|
|
||||||
}
|
|
||||||
err = test.CheckInt(statusFields[i], got[i], v, t)
|
err = test.CheckInt(statusFields[i], got[i], v, t)
|
||||||
// fmt.Printf("%v: %v (%v)\n", statusFields[i], got[i], v)
|
// fmt.Printf("%v: %v (%v)\n", statusFields[i], got[i], v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@ -63,6 +61,25 @@ func checkStatus(t *testing.T, bp *BlockPool, syncing bool, expected []int) (err
|
|||||||
|
|
||||||
func TestBlockPoolStatus(t *testing.T) {
|
func TestBlockPoolStatus(t *testing.T) {
|
||||||
test.LogInit()
|
test.LogInit()
|
||||||
|
var err error
|
||||||
|
n := 3
|
||||||
|
for n > 0 {
|
||||||
|
n--
|
||||||
|
err = testBlockPoolStatus(t)
|
||||||
|
if err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("no pass out of 3: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testBlockPoolStatus(t *testing.T) (err error) {
|
||||||
|
|
||||||
_, blockPool, blockPoolTester := newTestBlockPool(t)
|
_, blockPool, blockPoolTester := newTestBlockPool(t)
|
||||||
blockPoolTester.blockChain[0] = nil
|
blockPoolTester.blockChain[0] = nil
|
||||||
blockPoolTester.initRefBlockChain(12)
|
blockPoolTester.initRefBlockChain(12)
|
||||||
@ -70,6 +87,7 @@ func TestBlockPoolStatus(t *testing.T) {
|
|||||||
delete(blockPoolTester.refBlockChain, 6)
|
delete(blockPoolTester.refBlockChain, 6)
|
||||||
|
|
||||||
blockPool.Start()
|
blockPool.Start()
|
||||||
|
defer blockPool.Stop()
|
||||||
blockPoolTester.tds = make(map[int]int)
|
blockPoolTester.tds = make(map[int]int)
|
||||||
blockPoolTester.tds[9] = 1
|
blockPoolTester.tds[9] = 1
|
||||||
blockPoolTester.tds[11] = 3
|
blockPoolTester.tds[11] = 3
|
||||||
@ -79,73 +97,67 @@ func TestBlockPoolStatus(t *testing.T) {
|
|||||||
peer2 := blockPoolTester.newPeer("peer2", 2, 6)
|
peer2 := blockPoolTester.newPeer("peer2", 2, 6)
|
||||||
peer3 := blockPoolTester.newPeer("peer3", 3, 11)
|
peer3 := blockPoolTester.newPeer("peer3", 3, 11)
|
||||||
peer4 := blockPoolTester.newPeer("peer4", 1, 9)
|
peer4 := blockPoolTester.newPeer("peer4", 1, 9)
|
||||||
// peer1 := blockPoolTester.newPeer("peer1", 1, 9)
|
|
||||||
// peer2 := blockPoolTester.newPeer("peer2", 2, 6)
|
|
||||||
// peer3 := blockPoolTester.newPeer("peer3", 3, 11)
|
|
||||||
// peer4 := blockPoolTester.newPeer("peer4", 1, 9)
|
|
||||||
peer2.blocksRequestsMap = peer1.blocksRequestsMap
|
peer2.blocksRequestsMap = peer1.blocksRequestsMap
|
||||||
|
|
||||||
var expected []int
|
var expected []int
|
||||||
var err error
|
|
||||||
expected = []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
expected = []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
||||||
err = checkStatus(t, blockPool, false, expected)
|
err = checkStatus(nil, blockPool, false, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer1.AddPeer()
|
peer1.AddPeer()
|
||||||
expected = []int{0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0}
|
expected = []int{0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer1.serveBlocks(8, 9)
|
peer1.serveBlocks(8, 9)
|
||||||
expected = []int{0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
expected = []int{1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
||||||
// err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer1.serveBlockHashes(9, 8, 7, 3, 2)
|
peer1.serveBlockHashes(9, 8, 7, 3, 2)
|
||||||
expected = []int{6, 5, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
expected = []int{6, 5, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
||||||
// expected = []int{5, 5, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer1.serveBlocks(3, 7, 8)
|
peer1.serveBlocks(3, 7, 8)
|
||||||
expected = []int{6, 5, 3, 3, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
expected = []int{6, 5, 3, 3, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer1.serveBlocks(2, 3)
|
peer1.serveBlocks(2, 3)
|
||||||
expected = []int{6, 5, 4, 4, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
expected = []int{6, 5, 4, 4, 0, 1, 0, 0, 1, 1, 1, 1, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer4.AddPeer()
|
peer4.AddPeer()
|
||||||
expected = []int{6, 5, 4, 4, 0, 2, 0, 0, 2, 2, 1, 1, 0}
|
expected = []int{6, 5, 4, 4, 0, 2, 0, 0, 2, 2, 1, 1, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer4.sendBlockHashes(12, 11)
|
peer4.sendBlockHashes(12, 11)
|
||||||
expected = []int{6, 5, 4, 4, 0, 2, 0, 0, 2, 2, 1, 1, 0}
|
expected = []int{6, 5, 4, 4, 0, 2, 0, 0, 2, 2, 1, 1, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer2.AddPeer()
|
peer2.AddPeer()
|
||||||
expected = []int{6, 5, 4, 4, 0, 3, 0, 0, 3, 3, 1, 2, 0}
|
expected = []int{6, 5, 4, 4, 0, 3, 0, 0, 3, 3, 1, 2, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -153,76 +165,76 @@ func TestBlockPoolStatus(t *testing.T) {
|
|||||||
peer2.serveBlocks(5, 6)
|
peer2.serveBlocks(5, 6)
|
||||||
peer2.serveBlockHashes(6, 5, 4, 3, 2)
|
peer2.serveBlockHashes(6, 5, 4, 3, 2)
|
||||||
expected = []int{10, 8, 5, 5, 0, 3, 1, 0, 3, 3, 2, 2, 0}
|
expected = []int{10, 8, 5, 5, 0, 3, 1, 0, 3, 3, 2, 2, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer2.serveBlocks(2, 3, 4)
|
peer2.serveBlocks(2, 3, 4)
|
||||||
expected = []int{10, 8, 6, 6, 0, 3, 1, 0, 3, 3, 2, 2, 0}
|
expected = []int{10, 8, 6, 6, 0, 3, 1, 0, 3, 3, 2, 2, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
blockPool.RemovePeer("peer2")
|
blockPool.RemovePeer("peer2")
|
||||||
expected = []int{10, 8, 6, 6, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
expected = []int{10, 8, 6, 6, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer1.serveBlockHashes(2, 1, 0)
|
peer1.serveBlockHashes(2, 1, 0)
|
||||||
expected = []int{11, 9, 6, 6, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
expected = []int{11, 9, 6, 6, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer1.serveBlocks(1, 2)
|
peer1.serveBlocks(1, 2)
|
||||||
expected = []int{11, 9, 7, 7, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
expected = []int{11, 9, 7, 7, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer1.serveBlocks(4, 5)
|
peer1.serveBlocks(4, 5)
|
||||||
expected = []int{11, 9, 8, 8, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
expected = []int{11, 9, 8, 8, 0, 3, 1, 0, 3, 2, 2, 2, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer3.AddPeer()
|
peer3.AddPeer()
|
||||||
expected = []int{11, 9, 8, 8, 0, 4, 1, 0, 4, 3, 2, 3, 0}
|
expected = []int{11, 9, 8, 8, 0, 4, 1, 0, 4, 3, 2, 3, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer3.serveBlocks(10, 11)
|
peer3.serveBlocks(10, 11)
|
||||||
expected = []int{12, 9, 9, 9, 0, 4, 1, 0, 4, 3, 3, 3, 0}
|
expected = []int{12, 9, 9, 9, 0, 4, 1, 0, 4, 3, 3, 3, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer3.serveBlockHashes(11, 10, 9)
|
peer3.serveBlockHashes(11, 10, 9)
|
||||||
expected = []int{14, 11, 9, 9, 0, 4, 1, 0, 4, 3, 3, 3, 0}
|
expected = []int{14, 11, 9, 9, 0, 4, 1, 0, 4, 3, 3, 3, 0}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
peer4.sendBlocks(11, 12)
|
peer4.sendBlocks(11, 12)
|
||||||
expected = []int{14, 11, 9, 9, 0, 4, 1, 0, 4, 3, 4, 3, 1}
|
expected = []int{14, 11, 9, 9, 0, 4, 1, 0, 4, 3, 4, 3, 1}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
peer3.serveBlocks(9, 10)
|
peer3.serveBlocks(9, 10)
|
||||||
expected = []int{14, 11, 10, 10, 0, 4, 1, 0, 4, 3, 4, 3, 1}
|
expected = []int{14, 11, 10, 10, 0, 4, 1, 0, 4, 3, 4, 3, 1}
|
||||||
err = checkStatus(t, blockPool, true, expected)
|
err = checkStatus(nil, blockPool, true, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -231,10 +243,9 @@ func TestBlockPoolStatus(t *testing.T) {
|
|||||||
blockPool.Wait(waitTimeout)
|
blockPool.Wait(waitTimeout)
|
||||||
time.Sleep(200 * time.Millisecond)
|
time.Sleep(200 * time.Millisecond)
|
||||||
expected = []int{14, 3, 11, 3, 8, 4, 1, 8, 4, 3, 4, 3, 1}
|
expected = []int{14, 3, 11, 3, 8, 4, 1, 8, 4, 3, 4, 3, 1}
|
||||||
err = checkStatus(t, blockPool, false, expected)
|
err = checkStatus(nil, blockPool, false, expected)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
blockPool.Stop()
|
|
||||||
}
|
}
|
||||||
|
@ -10,16 +10,20 @@ import (
|
|||||||
|
|
||||||
func CheckInt(name string, got int, expected int, t *testing.T) (err error) {
|
func CheckInt(name string, got int, expected int, t *testing.T) (err error) {
|
||||||
if got != expected {
|
if got != expected {
|
||||||
t.Errorf("status for %v incorrect. expected %v, got %v", name, expected, got)
|
err = fmt.Errorf("status for %v incorrect. expected %v, got %v", name, expected, got)
|
||||||
err = fmt.Errorf("")
|
if t != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckDuration(name string, got time.Duration, expected time.Duration, t *testing.T) (err error) {
|
func CheckDuration(name string, got time.Duration, expected time.Duration, t *testing.T) (err error) {
|
||||||
if got != expected {
|
if got != expected {
|
||||||
t.Errorf("status for %v incorrect. expected %v, got %v", name, expected, got)
|
err = fmt.Errorf("status for %v incorrect. expected %v, got %v", name, expected, got)
|
||||||
err = fmt.Errorf("")
|
if t != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user