| 149 | } |
| 150 | |
| 151 | func TestWatchKeyWithNoStartValue(t *testing.T) { |
| 152 | c, closer := NewInMemoryClient(codec.String{}, testLogger{}, prometheus.NewPedanticRegistry()) |
| 153 | t.Cleanup(func() { |
| 154 | assert.NoError(t, closer.Close()) |
| 155 | }) |
| 156 | |
| 157 | const key = "test" |
| 158 | |
| 159 | go func() { |
| 160 | time.Sleep(100 * time.Millisecond) |
| 161 | _, err := c.kv.Put(&consul.KVPair{Key: key, Value: []byte("start")}, nil) |
| 162 | require.NoError(t, err) |
| 163 | |
| 164 | time.Sleep(100 * time.Millisecond) |
| 165 | _, err = c.kv.Put(&consul.KVPair{Key: key, Value: []byte("end")}, nil) |
| 166 | require.NoError(t, err) |
| 167 | }() |
| 168 | |
| 169 | ctx, fn := context.WithTimeout(context.Background(), 300*time.Millisecond) |
| 170 | defer fn() |
| 171 | |
| 172 | reported := 0 |
| 173 | c.WatchKey(ctx, key, func(interface{}) bool { |
| 174 | reported++ |
| 175 | return reported != 2 |
| 176 | }) |
| 177 | |
| 178 | // we should see both start and end values. |
| 179 | require.Equal(t, 2, reported) |
| 180 | } |
| 181 | |
| 182 | type testLogger struct { |
| 183 | } |