chore: test timeout behaviour (#26554)
Some checks failed
Some checks failed
This commit is contained in:
parent
c335df19fc
commit
cbf3bd82af
3 changed files with 23 additions and 6 deletions
|
|
@ -29,8 +29,7 @@ func TestMergeRatesAfter(t *testing.T) {
|
||||||
new := api.Rates{rate(2, 2), rate(3, 3)}
|
new := api.Rates{rate(2, 2), rate(3, 3)}
|
||||||
combined := api.Rates{rate(1, 1), 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 := util.NewMonitor[api.Rates](time.Hour).WithClock(clock)
|
||||||
data.WithClock(clock)
|
|
||||||
|
|
||||||
data.Set(old)
|
data.Set(old)
|
||||||
res, err := data.Get()
|
res, err := data.Get()
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,9 @@ func NewMonitor[T any](timeout time.Duration) *Monitor[T] {
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithClock sets the a clock for debugging
|
// 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
|
m.clock = clock
|
||||||
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set updates the current value and timestamp
|
// Set updates the current value and timestamp
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/benbjohnson/clock"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMonitorRacyMaps(t *testing.T) {
|
func TestMonitorRacyMaps(t *testing.T) {
|
||||||
|
|
@ -42,13 +43,29 @@ func TestMonitorRacyMaps(t *testing.T) {
|
||||||
default:
|
default:
|
||||||
err := m.GetFunc(func(mm map[int]int) {
|
err := m.GetFunc(func(mm map[int]int) {
|
||||||
for k, v := range mm {
|
for k, v := range mm {
|
||||||
require.Equal(t, k, v)
|
assert.Equal(t, k, v)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
<-done
|
<-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)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue