MCPcopy
hub / github.com/grafana/dskit / TestWatchKeyWithRateLimit

Function TestWatchKeyWithRateLimit

kv/consul/client_test.go:33–66  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

31}
32
33func TestWatchKeyWithRateLimit(t *testing.T) {
34 c, closer := NewInMemoryClientWithConfig(codec.String{}, Config{
35 WatchKeyRateLimit: 5.0,
36 WatchKeyBurstSize: 1,
37 }, testLogger{}, prometheus.NewPedanticRegistry())
38 t.Cleanup(func() {
39 assert.NoError(t, closer.Close())
40 })
41
42 const key = "test"
43 const max = 100
44
45 ch := writeValuesToKV(t, c, key, 0, max, 10*time.Millisecond)
46
47 observed := observeValueForSomeTime(t, c, key, 1200*time.Millisecond) // little over 1 second
48
49 // wait until updater finishes
50 <-ch
51
52 if testing.Verbose() {
53 t.Log(observed)
54 }
55 // Let's see how many updates we have observed. Given the rate limit and our observing time, it should be 6
56 // We should also have seen one of the later values, as we're observing for longer than a second, so rate limit should allow
57 // us to see it.
58 if len(observed) < 5 || len(observed) > 10 {
59 t.Error("Expected ~6 observed values, got", observed)
60 }
61 last := observed[len(observed)-1]
62 n, _ := strconv.Atoi(last)
63 if n < max/2 {
64 t.Error("Expected to see high last observed value, got", observed)
65 }
66}
67
68func TestWatchKeyNoRateLimit(t *testing.T) {
69 c, closer := NewInMemoryClientWithConfig(codec.String{}, Config{

Callers

nothing calls this directly

Calls 6

writeValuesToKVFunction · 0.85
observeValueForSomeTimeFunction · 0.85
CloseMethod · 0.65
LogMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected