From c82c60df11e97e83bf402255191d3a97af4e97eb Mon Sep 17 00:00:00 2001 From: Alexander Simmerl Date: Thu, 19 Jul 2018 17:44:48 +0200 Subject: [PATCH] rpc: Test Validator retrevial timeout --- rpc/core/status.go | 18 +++++++++++++++--- rpc/core/status_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) create mode 100644 rpc/core/status_test.go diff --git a/rpc/core/status.go b/rpc/core/status.go index de3ddb7f7..739e67b86 100644 --- a/rpc/core/status.go +++ b/rpc/core/status.go @@ -104,8 +104,13 @@ func Status() (*ctypes.ResultStatus, error) { return result, nil } +const consensusTimeout = time.Second + func validatorAtHeight(h int64) *types.Validator { - lastBlockHeight, vals := getValidatorsWithTimeout(1 * time.Second) + lastBlockHeight, vals := getValidatorsWithTimeout( + consensusState, + consensusTimeout, + ) if lastBlockHeight == -1 { return nil @@ -136,15 +141,22 @@ func validatorAtHeight(h int64) *types.Validator { return nil } +type validatorRetriever interface { + GetValidators() (int64, []*types.Validator) +} + // NOTE: Consensus might halt, but we still need to process RPC requests (at // least for endpoints whole output does not depend on consensus state). -func getValidatorsWithTimeout(t time.Duration) (int64, []*types.Validator) { +func getValidatorsWithTimeout( + vr validatorRetriever, + t time.Duration, +) (int64, []*types.Validator) { resultCh := make(chan struct { lastBlockHeight int64 vals []*types.Validator }) go func() { - h, v := consensusState.GetValidators() + h, v := vr.GetValidators() resultCh <- struct { lastBlockHeight int64 vals []*types.Validator diff --git a/rpc/core/status_test.go b/rpc/core/status_test.go new file mode 100644 index 000000000..e44ffed0f --- /dev/null +++ b/rpc/core/status_test.go @@ -0,0 +1,39 @@ +package core + +import ( + "testing" + "time" + + "github.com/tendermint/tendermint/types" +) + +func TestGetValidatorsWithTimeout(t *testing.T) { + height, vs := getValidatorsWithTimeout( + testValidatorReceiver{}, + time.Millisecond, + ) + + if height != -1 { + t.Errorf("expected negative height") + } + + if len(vs) != 0 { + t.Errorf("expected no validators") + } +} + +type testValidatorReceiver struct{} + +func (tr testValidatorReceiver) GetValidators() (int64, []*types.Validator) { + vs := []*types.Validator{} + + for i := 0; i < 3; i++ { + v, _ := types.RandValidator(true, 10) + + vs = append(vs, v) + } + + time.Sleep(time.Millisecond) + + return 10, vs +}