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

Function TestDoBatchWithOptionsContextCancellation

ring/ring_test.go:211–342  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

209}
210
211func 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 },

Callers

nothing calls this directly

Calls 12

DoBatchWithOptionsFunction · 0.85
generateKeysFunction · 0.85
generateRingInstancesFunction · 0.85
newRingForTestingFunction · 0.85
ErrorfMethod · 0.80
RunMethod · 0.80
setRingStateFromDescMethod · 0.80
DoneMethod · 0.65
AddMethod · 0.65
WaitMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected