Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ZMPOP/BZMPOP duplicate parameters #1543

Merged
merged 3 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
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
18 changes: 10 additions & 8 deletions src/commands/cmd_zset.cc
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,11 @@ class CommandZMPop : public Commander {
}

while (parser.Good()) {
if (parser.EatEqICase("min")) {
if (flag_ == ZSET_NONE && parser.EatEqICase("min")) {
flag_ = ZSET_MIN;
} else if (parser.EatEqICase("max")) {
} else if (flag_ == ZSET_NONE && parser.EatEqICase("max")) {
flag_ = ZSET_MAX;
} else if (parser.EatEqICase("count")) {
} else if (count_ == 0 && parser.EatEqICase("count")) {
count_ = GET_OR_RET(parser.TakeInt<int>(NumericRange<int>{1, std::numeric_limits<int>::max()}));
} else {
return parser.InvalidSyntax();
Expand All @@ -483,6 +483,7 @@ class CommandZMPop : public Commander {
if (flag_ == ZSET_NONE) {
return parser.InvalidSyntax();
}
if (count_ == 0) count_ = 1;
return Commander::Parse(args);
}

Expand Down Expand Up @@ -514,7 +515,7 @@ class CommandZMPop : public Commander {
int numkeys_;
std::vector<std::string> keys_;
enum { ZSET_MIN, ZSET_MAX, ZSET_NONE } flag_ = ZSET_NONE;
int count_ = 1;
int count_ = 0;
};

class CommandBZMPop : public Commander,
Expand All @@ -535,11 +536,11 @@ class CommandBZMPop : public Commander,
}

while (parser.Good()) {
if (parser.EatEqICase("min")) {
if (flag_ == ZSET_NONE && parser.EatEqICase("min")) {
flag_ = ZSET_MIN;
} else if (parser.EatEqICase("max")) {
} else if (flag_ == ZSET_NONE && parser.EatEqICase("max")) {
flag_ = ZSET_MAX;
} else if (parser.EatEqICase("count")) {
} else if (count_ == 0 && parser.EatEqICase("count")) {
count_ = GET_OR_RET(parser.TakeInt<int>(NumericRange<int>{1, std::numeric_limits<int>::max()}));
} else {
return parser.InvalidSyntax();
Expand All @@ -549,6 +550,7 @@ class CommandBZMPop : public Commander,
if (flag_ == ZSET_NONE) {
return parser.InvalidSyntax();
}
if (count_ == 0) count_ = 1;

return Commander::Parse(args);
}
Expand Down Expand Up @@ -659,7 +661,7 @@ class CommandBZMPop : public Commander,
int num_keys_;
std::vector<std::string> keys_;
enum { ZSET_MIN, ZSET_MAX, ZSET_NONE } flag_ = ZSET_NONE;
int count_ = 1;
int count_ = 0;
Server *svr_ = nullptr;
Connection *conn_ = nullptr;
UniqueEvent timer_;
Expand Down
27 changes: 27 additions & 0 deletions tests/gocase/unit/type/zset/zset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,19 @@ func basicTests(t *testing.T, rdb *redis.Client, ctx context.Context, encoding s
require.EqualValues(t, 0, rdb.Exists(ctx, "zseta", "zsetb").Val())
})

t.Run(fmt.Sprintf("ZMPOP error - %s", encoding), func(t *testing.T) {
rdb.Del(ctx, "zseta")
rdb.Del(ctx, "zsetb")

util.ErrorRegexp(t, rdb.Do(ctx, "zmpop", 1, "zseta").Err(), ".*wrong number of arguments.*")
util.ErrorRegexp(t, rdb.Do(ctx, "zmpop", "wrong_numkeys", "zseta", "zsetb").Err(), ".*not started as an integer.*")
util.ErrorRegexp(t, rdb.Do(ctx, "zmpop", 2, "zseta", "min").Err(), ".*syntax error.*")
util.ErrorRegexp(t, rdb.Do(ctx, "zmpop", 2, "zseta", "zsetb", "min", "min").Err(), ".*syntax error.*")
util.ErrorRegexp(t, rdb.Do(ctx, "zmpop", 1, "zseta", "min", "max").Err(), ".*syntax error.*")
util.ErrorRegexp(t, rdb.Do(ctx, "zmpop", 1, "zseta", "min", "count", "wrong_count").Err(), ".*not started as an integer.*")
util.ErrorRegexp(t, rdb.Do(ctx, "zmpop", 1, "zseta", "min", "count", 1, "count", 10).Err(), ".*syntax error.*")
})

t.Run(fmt.Sprintf("BZMPOP basics - %s", encoding), func(t *testing.T) {
rdb.Del(ctx, "zseta")
rdb.Del(ctx, "zsetb")
Expand Down Expand Up @@ -427,6 +440,20 @@ func basicTests(t *testing.T, rdb *redis.Client, ctx context.Context, encoding s
require.Equal(t, []redis.Z{{Score: 1, Member: "a"}, {Score: 2, Member: "b"}}, zset)
})

t.Run(fmt.Sprintf("BZMPOP error - %s", encoding), func(t *testing.T) {
rdb.Del(ctx, "zseta")
rdb.Del(ctx, "zsetb")

util.ErrorRegexp(t, rdb.Do(ctx, "bzmpop", 0.1, 1, "zseta").Err(), ".*wrong number of arguments.*")
util.ErrorRegexp(t, rdb.Do(ctx, "bzmpop", "wrong_timeout", 1, "zseta", "min").Err(), ".*not started as a number.*")
util.ErrorRegexp(t, rdb.Do(ctx, "bzmpop", 0.1, "wrong_numkeys", "zseta", "min").Err(), ".*not started as an integer.*")
util.ErrorRegexp(t, rdb.Do(ctx, "bzmpop", 0.1, 2, "zseta", "min").Err(), ".*syntax error.*")
util.ErrorRegexp(t, rdb.Do(ctx, "bzmpop", 0.1, 1, "zseta", "min", "max").Err(), ".*syntax error.*")
util.ErrorRegexp(t, rdb.Do(ctx, "bzmpop", 0.1, 2, "zseta", "zsetb", "min", "min").Err(), ".*syntax error.*")
util.ErrorRegexp(t, rdb.Do(ctx, "bzmpop", 0.1, 1, "zseta", "min", "count", "wrong_count").Err(), ".*not started as an integer.*")
util.ErrorRegexp(t, rdb.Do(ctx, "bzmpop", 0.1, 1, "zseta", "min", "count", 1, "count", 10).Err(), ".*syntax error.*")
})

t.Run(fmt.Sprintf("ZRANGESTORE basics - %s", encoding), func(t *testing.T) {
rdb.Del(ctx, "zsrc")
rdb.Del(ctx, "zdst")
Expand Down