tendermint/blockchain/pool_test.go

134 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
. "github.com/tendermint/go-common"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/types"
2015-03-22 12:46:53 -07:00
)
type testPeer struct {
id string
height int
2015-03-22 12:46:53 -07:00
}
func makePeers(numPeers int, minHeight, maxHeight int) map[string]testPeer {
2015-03-22 12:46:53 -07:00
peers := make(map[string]testPeer, numPeers)
for i := 0; i < numPeers; i++ {
peerID := RandStr(12)
height := minHeight + rand.Intn(maxHeight-minHeight)
peers[peerID] = testPeer{peerID, height}
2015-03-22 12:46:53 -07:00
}
return peers
}
func TestBasic(t *testing.T) {
start := 42
2015-09-09 21:44:48 -07:00
peers := makePeers(10, start+1, 1000)
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)
2015-03-22 12:46:53 -07:00
case request := <-requestsCh:
2015-07-19 14:49:13 -07:00
log.Info("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}}
pool.AddBlock(request.PeerID, block, 123)
log.Info("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) {
start := 42
2015-09-09 21:44:48 -07:00
peers := makePeers(10, start+1, 1000)
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()
2015-09-09 21:44:48 -07:00
for _, peer := range peers {
2015-09-11 08:12:48 -07:00
log.Info("Peer", "id", peer.id)
2015-09-09 21:44:48 -07:00
}
2015-03-22 16:23:24 -07:00
// 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:
log.Info("Timeout", "peerID", peerID)
if _, ok := timedOut[peerID]; !ok {
2015-03-24 11:02:30 -07:00
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:
2015-07-19 14:49:13 -07:00
log.Info("TEST: Pulled new BlockRequest", "request", request)
2015-03-22 16:23:24 -07:00
}
}
2015-03-22 12:46:53 -07:00
pool.Stop()
}