diff --git a/README.md b/README.md index 710d164..8216d0c 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,7 @@ Implemented commands: - TTL - TYPE - UNLINK + - WAIT -- no-op - Transactions (complete) - DISCARD - EXEC @@ -305,7 +306,6 @@ Commands which will probably not be implemented: - Key - ~~MIGRATE~~ - ~~OBJECT~~ - - ~~WAIT~~ - Scripting - ~~FCALL / FCALL_RO *~~ - ~~FUNCTION *~~ diff --git a/cmd_generic.go b/cmd_generic.go index 2f55d5f..e27d009 100644 --- a/cmd_generic.go +++ b/cmd_generic.go @@ -56,6 +56,7 @@ func commandsGeneric(m *Miniredis) { m.srv.Register("SCAN", m.cmdScan, server.ReadOnlyOption()) // SORT m.srv.Register("UNLINK", m.cmdDel) + m.srv.Register("WAIT", m.cmdWait) } type expireOpts struct { @@ -773,3 +774,22 @@ func (m *Miniredis) cmdCopy(c *server.Peer, cmd string, args []string) { c.WriteInt(1) }) } + +// WAIT +func (m *Miniredis) cmdWait(c *server.Peer, cmd string, args []string) { + if !m.isValidCMD(c, cmd, args, exactly(2)) { + return + } + nReplicas, err := strconv.Atoi(args[0]) + if err != nil || nReplicas < 0 { + c.WriteError(msgInvalidInt) + return + } + timeout, err := strconv.Atoi(args[1]) + if err != nil || timeout < 0 { + c.WriteError(msgInvalidInt) + return + } + // WAIT always returns 0 when called on a standalone instance + c.WriteInt(0) +} diff --git a/cmd_generic_test.go b/cmd_generic_test.go index 3994bac..ef3f025 100644 --- a/cmd_generic_test.go +++ b/cmd_generic_test.go @@ -990,3 +990,27 @@ func TestCopy(t *testing.T) { ) }) } + +func TestWait(t *testing.T) { + _, c := runWithClient(t) + + t.Run("success", func(t *testing.T) { + must0(t, c, "WAIT", "2", "100") + must0(t, c, "WAIT", "1", "0") + }) + + t.Run("errors", func(t *testing.T) { + mustDo(t, c, "WAIT", + proto.Error(errWrongNumber("wait")), + ) + mustDo(t, c, "WAIT", "2", + proto.Error(errWrongNumber("wait")), + ) + mustDo(t, c, "WAIT", "2", "bar", + proto.Error(msgInvalidInt), + ) + mustDo(t, c, "WAIT", "foo", "100", + proto.Error(msgInvalidInt), + ) + }) +} diff --git a/cmd_scripting_test.go b/cmd_scripting_test.go index b8fcd69..144f0f4 100644 --- a/cmd_scripting_test.go +++ b/cmd_scripting_test.go @@ -469,6 +469,11 @@ func TestCmdEvalResponse(t *testing.T) { "EVAL", "return redis.call('HMGET','mkey', 'bad', 'key')", "0", proto.Array(proto.Nil, proto.Nil), ) + + mustDo(t, c, + "EVAL", "return redis.call('WAIT', '2', '100')", "0", + proto.Int(0), + ) } func TestCmdEvalAuth(t *testing.T) {