diff --git a/tariff/helper_test.go b/tariff/helper_test.go index e8ccf151b..1a0ed921b 100644 --- a/tariff/helper_test.go +++ b/tariff/helper_test.go @@ -29,8 +29,7 @@ func TestMergeRatesAfter(t *testing.T) { new := api.Rates{rate(2, 2), rate(3, 3)} combined := api.Rates{rate(1, 1), rate(2, 2), rate(3, 3)} - data := util.NewMonitor[api.Rates](time.Hour) - data.WithClock(clock) + data := util.NewMonitor[api.Rates](time.Hour).WithClock(clock) data.Set(old) res, err := data.Get() diff --git a/util/monitor.go b/util/monitor.go index acb97d16c..ab5497c7f 100644 --- a/util/monitor.go +++ b/util/monitor.go @@ -31,8 +31,9 @@ func NewMonitor[T any](timeout time.Duration) *Monitor[T] { } // WithClock sets the a clock for debugging -func (m *Monitor[T]) WithClock(clock clock.Clock) { +func (m *Monitor[T]) WithClock(clock clock.Clock) *Monitor[T] { m.clock = clock + return m } // Set updates the current value and timestamp diff --git a/util/monitor_test.go b/util/monitor_test.go index 8dc149023..b1e8bd8f6 100644 --- a/util/monitor_test.go +++ b/util/monitor_test.go @@ -5,7 +5,8 @@ import ( "testing" "time" - "github.com/stretchr/testify/require" + "github.com/benbjohnson/clock" + "github.com/stretchr/testify/assert" ) func TestMonitorRacyMaps(t *testing.T) { @@ -42,13 +43,29 @@ func TestMonitorRacyMaps(t *testing.T) { default: err := m.GetFunc(func(mm map[int]int) { for k, v := range mm { - require.Equal(t, k, v) + assert.Equal(t, k, v) } }) - require.NoError(t, err) + assert.NoError(t, err) } } }() <-done } + +func TestMonitorWithoutTimeout(t *testing.T) { + clock := clock.NewMock() + m := NewMonitor[int](0).WithClock(clock) + + _, err := m.Get() + assert.Error(t, err) + + m.Set(0) + _, err = m.Get() + assert.NoError(t, err) + + clock.Add(time.Hour) + _, err = m.Get() + assert.NoError(t, err) +}