2021-04-15 13:50:34 +00:00
|
|
|
package timer_test
|
2021-01-21 16:32:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-04-15 13:50:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/timer"
|
2021-01-21 16:32:41 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-04-15 13:50:34 +00:00
|
|
|
func tickN(t *timer.BlockTimer, n uint32) {
|
2021-01-21 16:32:41 +00:00
|
|
|
for i := uint32(0); i < n; i++ {
|
|
|
|
t.Tick()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlockTimer(t *testing.T) {
|
|
|
|
blockDur := uint32(10)
|
|
|
|
baseCallCounter := uint32(0)
|
|
|
|
|
2021-04-15 13:50:34 +00:00
|
|
|
bt := timer.NewBlockTimer(timer.StaticBlockMeter(blockDur), func() {
|
2021-01-21 16:32:41 +00:00
|
|
|
baseCallCounter++
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, bt.Reset())
|
|
|
|
|
|
|
|
intervalNum := uint32(7)
|
|
|
|
|
|
|
|
tickN(bt, intervalNum*blockDur)
|
|
|
|
|
|
|
|
require.Equal(t, intervalNum, uint32(baseCallCounter))
|
|
|
|
|
|
|
|
// add half-interval handler
|
|
|
|
halfCallCounter := uint32(0)
|
|
|
|
|
|
|
|
bt.OnDelta(1, 2, func() {
|
|
|
|
halfCallCounter++
|
|
|
|
})
|
|
|
|
|
|
|
|
// add double interval handler
|
|
|
|
doubleCallCounter := uint32(0)
|
|
|
|
|
|
|
|
bt.OnDelta(2, 1, func() {
|
|
|
|
doubleCallCounter++
|
|
|
|
})
|
|
|
|
|
|
|
|
require.NoError(t, bt.Reset())
|
|
|
|
|
|
|
|
baseCallCounter = 0
|
|
|
|
intervalNum = 20
|
|
|
|
|
|
|
|
tickN(bt, intervalNum*blockDur)
|
|
|
|
|
2021-01-21 19:16:43 +00:00
|
|
|
require.Equal(t, intervalNum, uint32(halfCallCounter))
|
2021-01-21 16:32:41 +00:00
|
|
|
require.Equal(t, intervalNum, uint32(baseCallCounter))
|
|
|
|
require.Equal(t, intervalNum/2, uint32(doubleCallCounter))
|
|
|
|
}
|
2021-01-21 19:16:43 +00:00
|
|
|
|
|
|
|
func TestDeltaPulse(t *testing.T) {
|
|
|
|
blockDur := uint32(9)
|
|
|
|
baseCallCounter := uint32(0)
|
|
|
|
|
2021-04-15 13:50:34 +00:00
|
|
|
bt := timer.NewBlockTimer(timer.StaticBlockMeter(blockDur), func() {
|
2021-01-21 19:16:43 +00:00
|
|
|
baseCallCounter++
|
|
|
|
})
|
|
|
|
|
|
|
|
deltaCallCounter := uint32(0)
|
|
|
|
|
|
|
|
div := uint32(3)
|
|
|
|
|
|
|
|
bt.OnDelta(1, div, func() {
|
|
|
|
deltaCallCounter++
|
2021-04-15 13:50:34 +00:00
|
|
|
}, timer.WithPulse())
|
2021-01-21 19:16:43 +00:00
|
|
|
|
|
|
|
require.NoError(t, bt.Reset())
|
|
|
|
|
|
|
|
intervalNum := uint32(7)
|
|
|
|
|
|
|
|
tickN(bt, intervalNum*blockDur)
|
|
|
|
|
|
|
|
require.Equal(t, intervalNum, uint32(baseCallCounter))
|
|
|
|
require.Equal(t, intervalNum*div, uint32(deltaCallCounter))
|
|
|
|
}
|
2021-01-28 16:10:21 +00:00
|
|
|
|
|
|
|
func TestDeltaReset(t *testing.T) {
|
|
|
|
blockDur := uint32(6)
|
|
|
|
baseCallCounter := 0
|
|
|
|
|
2021-04-15 13:50:34 +00:00
|
|
|
bt := timer.NewBlockTimer(timer.StaticBlockMeter(blockDur), func() {
|
2021-01-28 16:10:21 +00:00
|
|
|
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)
|
|
|
|
}
|