Skip to content

Commit cbf3bd8

Browse files
authored
chore: test timeout behaviour (#26554)
1 parent c335df1 commit cbf3bd8

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

tariff/helper_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ func TestMergeRatesAfter(t *testing.T) {
2929
new := api.Rates{rate(2, 2), rate(3, 3)}
3030
combined := api.Rates{rate(1, 1), rate(2, 2), rate(3, 3)}
3131

32-
data := util.NewMonitor[api.Rates](time.Hour)
33-
data.WithClock(clock)
32+
data := util.NewMonitor[api.Rates](time.Hour).WithClock(clock)
3433

3534
data.Set(old)
3635
res, err := data.Get()

util/monitor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ func NewMonitor[T any](timeout time.Duration) *Monitor[T] {
3131
}
3232

3333
// WithClock sets the a clock for debugging
34-
func (m *Monitor[T]) WithClock(clock clock.Clock) {
34+
func (m *Monitor[T]) WithClock(clock clock.Clock) *Monitor[T] {
3535
m.clock = clock
36+
return m
3637
}
3738

3839
// Set updates the current value and timestamp

util/monitor_test.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"testing"
66
"time"
77

8-
"github.com/stretchr/testify/require"
8+
"github.com/benbjohnson/clock"
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestMonitorRacyMaps(t *testing.T) {
@@ -42,13 +43,29 @@ func TestMonitorRacyMaps(t *testing.T) {
4243
default:
4344
err := m.GetFunc(func(mm map[int]int) {
4445
for k, v := range mm {
45-
require.Equal(t, k, v)
46+
assert.Equal(t, k, v)
4647
}
4748
})
48-
require.NoError(t, err)
49+
assert.NoError(t, err)
4950
}
5051
}
5152
}()
5253

5354
<-done
5455
}
56+
57+
func TestMonitorWithoutTimeout(t *testing.T) {
58+
clock := clock.NewMock()
59+
m := NewMonitor[int](0).WithClock(clock)
60+
61+
_, err := m.Get()
62+
assert.Error(t, err)
63+
64+
m.Set(0)
65+
_, err = m.Get()
66+
assert.NoError(t, err)
67+
68+
clock.Add(time.Hour)
69+
_, err = m.Get()
70+
assert.NoError(t, err)
71+
}

0 commit comments

Comments
 (0)