Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions tests/gocase/unit/wait/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,25 +242,28 @@ func TestWaitBlockExecutingCommand(t *testing.T) {
tcpClient := masterSrv.NewTCPClient()
defer func() { require.NoError(t, tcpClient.Close()) }()

// Create a normal client to check server state
masterRdb := masterSrv.NewClient()
defer func() { require.NoError(t, masterRdb.Close()) }()

// should be blocked after k1 is set
require.NoError(t, tcpClient.WriteArgs("SET", "k1", "v1"))
require.NoError(t, tcpClient.WriteArgs("WAIT", "1", "0"))

// sleep for some time, so the commands are not read by a single read callback
// if the server does not block the command while waiting
time.Sleep(1 * time.Second)
// FIX: Instead of sleeping, we wait until the server confirms the client is blocked.
// This makes the test deterministic and faster.
require.Eventually(t, func() bool {
info := masterRdb.Info(context.Background(), "clients").Val()
return strings.Contains(info, "blocked_clients:1")
}, 5*time.Second, 100*time.Millisecond)

// should be blocked after k1 is set to v1
require.NoError(t, tcpClient.WriteArgs("SET", "k1", "v2"))
require.NoError(t, tcpClient.WriteArgs("WAIT", "1", "0"))

time.Sleep(1 * time.Second)

// No need to sleep here; we know the previous WAIT is holding the connection.
require.NoError(t, tcpClient.WriteArgs("SET", "k1", "v3"))

masterRdb := masterSrv.NewClient()
defer func() { require.NoError(t, masterRdb.Close()) }()

// only the first command should be executed
require.Equal(t, "v1", masterRdb.Get(context.Background(), "k1").Val())

Expand Down
Loading