poll_test.go 1.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
package tasks

import (
	"sync/atomic"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"github.com/ethereum-optimism/optimism/op-service/clock"
)

const eventualTimeout = 10 * time.Second

func TestPoller(t *testing.T) {
	cl := clock.NewDeterministicClock(time.Now())
	counter := new(atomic.Int64)
	poller := NewPoller(func() {
		counter.Add(1)
	}, cl, time.Second*5)

	poller.Start()

	cl.AdvanceTime(time.Second * 6) // hit the first tick

	require.Eventually(t, func() bool {
		t.Log("counter", counter.Load())
		return counter.Load() == 1
	}, eventualTimeout, time.Millisecond*100)

	cl.AdvanceTime(time.Second * 3) // no hit yet, 9 seconds have passsed now

	require.Never(t, func() bool {
		return counter.Load() == 2
	}, time.Second, time.Millisecond*100)

	// hit the second tick at 10s
	cl.AdvanceTime(time.Second * 2) // 11 seconds have passed now
	require.Eventually(t, func() bool {
		return counter.Load() == 2
	}, eventualTimeout, time.Millisecond*100)

	poller.Stop()

	// Poller was stopped, this shouldn't affect it
	cl.AdvanceTime(time.Second * 1000)

	// We should have stopped counting
	require.Never(t, func() bool {
		return counter.Load() > 2
	}, time.Second, time.Millisecond*100)

	// Start back up
	poller.Start()
	// No previously buffered ticks
	require.Never(t, func() bool {
		return counter.Load() > 2
	}, time.Second, time.Millisecond*100)

	// Change the interval, so we poll faster
	poller.SetInterval(time.Second * 2)

	cl.AdvanceTime(time.Second * 3)
	require.Eventually(t, func() bool {
		t.Log("counter", counter.Load())
		return counter.Load() == 3
	}, eventualTimeout, time.Millisecond*100)

	poller.Stop()
}