Skip to content

Commit 5c0bc2d

Browse files
committed
move test to using require.Eventually and not time.Sleep
Signed-off-by: Nick Van Wiggeren <[email protected]>
1 parent 25a0e9c commit 5c0bc2d

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

go/test/endtoend/vtgate/misc_test.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -922,8 +922,11 @@ func TestTabletTargeting(t *testing.T) {
922922
useStmt = fmt.Sprintf("USE `ks:-80@replica|%s`", replicaAlias)
923923
utils.Exec(t, conn, useStmt)
924924

925-
// Reads should work on replica
926-
utils.AssertMatches(t, conn, "select id1 from t1 where id1 in (1, 2, 4, 10) order by id1", "[[INT64(1)] [INT64(2)] [INT64(4)] [INT64(10)]]")
925+
// Reads should work on replica (wait for replication)
926+
require.Eventually(t, func() bool {
927+
result, err := conn.ExecuteFetch("select id1 from t1 where id1 in (1, 2, 4, 10) order by id1", 10, false)
928+
return err == nil && len(result.Rows) == 4
929+
}, 15*time.Second, 100*time.Millisecond, "replication did not catch up for first replica read")
927930

928931
// Writes should fail on replica (replicas are read-only)
929932
_, err = conn.ExecuteFetch("insert into t1(id1, id2) values(99, 999)", 1, false)
@@ -935,8 +938,11 @@ func TestTabletTargeting(t *testing.T) {
935938
useStmt = fmt.Sprintf("USE `ks:-80@replica|%s`", secondReplicaAlias)
936939
utils.Exec(t, conn, useStmt)
937940

938-
// Should still be able to read from this different replica
939-
utils.AssertMatches(t, conn, "select id1 from t1 where id1 in (1, 2, 4, 10) order by id1", "[[INT64(1)] [INT64(2)] [INT64(4)] [INT64(10)]]")
941+
// Should still be able to read from this different replica (wait for replication)
942+
require.Eventually(t, func() bool {
943+
result, err := conn.ExecuteFetch("select id1 from t1 where id1 in (1, 2, 4, 10) order by id1", 10, false)
944+
return err == nil && len(result.Rows) == 4
945+
}, 15*time.Second, 100*time.Millisecond, "replication did not catch up for second replica read")
940946

941947
// Writes should still fail
942948
_, err = conn.ExecuteFetch("insert into t1(id1, id2) values(98, 998)", 1, false)
@@ -954,9 +960,11 @@ func TestTabletTargeting(t *testing.T) {
954960
replicaAlias = instances["-80"]["replica"][0]
955961
useStmt = fmt.Sprintf("USE `ks:-80@replica|%s`", replicaAlias)
956962
utils.Exec(t, conn, useStmt)
957-
// Give replication a moment to catch up
958-
time.Sleep(100 * time.Millisecond)
959-
utils.AssertMatches(t, conn, "select id1 from t1 where id1=50", "[[INT64(50)]]")
963+
// Wait for replication to catch up
964+
require.Eventually(t, func() bool {
965+
result, err := conn.ExecuteFetch("select id1 from t1 where id1=50", 1, false)
966+
return err == nil && len(result.Rows) == 1
967+
}, 15*time.Second, 100*time.Millisecond, "replication did not catch up")
960968

961969
// Query different replicas and verify different server UUIDs
962970
// This proves we're actually hitting different physical tablets

0 commit comments

Comments
 (0)