2018-07-19 16:31:50 +00:00
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2018-07-22 16:58:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/powchain"
|
2018-07-30 06:14:50 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/database"
|
2018-07-19 16:31:50 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStartStop(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
ctx := context.Background()
|
|
|
|
tmp := fmt.Sprintf("%s/beacontest", os.TempDir())
|
2018-07-31 04:41:27 +00:00
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
|
2018-07-30 06:14:50 +00:00
|
|
|
config := &database.DBConfig{DataDir: tmp, Name: "beacontestdata", InMemory: false}
|
|
|
|
db, err := database.NewDB(config)
|
2018-07-19 16:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup beaconDB: %v", err)
|
2018-07-22 16:58:14 +00:00
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
}
|
2018-07-22 16:58:14 +00:00
|
|
|
endpoint := "ws://127.0.0.1"
|
2018-07-28 19:53:02 +00:00
|
|
|
web3Service, err := powchain.NewWeb3Service(ctx, &powchain.Web3ServiceConfig{Endpoint: endpoint, Pubkey: "", VrcAddr: common.Address{}})
|
2018-07-22 16:58:14 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to set up web3 service: %v", err)
|
|
|
|
}
|
|
|
|
chainService, err := NewChainService(ctx, db, web3Service)
|
2018-07-19 16:31:50 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to setup chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
chainService.Start()
|
|
|
|
|
|
|
|
if err := chainService.Stop(); err != nil {
|
|
|
|
t.Fatalf("unable to stop chain service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := hook.AllEntries()[0].Message
|
2018-07-28 19:53:02 +00:00
|
|
|
want := "Starting service"
|
2018-07-19 16:31:50 +00:00
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = hook.AllEntries()[1].Message
|
|
|
|
want = "No chainstate found on disk, initializing beacon from genesis"
|
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:14:50 +00:00
|
|
|
msg = hook.AllEntries()[2].Message
|
2018-07-25 16:57:44 +00:00
|
|
|
want = "Stopping service"
|
2018-07-19 16:31:50 +00:00
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
2018-07-31 04:41:27 +00:00
|
|
|
msg = hook.AllEntries()[3].Message
|
|
|
|
want = "Persisting current active and crystallized states before closing"
|
|
|
|
if msg != want {
|
|
|
|
t.Errorf("incorrect log, expected %s, got %s", want, msg)
|
|
|
|
}
|
|
|
|
|
2018-07-19 16:31:50 +00:00
|
|
|
// The context should have been canceled.
|
|
|
|
if chainService.ctx.Err() == nil {
|
|
|
|
t.Error("context was not canceled")
|
|
|
|
}
|
|
|
|
hook.Reset()
|
|
|
|
}
|