prysm-pulse/sharding/syncer/service_test.go
Raul Jordan ce5fde1f57 sharding: Fix Concurrency Issues, Fix Simulator/Proposer/Syncer Runtime Problems (#276)
Former-commit-id: 2547a60befd85276fb4fb702a4277f3e1dbd9c48 [formerly 9d23a567fbf09e8a19dcbb2ca39b8c8678ce879f]
Former-commit-id: 8c1a555faf070f4070c50dae4913bdd615c66ee7
2018-07-14 15:23:07 -05:00

140 lines
3.8 KiB
Go

package syncer
import (
"fmt"
"math/big"
"testing"
"github.com/prysmaticlabs/geth-sharding/sharding/mainchain"
"github.com/prysmaticlabs/geth-sharding/sharding/params"
"github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/prysmaticlabs/geth-sharding/sharding/p2p/messages"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/prysmaticlabs/geth-sharding/sharding/database"
"github.com/prysmaticlabs/geth-sharding/sharding/p2p"
"github.com/prysmaticlabs/geth-sharding/sharding/types"
)
var _ = types.Service(&Syncer{})
func TestStop(t *testing.T) {
hook := logTest.NewGlobal()
shardChainDB, err := database.NewShardDB("", "", true)
if err != nil {
t.Fatalf("unable to setup db: %v", err)
}
shardID := 0
server, err := p2p.NewServer()
if err != nil {
t.Fatalf("Unable to setup p2p server: %v", err)
}
syncer, err := NewSyncer(params.DefaultConfig, &mainchain.SMCClient{}, server, shardChainDB, shardID)
if err != nil {
t.Fatalf("Unable to setup sync service: %v", err)
}
feed := server.Feed(messages.CollationBodyRequest{})
syncer.msgChan = make(chan p2p.Message)
syncer.bodyRequests = feed.Subscribe(syncer.msgChan)
if err := syncer.Stop(); err != nil {
t.Fatalf("Unable to stop sync service: %v", err)
}
msg := hook.LastEntry().Message
want := "Stopping sync service"
if msg != want {
t.Errorf("incorrect log, expected %s, got %s", want, msg)
}
// The context should have been canceled.
if syncer.ctx.Err() == nil {
t.Error("Context was not canceled")
}
hook.Reset()
}
// This test checks the proper functioning of the handleCollationBodyRequests goroutine
// by listening to the responseSent channel which occurs after successful
// construction and sending of a response via p2p.
func TestHandleCollationBodyRequests(t *testing.T) {
hook := logTest.NewGlobal()
shardChainDB, err := database.NewShardDB("", "", true)
if err != nil {
t.Fatalf("unable to setup db: %v", err)
}
server, err := p2p.NewServer()
if err != nil {
t.Fatalf("Unable to setup p2p server: %v", err)
}
body := []byte{1, 2, 3, 4, 5}
shardID := big.NewInt(0)
chunkRoot := gethTypes.DeriveSha(types.Chunks(body))
period := big.NewInt(0)
proposerAddress := common.BytesToAddress([]byte{})
header := types.NewCollationHeader(shardID, &chunkRoot, period, &proposerAddress, [32]byte{})
// Stores the collation into the inmemory kv store shardChainDB.
collation := types.NewCollation(header, body, nil)
shard := types.NewShard(shardID, shardChainDB.DB())
if err := shard.SaveCollation(collation); err != nil {
t.Fatalf("Could not store collation in shardChainDB: %v", err)
}
syncer, err := NewSyncer(params.DefaultConfig, &mainchain.SMCClient{}, server, shardChainDB, 0)
if err != nil {
t.Fatalf("Unable to setup syncer service: %v", err)
}
feed := server.Feed(messages.CollationBodyRequest{})
syncer.msgChan = make(chan p2p.Message)
syncer.bodyRequests = feed.Subscribe(syncer.msgChan)
doneChan := make(chan struct{})
exitRoutine := make(chan bool)
go func() {
syncer.HandleCollationBodyRequests(shard, doneChan)
<-exitRoutine
}()
msg := p2p.Message{
Peer: p2p.Peer{},
Data: messages.CollationBodyRequest{
ChunkRoot: &chunkRoot,
ShardID: shardID,
Period: period,
Proposer: &proposerAddress,
},
}
syncer.msgChan <- msg
doneChan <- struct{}{}
exitRoutine <- true
logMsg := hook.AllEntries()[0].Message
want := fmt.Sprintf("Received p2p request of type: %T", p2p.Message{})
if logMsg != want {
t.Errorf("incorrect log, expected %s, got %s", want, logMsg)
}
logMsg = hook.AllEntries()[1].Message
want = fmt.Sprintf("Responding to p2p request with collation with headerHash: %v", header.Hash().Hex())
if logMsg != want {
t.Errorf("incorrect log, expected %s, got %s", want, logMsg)
}
hook.Reset()
}