(t *testing.T)
| 209 | } |
| 210 | |
| 211 | func TestDoBatchWithOptionsContextCancellation(t *testing.T) { |
| 212 | const ( |
| 213 | numKeys = 5e6 |
| 214 | numInstances = 100 |
| 215 | numZones = 3 |
| 216 | ) |
| 217 | cancelCause := errors.New("cancel cause") |
| 218 | |
| 219 | measureDuration := func(r *Ring, keys []uint32) time.Duration { |
| 220 | callback := func(InstanceDesc, []int) error { return nil } |
| 221 | t0 := time.Now() |
| 222 | err := DoBatchWithOptions(context.Background(), Write, r, keys, callback, DoBatchOptions{}) |
| 223 | duration := time.Since(t0) |
| 224 | require.NoError(t, err) |
| 225 | t.Logf("Call took %s", duration) |
| 226 | return duration |
| 227 | } |
| 228 | |
| 229 | type callbackFunc = func(InstanceDesc, []int) error |
| 230 | never := func(_ InstanceDesc, _ []int) error { |
| 231 | t.Errorf("should not be called.") |
| 232 | return nil |
| 233 | } |
| 234 | tests := []struct { |
| 235 | name string |
| 236 | setup func(*Ring, []uint32) (context.Context, callbackFunc) |
| 237 | expectedErr error |
| 238 | }{ |
| 239 | { |
| 240 | name: "context deadline exceeded", |
| 241 | setup: func(r *Ring, keys []uint32) (context.Context, callbackFunc) { |
| 242 | duration := measureDuration(r, keys) |
| 243 | |
| 244 | // Make a second call cancelling after a hundredth of duration of the first one. |
| 245 | // For a 4s first call, this is 40ms: should be enough for this test to not be flaky. |
| 246 | ctx, cancel := context.WithTimeout(context.Background(), duration/100) |
| 247 | go func() { |
| 248 | <-ctx.Done() |
| 249 | cancel() |
| 250 | }() |
| 251 | return ctx, never |
| 252 | }, |
| 253 | expectedErr: context.DeadlineExceeded, |
| 254 | }, |
| 255 | { |
| 256 | name: "context deadline exceeded with cause", |
| 257 | setup: func(r *Ring, keys []uint32) (context.Context, callbackFunc) { |
| 258 | duration := measureDuration(r, keys) |
| 259 | |
| 260 | // Make a second call cancelling after a hundredth of duration of the first one. |
| 261 | // For a 4s first call, this is 40ms: should be enough for this test to not be flaky. |
| 262 | ctx, cancel := context.WithTimeoutCause(context.Background(), duration/100, cancelCause) |
| 263 | go func() { |
| 264 | <-ctx.Done() |
| 265 | cancel() |
| 266 | }() |
| 267 | return ctx, never |
| 268 | }, |
nothing calls this directly
no test coverage detected