Test that writes only happen every write timeout.
(t *testing.T)
| 528 | |
| 529 | // Test that writes only happen every write timeout. |
| 530 | func TestCheckReplicaMultiUser(t *testing.T) { |
| 531 | t.Parallel() |
| 532 | replica := "r1" |
| 533 | replicaGroup := "c1" |
| 534 | |
| 535 | codec := GetReplicaDescCodec() |
| 536 | kvStore, closer := consul.NewInMemoryClient(codec, log.NewNopLogger(), nil) |
| 537 | t.Cleanup(func() { assert.NoError(t, closer.Close()) }) |
| 538 | |
| 539 | mock := kv.PrefixClient(kvStore, "prefix") |
| 540 | c, err := NewHATracker(HATrackerConfig{ |
| 541 | EnableHATracker: true, |
| 542 | KVStore: kv.Config{Mock: mock}, |
| 543 | UpdateTimeout: 100 * time.Millisecond, |
| 544 | UpdateTimeoutJitterMax: 0, |
| 545 | FailoverTimeout: time.Second, |
| 546 | }, trackerLimits{maxReplicaGroups: 100}, haTrackerStatusConfig, nil, "test-ha-tracker", log.NewNopLogger()) |
| 547 | require.NoError(t, err) |
| 548 | require.NoError(t, services.StartAndAwaitRunning(context.Background(), c)) |
| 549 | defer services.StopAndAwaitTerminated(context.Background(), c) //nolint:errcheck |
| 550 | |
| 551 | now := time.Now() |
| 552 | |
| 553 | // Write the first time for user 1. |
| 554 | err = c.CheckReplica(context.Background(), "user1", replicaGroup, replica, now) |
| 555 | assert.NoError(t, err) |
| 556 | checkReplicaTimestamp(t, time.Second, c, "user1", replicaGroup, replica, now) |
| 557 | |
| 558 | // Write the first time for user 2. |
| 559 | err = c.CheckReplica(context.Background(), "user2", replicaGroup, replica, now) |
| 560 | assert.NoError(t, err) |
| 561 | checkReplicaTimestamp(t, time.Second, c, "user2", replicaGroup, replica, now) |
| 562 | |
| 563 | // Now we've waited > 1s, so the timestamp should update. |
| 564 | updated := now.Add(1100 * time.Millisecond) |
| 565 | err = c.CheckReplica(context.Background(), "user1", replicaGroup, replica, updated) |
| 566 | assert.NoError(t, err) |
| 567 | checkReplicaTimestamp(t, time.Second, c, "user1", replicaGroup, replica, updated) |
| 568 | // No update for user2. |
| 569 | checkReplicaTimestamp(t, time.Second, c, "user2", replicaGroup, replica, now) |
| 570 | } |
| 571 | |
| 572 | func TestCheckReplicaUpdateTimeoutJitter(t *testing.T) { |
| 573 | t.Parallel() |
nothing calls this directly
no test coverage detected