Test that writes only happen every update timeout.
(t *testing.T)
| 477 | |
| 478 | // Test that writes only happen every update timeout. |
| 479 | func 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. |
| 530 | func TestCheckReplicaMultiUser(t *testing.T) { |
nothing calls this directly
no test coverage detected