MCPcopy Create free account
hub / github.com/cortexproject/cortex / TestCheckReplicaUpdateTimeout

Function TestCheckReplicaUpdateTimeout

pkg/ha/ha_tracker_test.go:479–527  ·  view source on GitHub ↗

Test that writes only happen every update timeout.

(t *testing.T)

Source from the content-addressed store, hash-verified

477
478// Test that writes only happen every update timeout.
479func TestCheckReplicaUpdateTimeout(t *testing.T) {
480 t.Parallel()
481 replica := "r1"
482 replicaGroup := "c1"
483 user := "user"
484
485 codec := GetReplicaDescCodec()
486 kvStore, closer := consul.NewInMemoryClient(codec, log.NewNopLogger(), nil)
487 t.Cleanup(func() { assert.NoError(t, closer.Close()) })
488
489 mock := kv.PrefixClient(kvStore, "prefix")
490 c, err := NewHATracker(HATrackerConfig{
491 EnableHATracker: true,
492 KVStore: kv.Config{Mock: mock},
493 UpdateTimeout: time.Second,
494 UpdateTimeoutJitterMax: 0,
495 FailoverTimeout: time.Second,
496 }, trackerLimits{maxReplicaGroups: 100}, haTrackerStatusConfig, nil, "test-ha-tracker", log.NewNopLogger())
497 require.NoError(t, err)
498 require.NoError(t, services.StartAndAwaitRunning(context.Background(), c))
499 defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck
500
501 // Write the first time.
502 startTime := time.Now()
503 err = c.CheckReplica(context.Background(), user, replicaGroup, replica, startTime)
504 assert.NoError(t, err)
505
506 checkReplicaTimestamp(t, time.Second, c, user, replicaGroup, replica, startTime)
507
508 // Timestamp should not update here, since time has not advanced.
509 err = c.CheckReplica(context.Background(), user, replicaGroup, replica, startTime)
510 assert.NoError(t, err)
511
512 checkReplicaTimestamp(t, time.Second, c, user, replicaGroup, replica, startTime)
513
514 // Wait 500ms and the timestamp should still not update.
515 updateTime := time.Unix(0, startTime.UnixNano()).Add(500 * time.Millisecond)
516
517 err = c.CheckReplica(context.Background(), user, replicaGroup, replica, updateTime)
518 assert.NoError(t, err)
519 checkReplicaTimestamp(t, time.Second, c, user, replicaGroup, replica, startTime)
520
521 // Now we've waited > 1s, so the timestamp should update.
522 updateTime = time.Unix(0, startTime.UnixNano()).Add(1100 * time.Millisecond)
523
524 err = c.CheckReplica(context.Background(), user, replicaGroup, replica, updateTime)
525 assert.NoError(t, err)
526 checkReplicaTimestamp(t, time.Second, c, user, replicaGroup, replica, updateTime)
527}
528
529// Test that writes only happen every write timeout.
530func TestCheckReplicaMultiUser(t *testing.T) {

Callers

nothing calls this directly

Calls 10

CheckReplicaMethod · 0.95
NewInMemoryClientFunction · 0.92
PrefixClientFunction · 0.92
StartAndAwaitRunningFunction · 0.92
StopAndAwaitTerminatedFunction · 0.92
GetReplicaDescCodecFunction · 0.85
NewHATrackerFunction · 0.85
checkReplicaTimestampFunction · 0.85
CloseMethod · 0.65
AddMethod · 0.45

Tested by

no test coverage detected