Skip to content

Commit

Permalink
Fixes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKiwi committed Apr 10, 2020
1 parent 2b63c9e commit c067dd6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 55 deletions.
2 changes: 1 addition & 1 deletion beacon-chain/rpc/validator/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (vs *Server) WaitForSynced(req *ptypes.Empty, stream ethpb.BeaconNodeValida
select {
case event := <-stateChannel:
if event.Type == statefeed.Synced {
data := event.Data.(*statefeed.ChainStartedData)
data := event.Data.(*statefeed.SyncedData)
log.WithField("starttime", data.StartTime).Debug("Received sync completed event")
log.Info("Sending genesis time notification to connected validator clients")
res := &ethpb.SyncedResponse{
Expand Down
6 changes: 4 additions & 2 deletions beacon-chain/rpc/validator/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
internal "github.com/prysmaticlabs/prysm/beacon-chain/rpc/testing"
mockRPC "github.com/prysmaticlabs/prysm/beacon-chain/rpc/testing"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
mockSync "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
pbp2p "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/event"
Expand Down Expand Up @@ -275,6 +276,7 @@ func TestWaitForSynced_AlreadyStarted(t *testing.T) {
BeaconDB: db,
StateNotifier: chainService.StateNotifier(),
HeadFetcher: chainService,
SyncChecker: &mockSync.Sync{IsSyncing: false},
}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down Expand Up @@ -325,8 +327,8 @@ func TestWaitForSynced_NotStartedThenLogFired(t *testing.T) {
// Send in a loop to ensure it is delivered (busy wait for the service to subscribe to the state feed).
for sent := 0; sent == 0; {
sent = Server.StateNotifier.StateFeed().Send(&feed.Event{
Type: statefeed.ChainStarted,
Data: &statefeed.ChainStartedData{
Type: statefeed.Synced,
Data: &statefeed.SyncedData{
StartTime: time.Unix(0, 0),
},
})
Expand Down
1 change: 0 additions & 1 deletion shared/mock/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ go_library(
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@io_bazel_rules_go//proto/wkt:empty_go_proto",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//metadata:go_default_library",
],
Expand Down
16 changes: 8 additions & 8 deletions shared/mock/beacon_chain_service_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions shared/mock/node_service_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions validator/client/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func (v *validator) WaitForSynced(ctx context.Context) error {
return nil
}
v.genesisTime = syncedRes.GenesisTime
// Once we know the beacon chain is ready log is received, we update the genesis time of the validator client
// and begin a slot ticker used to track the current slot the beacon node is in.
v.ticker = slotutil.GetSlotTicker(time.Unix(int64(v.genesisTime), 0), params.BeaconConfig().SecondsPerSlot)
log.WithField("genesisTime", time.Unix(int64(v.genesisTime), 0)).Info("Beacon chain started")
break
}
// Once we know the beacon chain is ready log is received, we update the genesis time of the validator client
// and begin a slot ticker used to track the current slot the beacon node is in.
v.ticker = slotutil.GetSlotTicker(time.Unix(int64(v.genesisTime), 0), params.BeaconConfig().SecondsPerSlot)
log.WithField("genesisTime", time.Unix(int64(v.genesisTime), 0)).Info("Beacon chain started")
return nil
}

Expand Down Expand Up @@ -151,7 +151,6 @@ func (v *validator) WaitForActivation(ctx context.Context) error {
for _, pubKey := range validatorActivatedRecords {
log.WithField("pubKey", fmt.Sprintf("%#x", bytesutil.Trunc(pubKey[:]))).Info("Validator activated")
}
v.ticker = slotutil.GetSlotTicker(time.Unix(int64(v.genesisTime), 0), params.BeaconConfig().SecondsPerSlot)

return nil
}
Expand Down
61 changes: 29 additions & 32 deletions validator/client/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestWaitForSynced_ReceiveErrorFromStream(t *testing.T) {
errors.New("fails"),
)
err := v.WaitForSynced(context.Background())
want := "could not receive ChainStart from stream"
want := "could not receive Synced from stream"
if !strings.Contains(err.Error(), want) {
t.Errorf("Expected %v, received %v", want, err)
}
Expand Down Expand Up @@ -377,18 +377,29 @@ func TestWaitSync_ContextCanceled(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
n := internal.NewMockNodeClient(ctrl)
valClient := internal.NewMockBeaconNodeValidatorClient(ctrl)

v := validator{
node: n,
node: n,
validatorClient: valClient,
}

ctx, cancel := context.WithCancel(context.Background())
cancel()

n.EXPECT().GetSyncStatus(
genesis := uint64(time.Unix(0, 0).Unix())
clientStream := internal.NewMockBeaconNodeValidator_WaitForSyncedClient(ctrl)
clientStream.EXPECT().Recv().Return(
&ethpb.SyncedResponse{
Synced: true,
GenesisTime: genesis,
},
nil,
)
valClient.EXPECT().WaitForSynced(
gomock.Any(),
gomock.Any(),
).Return(&ethpb.SyncStatus{Syncing: true}, nil)
).Return(clientStream, nil)

err := v.WaitForSynced(ctx)
want := cancelledCtx
Expand All @@ -401,40 +412,26 @@ func TestWaitSync_NotSyncing(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
n := internal.NewMockNodeClient(ctrl)
valClient := internal.NewMockBeaconNodeValidatorClient(ctrl)

v := validator{
node: n,
node: n,
validatorClient: valClient,
}

n.EXPECT().GetSyncStatus(
gomock.Any(),
gomock.Any(),
).Return(&ethpb.SyncStatus{Syncing: false}, nil)

err := v.WaitForSynced(context.Background())
if err != nil {
t.Fatal(err)
}
}

func TestWaitSync_Syncing(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
n := internal.NewMockNodeClient(ctrl)

v := validator{
node: n,
}

n.EXPECT().GetSyncStatus(
gomock.Any(),
gomock.Any(),
).Return(&ethpb.SyncStatus{Syncing: true}, nil)

n.EXPECT().GetSyncStatus(
genesis := uint64(time.Unix(1, 0).Unix())
clientStream := internal.NewMockBeaconNodeValidator_WaitForSyncedClient(ctrl)
clientStream.EXPECT().Recv().Return(
&ethpb.SyncedResponse{
Synced: true,
GenesisTime: genesis,
},
nil,
)
valClient.EXPECT().WaitForSynced(
gomock.Any(),
gomock.Any(),
).Return(&ethpb.SyncStatus{Syncing: false}, nil)
).Return(clientStream, nil)

err := v.WaitForSynced(context.Background())
if err != nil {
Expand Down

0 comments on commit c067dd6

Please sign in to comment.