Skip to content

Commit 44ac3f3

Browse files
aroulletalicebob
andauthored
feat: implement WAIT command as no-op (#420)
* feat: implement WAIT command as no-op * add small int test --------- Co-authored-by: Harmen <alicebob@lijzij.de>
1 parent ed06249 commit 44ac3f3

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Implemented commands:
5757
- TTL
5858
- TYPE
5959
- UNLINK
60+
- WAIT -- no-op
6061
- Transactions (complete)
6162
- DISCARD
6263
- EXEC
@@ -305,7 +306,6 @@ Commands which will probably not be implemented:
305306
- Key
306307
- ~~MIGRATE~~
307308
- ~~OBJECT~~
308-
- ~~WAIT~~
309309
- Scripting
310310
- ~~FCALL / FCALL_RO *~~
311311
- ~~FUNCTION *~~

cmd_generic.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func commandsGeneric(m *Miniredis) {
5656
m.srv.Register("SCAN", m.cmdScan, server.ReadOnlyOption())
5757
// SORT
5858
m.srv.Register("UNLINK", m.cmdDel)
59+
m.srv.Register("WAIT", m.cmdWait)
5960
}
6061

6162
type expireOpts struct {
@@ -773,3 +774,26 @@ func (m *Miniredis) cmdCopy(c *server.Peer, cmd string, args []string) {
773774
c.WriteInt(1)
774775
})
775776
}
777+
778+
// WAIT
779+
func (m *Miniredis) cmdWait(c *server.Peer, cmd string, args []string) {
780+
if !m.isValidCMD(c, cmd, args, exactly(2)) {
781+
return
782+
}
783+
nReplicas, err := strconv.Atoi(args[0])
784+
if err != nil || nReplicas < 0 {
785+
c.WriteError(msgInvalidInt)
786+
return
787+
}
788+
timeout, err := strconv.Atoi(args[1])
789+
if err != nil {
790+
c.WriteError(msgInvalidInt)
791+
return
792+
}
793+
if timeout < 0 {
794+
c.WriteError(msgTimeoutNegative)
795+
return
796+
}
797+
// WAIT always returns 0 when called on a standalone instance
798+
c.WriteInt(0)
799+
}

cmd_generic_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,3 +990,27 @@ func TestCopy(t *testing.T) {
990990
)
991991
})
992992
}
993+
994+
func TestWait(t *testing.T) {
995+
_, c := runWithClient(t)
996+
997+
t.Run("success", func(t *testing.T) {
998+
must0(t, c, "WAIT", "2", "100")
999+
must0(t, c, "WAIT", "1", "0")
1000+
})
1001+
1002+
t.Run("errors", func(t *testing.T) {
1003+
mustDo(t, c, "WAIT",
1004+
proto.Error(errWrongNumber("wait")),
1005+
)
1006+
mustDo(t, c, "WAIT", "2",
1007+
proto.Error(errWrongNumber("wait")),
1008+
)
1009+
mustDo(t, c, "WAIT", "2", "bar",
1010+
proto.Error(msgInvalidInt),
1011+
)
1012+
mustDo(t, c, "WAIT", "foo", "100",
1013+
proto.Error(msgInvalidInt),
1014+
)
1015+
})
1016+
}

cmd_scripting_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,11 @@ func TestCmdEvalResponse(t *testing.T) {
469469
"EVAL", "return redis.call('HMGET','mkey', 'bad', 'key')", "0",
470470
proto.Array(proto.Nil, proto.Nil),
471471
)
472+
473+
mustDo(t, c,
474+
"EVAL", "return redis.call('WAIT', '2', '100')", "0",
475+
proto.Int(0),
476+
)
472477
}
473478

474479
func TestCmdEvalAuth(t *testing.T) {

integration/server_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,19 @@ func TestServer(t *testing.T) {
5555
c.Error("wrong number of arguments", "MEMORY", "USAGE")
5656
c.Error("syntax error", "MEMORY", "USAGE", "too", "many")
5757
})
58+
59+
testRaw(t, func(c *client) {
60+
c.DoLoosely("WAIT", "1", "1")
61+
62+
// Failure cases
63+
c.Error("wrong number", "WAIT", "1")
64+
c.Error("wrong number", "WAIT", "1", "2", "3")
65+
c.Error("wrong number", "WAIT")
66+
c.Error("not an integer", "WAIT", "foo", "0")
67+
c.Error("not an integer", "WAIT", "1", "foo")
68+
// c.Error("out of range", "WAIT", "-1", "0") // something weird going on
69+
c.Error("timeout is negative", "WAIT", "11", "-12")
70+
})
5871
}
5972

6073
func TestServerTLS(t *testing.T) {

0 commit comments

Comments
 (0)