2018-06-06 02:38:16 +00:00
|
|
|
package observer
|
|
|
|
|
2018-06-20 03:59:02 +00:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2018-07-20 21:31:26 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/client/mainchain"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/syncer"
|
|
|
|
"github.com/prysmaticlabs/prysm/client/types"
|
2018-07-30 06:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/database"
|
2018-07-24 15:21:58 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/p2p"
|
2018-07-10 02:27:23 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2018-06-20 03:59:02 +00:00
|
|
|
)
|
2018-06-06 02:38:16 +00:00
|
|
|
|
|
|
|
// Verifies that Observer implements the Actor interface.
|
2018-07-09 02:40:34 +00:00
|
|
|
var _ = types.Actor(&Observer{})
|
2018-06-20 03:59:02 +00:00
|
|
|
|
|
|
|
func TestStartStop(t *testing.T) {
|
2018-07-10 02:27:23 +00:00
|
|
|
|
|
|
|
hook := logTest.NewGlobal()
|
2018-06-20 03:59:02 +00:00
|
|
|
|
|
|
|
server, err := p2p.NewServer()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to setup p2p server: %v", err)
|
|
|
|
}
|
2018-07-30 06:14:50 +00:00
|
|
|
config := &database.DBConfig{Name: "", DataDir: "", InMemory: true}
|
|
|
|
shardChainDB, err := database.NewDB(config)
|
2018-06-29 00:56:51 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to setup db: %v", err)
|
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
shardID := 0
|
2018-07-07 17:23:19 +00:00
|
|
|
client := &mainchain.SMCClient{}
|
2018-06-20 03:59:02 +00:00
|
|
|
|
2018-07-27 00:29:28 +00:00
|
|
|
syncer, err := syncer.NewSyncer(params.DefaultConfig(), client, server, shardChainDB, shardID)
|
2018-07-07 17:23:19 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to setup sync service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
observer, err := NewObserver(server, shardChainDB, shardID, syncer, client)
|
2018-06-20 03:59:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to set up observer service: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-07 17:23:19 +00:00
|
|
|
observer.sync.Start()
|
2018-07-10 02:27:23 +00:00
|
|
|
msg := hook.LastEntry().Message
|
|
|
|
if msg != "Starting sync service" {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", "Starting sync service", msg)
|
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
|
2018-07-07 17:23:19 +00:00
|
|
|
observer.Start()
|
2018-07-10 02:27:23 +00:00
|
|
|
msg = hook.LastEntry().Message
|
|
|
|
if msg != "Starting observer service" {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", "Starting observer service", msg)
|
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
|
|
|
|
err = observer.Stop()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Unable to stop observer service: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-07-10 02:27:23 +00:00
|
|
|
msg = hook.LastEntry().Message
|
|
|
|
if msg != "Stopping observer service" {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", "Stopping observer service", msg)
|
|
|
|
}
|
2018-06-20 03:59:02 +00:00
|
|
|
|
|
|
|
if observer.ctx.Err() == nil {
|
|
|
|
t.Errorf("Context was not cancelled")
|
|
|
|
}
|
|
|
|
}
|