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

Function TestReset

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

Source from the content-addressed store, hash-verified

90}
91
92func TestReset(t *testing.T) {
93 c, closer := NewInMemoryClient(codec.String{}, testLogger{}, prometheus.NewPedanticRegistry())
94 t.Cleanup(func() {
95 assert.NoError(t, closer.Close())
96 })
97
98 const key = "test"
99 const max = 5
100
101 ch := make(chan error)
102 go func() {
103 defer close(ch)
104 for i := 0; i <= max; i++ {
105 t.Log("ts", time.Now(), "msg", "writing value", "val", i)
106 _, _ = c.kv.Put(&consul.KVPair{Key: key, Value: []byte(fmt.Sprintf("%d", i))}, nil)
107 if i == 1 {
108 c.kv.(*mockKV).ResetIndex()
109 }
110 if i == 2 {
111 c.kv.(*mockKV).ResetIndexForKey(key)
112 }
113 time.Sleep(10 * time.Millisecond)
114 }
115 }()
116
117 observed := observeValueForSomeTime(t, c, key, 25*max*time.Millisecond)
118
119 // wait until updater finishes
120 <-ch
121
122 // Let's see how many updates we have observed. Given the rate limit and our observing time, we should see all numeric values
123 if testing.Verbose() {
124 t.Log(observed)
125 }
126 if len(observed) < max {
127 t.Error("Expected all values, got", observed)
128 } else if observed[len(observed)-1] != fmt.Sprintf("%d", max) {
129 t.Error("Expected to see last written value, got", observed)
130 }
131}
132
133func observeValueForSomeTime(t *testing.T, client *Client, key string, timeout time.Duration) []string {
134 t.Helper()

Callers

nothing calls this directly

Calls 9

observeValueForSomeTimeFunction · 0.85
ResetIndexMethod · 0.80
ResetIndexForKeyMethod · 0.80
NewInMemoryClientFunction · 0.70
CloseMethod · 0.65
PutMethod · 0.65
SleepMethod · 0.65
LogMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected