prysm-pulse/validator/client/runner_test.go
Ivan Martinez 25102e0978
Use WaitForSynced in validator client for startup (#5465)
* Add WaitForSynced to beacon-chain
* Fix build issues
* Fix comment
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into simplify-validator
* Fix tests
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into simplify-validator
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into simplify-validator
* Change validator client to use WaitForSynced
* Change prysm script to support windows
* Update prysm.sh
* Merge branch '0xKiwi-patch-1' of https://github.com/prysmaticlabs/prysm into simplify-val-client
* Merge branch 'master' into simplify-val-client
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into simplify-val-client
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into simplify-val-client
* Add to mock
* Merge branch 'simplify-val-client' of https://github.com/0xKiwi/Prysm into simplify-val-client
* Fix mocks
* Add feature flag for WaitForSynced
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into simplify-val-client
* Fix flag
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into simplify-val-client
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into simplify-val-client
* Remove comment
* Merge branch 'master' into simplify-val-client
* Merge branch 'master' into simplify-val-client
* Merge branch 'master' into simplify-val-client
* Merge branch 'master' into simplify-val-client
2020-04-20 23:16:53 +00:00

199 lines
4.5 KiB
Go

package client
import (
"context"
"errors"
"testing"
"time"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func cancelledContext() context.Context {
ctx, cancel := context.WithCancel(context.Background())
cancel()
return ctx
}
func TestCancelledContext_CleansUpValidator(t *testing.T) {
v := &fakeValidator{}
run(cancelledContext(), v)
if !v.DoneCalled {
t.Error("Expected Done() to be called")
}
}
func TestCancelledContext_WaitsForChainStart(t *testing.T) {
v := &fakeValidator{}
run(cancelledContext(), v)
if !v.WaitForChainStartCalled {
t.Error("Expected WaitForChainStart() to be called")
}
}
func TestCancelledContext_WaitsForSynced(t *testing.T) {
cfg := &featureconfig.Flags{
WaitForSynced: true,
}
featureconfig.Init(cfg)
v := &fakeValidator{}
run(cancelledContext(), v)
if !v.WaitForSyncedCalled {
t.Error("Expected WaitForSynced() to be called")
}
}
func TestCancelledContext_WaitsForActivation(t *testing.T) {
v := &fakeValidator{}
run(cancelledContext(), v)
if !v.WaitForActivationCalled {
t.Error("Expected WaitForActivation() to be called")
}
}
func TestUpdateDuties_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
go func() {
ticker <- slot
cancel()
}()
run(ctx, v)
if !v.UpdateDutiesCalled {
t.Fatalf("Expected UpdateAssignments(%d) to be called", slot)
}
if v.UpdateDutiesArg1 != slot {
t.Errorf("UpdateAssignments was called with wrong argument. Want=%d, got=%d", slot, v.UpdateDutiesArg1)
}
}
func TestUpdateDuties_HandlesError(t *testing.T) {
hook := logTest.NewGlobal()
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
go func() {
ticker <- slot
cancel()
}()
v.UpdateDutiesRet = errors.New("bad")
run(ctx, v)
testutil.AssertLogsContain(t, hook, "Failed to update assignments")
}
func TestRoleAt_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
go func() {
ticker <- slot
cancel()
}()
run(ctx, v)
if !v.RoleAtCalled {
t.Fatalf("Expected RoleAt(%d) to be called", slot)
}
if v.RoleAtArg1 != slot {
t.Errorf("RoleAt called with the wrong arg. Want=%d, got=%d", slot, v.RoleAtArg1)
}
}
func TestAttests_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RolesAtRet = []validatorRole{roleAttester}
go func() {
ticker <- slot
cancel()
}()
timer := time.NewTimer(200 * time.Millisecond)
run(ctx, v)
<-timer.C
if !v.AttestToBlockHeadCalled {
t.Fatalf("SubmitAttestation(%d) was not called", slot)
}
if v.AttestToBlockHeadArg1 != slot {
t.Errorf("SubmitAttestation was called with wrong arg. Want=%d, got=%d", slot, v.AttestToBlockHeadArg1)
}
}
func TestProposes_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RolesAtRet = []validatorRole{roleProposer}
go func() {
ticker <- slot
cancel()
}()
timer := time.NewTimer(200 * time.Millisecond)
run(ctx, v)
<-timer.C
if !v.ProposeBlockCalled {
t.Fatalf("ProposeBlock(%d) was not called", slot)
}
if v.ProposeBlockArg1 != slot {
t.Errorf("ProposeBlock was called with wrong arg. Want=%d, got=%d", slot, v.AttestToBlockHeadArg1)
}
}
func TestBothProposesAndAttests_NextSlot(t *testing.T) {
v := &fakeValidator{}
ctx, cancel := context.WithCancel(context.Background())
slot := uint64(55)
ticker := make(chan uint64)
v.NextSlotRet = ticker
v.RolesAtRet = []validatorRole{roleAttester, roleProposer}
go func() {
ticker <- slot
cancel()
}()
timer := time.NewTimer(200 * time.Millisecond)
run(ctx, v)
<-timer.C
if !v.AttestToBlockHeadCalled {
t.Fatalf("SubmitAttestation(%d) was not called", slot)
}
if v.AttestToBlockHeadArg1 != slot {
t.Errorf("SubmitAttestation was called with wrong arg. Want=%d, got=%d", slot, v.AttestToBlockHeadArg1)
}
if !v.ProposeBlockCalled {
t.Fatalf("ProposeBlock(%d) was not called", slot)
}
if v.ProposeBlockArg1 != slot {
t.Errorf("ProposeBlock was called with wrong arg. Want=%d, got=%d", slot, v.AttestToBlockHeadArg1)
}
}