Skip to content

Commit

Permalink
- added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Dec 22, 2022
1 parent f9b3ce6 commit 2d4a6a8
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions consensus/spos/worker_test.go
Expand Up @@ -580,6 +580,41 @@ func TestWorker_ProcessReceivedMessageNodeNotInEligibleListShouldErr(t *testing.
func TestWorker_ProcessReceivedMessageComputeReceivedProposedBlockMetric(t *testing.T) {
t.Parallel()

t.Run("normal operation", func(t *testing.T) {
t.Parallel()

roundDuration := time.Millisecond * 1000
delay := time.Millisecond * 430
roundStartTimeStamp := time.Now()

receivedValue := testWorkerProcessReceivedMessageComputeReceivedProposedBlockMetric(roundStartTimeStamp, delay, roundDuration)

minimumExpectedValue := uint64(delay * 100 / roundDuration)
assert.True(t,
receivedValue >= minimumExpectedValue,
fmt.Sprintf("minimum expected was %d, got %d", minimumExpectedValue, receivedValue),
)
})
t.Run("time.Since returns negative value", func(t *testing.T) {
// test the edgecase when the returned NTP time stored in the round handler is
// slightly advanced when comparing with time.Now.
t.Parallel()

roundDuration := time.Millisecond * 1000
delay := time.Millisecond * 430
roundStartTimeStamp := time.Now().Add(time.Minute)

receivedValue := testWorkerProcessReceivedMessageComputeReceivedProposedBlockMetric(roundStartTimeStamp, delay, roundDuration)

assert.Zero(t, receivedValue)
})
}

func testWorkerProcessReceivedMessageComputeReceivedProposedBlockMetric(
roundStartTimeStamp time.Time,
delay time.Duration,
roundDuration time.Duration,
) uint64 {
receivedValue := uint64(0)
wrk := *initWorker(&statusHandlerMock.AppStatusHandlerStub{
SetUInt64ValueHandler: func(key string, value uint64) {
Expand All @@ -599,9 +634,7 @@ func TestWorker_ProcessReceivedMessageComputeReceivedProposedBlockMetric(t *test
return nil
},
})
roundDuration := time.Millisecond * 1000
delay := time.Millisecond * 430
roundStartTimeStamp := time.Now()

wrk.SetRoundHandler(&mock.RoundHandlerMock{
RoundIndex: 0,
TimeDurationCalled: func() time.Duration {
Expand Down Expand Up @@ -639,11 +672,7 @@ func TestWorker_ProcessReceivedMessageComputeReceivedProposedBlockMetric(t *test
}
_ = wrk.ProcessReceivedMessage(msg, "")

minimumExpectedValue := uint64(delay * 100 / roundDuration)
assert.True(t,
receivedValue >= minimumExpectedValue,
fmt.Sprintf("minimum expected was %d, got %d", minimumExpectedValue, receivedValue),
)
return receivedValue
}

func TestWorker_ProcessReceivedMessageInconsistentChainIDInConsensusMessageShouldErr(t *testing.T) {
Expand Down

0 comments on commit 2d4a6a8

Please sign in to comment.