From f7a9e43165bcdb4a2a319eac3047001bbab2b2a0 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 28 Jan 2021 19:10:21 +0300 Subject: [PATCH] [#356] ir/blocktimer: Fix reset behavior In previous implementation handler was not called more the one time after Reset. It was caused by tick counter not being reset inside Reset method. Signed-off-by: Leonard Lyubich --- pkg/innerring/timers/block.go | 5 +---- pkg/innerring/timers/block_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pkg/innerring/timers/block.go b/pkg/innerring/timers/block.go index fea993337..e44379f1a 100644 --- a/pkg/innerring/timers/block.go +++ b/pkg/innerring/timers/block.go @@ -136,10 +136,7 @@ func (t *BlockTimer) reset() { } t.tgt = delta - - for i := range t.ps { - t.ps[i].reset() - } + t.cur = 0 } // Tick ticks one block in the BlockTimer. diff --git a/pkg/innerring/timers/block_test.go b/pkg/innerring/timers/block_test.go index 533c442c6..2b2db1c4e 100644 --- a/pkg/innerring/timers/block_test.go +++ b/pkg/innerring/timers/block_test.go @@ -82,3 +82,31 @@ func TestDeltaPulse(t *testing.T) { require.Equal(t, intervalNum, uint32(baseCallCounter)) require.Equal(t, intervalNum*div, uint32(deltaCallCounter)) } + +func TestDeltaReset(t *testing.T) { + blockDur := uint32(6) + baseCallCounter := 0 + + bt := timers.NewBlockTimer(timers.StaticBlockMeter(blockDur), func() { + baseCallCounter++ + }) + + detlaCallCounter := 0 + + bt.OnDelta(1, 3, func() { + detlaCallCounter++ + }) + + require.NoError(t, bt.Reset()) + + tickN(bt, 6) + + require.Equal(t, 1, baseCallCounter) + require.Equal(t, 1, detlaCallCounter) + + require.NoError(t, bt.Reset()) + + tickN(bt, 3) + + require.Equal(t, 2, detlaCallCounter) +}