| 183 | } |
| 184 | |
| 185 | func TestSourceLoader_SuccessThenError(t *testing.T) { |
| 186 | t.Parallel() |
| 187 | synctest.Test(t, func(t *testing.T) { |
| 188 | inner := &mockSource{ |
| 189 | name: "test.yaml", |
| 190 | data: []byte("initial data"), |
| 191 | } |
| 192 | ctx := t.Context() |
| 193 | refreshInterval := 50 * time.Millisecond |
| 194 | sl := newSourceLoader(ctx, inner, refreshInterval) |
| 195 | |
| 196 | // Initial read succeeds |
| 197 | data, err := sl.Read(ctx) |
| 198 | require.NoError(t, err) |
| 199 | assert.Equal(t, []byte("initial data"), data) |
| 200 | |
| 201 | // Introduce error |
| 202 | inner.setErr(errors.New("refresh error")) |
| 203 | |
| 204 | synctest.Wait() |
| 205 | time.Sleep(60 * time.Millisecond) //nolint:forbidigo // fake time inside a synctest bubble; returns instantly |
| 206 | synctest.Wait() |
| 207 | |
| 208 | // Should still return old cached data despite refresh error |
| 209 | data, err = sl.Read(ctx) |
| 210 | require.NoError(t, err) |
| 211 | assert.Equal(t, []byte("initial data"), data) |
| 212 | }) |
| 213 | } |