tendermint/blockchain/pool_test.go

129 lines
2.7 KiB
Go
Raw Normal View History

package blockchain
2015-03-22 12:46:53 -07:00
import (
"math/rand"
"testing"
2015-03-22 19:20:54 -07:00
"time"
2015-03-22 12:46:53 -07:00
2015-03-30 22:50:27 -07:00
. "github.com/tendermint/tendermint2/common"
"github.com/tendermint/tendermint2/types"
2015-03-22 12:46:53 -07:00
)
type testPeer struct {
id string
height uint
}
func makePeers(numPeers int, minHeight, maxHeight uint) map[string]testPeer {
peers := make(map[string]testPeer, numPeers)
for i := 0; i < numPeers; i++ {
peerId := RandStr(12)
height := minHeight + uint(rand.Intn(int(maxHeight-minHeight)))
peers[peerId] = testPeer{peerId, height}
}
return peers
}
func TestBasic(t *testing.T) {
2015-03-24 11:02:30 -07:00
peers := makePeers(10, 0, 1000)
2015-03-22 12:46:53 -07:00
start := uint(42)
2015-03-22 16:23:24 -07:00
timeoutsCh := make(chan string, 100)
requestsCh := make(chan BlockRequest, 100)
2015-03-24 11:02:30 -07:00
pool := NewBlockPool(start, requestsCh, timeoutsCh)
2015-03-22 12:46:53 -07:00
pool.Start()
// Introduce each peer.
go func() {
for _, peer := range peers {
2015-03-24 11:02:30 -07:00
pool.SetPeerHeight(peer.id, peer.height)
2015-03-22 12:46:53 -07:00
}
}()
2015-03-24 11:02:30 -07:00
// Start a goroutine to pull blocks
go func() {
for {
if !pool.IsRunning() {
return
}
first, second := pool.PeekTwoBlocks()
if first != nil && second != nil {
pool.PopRequest()
} else {
time.Sleep(1 * time.Second)
}
}
}()
2015-03-22 12:46:53 -07:00
// Pull from channels
for {
select {
case peerId := <-timeoutsCh:
t.Errorf("timeout: %v", peerId)
case request := <-requestsCh:
2015-03-22 16:23:24 -07:00
log.Debug("TEST: Pulled new BlockRequest", "request", request)
2015-03-24 11:02:30 -07:00
if request.Height == 300 {
return // Done!
}
// Request desired, pretend like we got the block immediately.
2015-03-22 12:46:53 -07:00
go func() {
block := &types.Block{Header: &types.Header{Height: request.Height}}
2015-03-22 12:46:53 -07:00
pool.AddBlock(block, request.PeerId)
2015-03-22 16:23:24 -07:00
log.Debug("TEST: Added block", "block", request.Height, "peer", request.PeerId)
2015-03-22 12:46:53 -07:00
}()
}
}
2015-03-22 16:23:24 -07:00
pool.Stop()
}
func TestTimeout(t *testing.T) {
2015-03-24 11:02:30 -07:00
peers := makePeers(10, 0, 1000)
2015-03-22 16:23:24 -07:00
start := uint(42)
2015-03-24 11:02:30 -07:00
timeoutsCh := make(chan string, 100)
requestsCh := make(chan BlockRequest, 100)
pool := NewBlockPool(start, requestsCh, timeoutsCh)
2015-03-22 16:23:24 -07:00
pool.Start()
// Introduce each peer.
go func() {
for _, peer := range peers {
2015-03-24 11:02:30 -07:00
pool.SetPeerHeight(peer.id, peer.height)
}
}()
// Start a goroutine to pull blocks
go func() {
for {
if !pool.IsRunning() {
return
}
first, second := pool.PeekTwoBlocks()
if first != nil && second != nil {
pool.PopRequest()
} else {
time.Sleep(1 * time.Second)
}
2015-03-22 16:23:24 -07:00
}
}()
// Pull from channels
2015-03-24 11:02:30 -07:00
counter := 0
timedOut := map[string]struct{}{}
2015-03-22 16:23:24 -07:00
for {
select {
case peerId := <-timeoutsCh:
2015-03-24 11:02:30 -07:00
log.Debug("Timeout", "peerId", peerId)
if _, ok := timedOut[peerId]; !ok {
counter++
if counter == len(peers) {
return // Done!
}
2015-03-22 16:23:24 -07:00
}
2015-03-24 11:02:30 -07:00
case request := <-requestsCh:
log.Debug("TEST: Pulled new BlockRequest", "request", request)
2015-03-22 16:23:24 -07:00
}
}
2015-03-22 12:46:53 -07:00
pool.Stop()
}