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 BZMPOP return and add more argument checking. #1078

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions libs/server/Resp/Objects/SortedSetCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ private unsafe bool SortedSetBlockingPop(RespCommand command)
return AbortWithWrongNumberOfArguments(command.ToString());
}

if (!parseState.TryGetDouble(parseState.Count - 1, out var timeout))
if (!parseState.TryGetDouble(parseState.Count - 1, out var timeout) || (timeout < 0))
{
return AbortWithErrorMessage(CmdStrings.RESP_ERR_TIMEOUT_NOT_VALID_FLOAT);
}
Expand Down Expand Up @@ -1605,13 +1605,13 @@ private unsafe bool SortedSetBlockingMPop()
var currTokenId = 0;

// Read timeout
if (!parseState.TryGetDouble(currTokenId++, out var timeout))
if (!parseState.TryGetDouble(currTokenId++, out var timeout) || (timeout < 0))
{
return AbortWithErrorMessage(CmdStrings.RESP_ERR_TIMEOUT_NOT_VALID_FLOAT);
}

// Read count of keys
if (!parseState.TryGetInt(currTokenId++, out var numKeys))
if (!parseState.TryGetInt(currTokenId++, out var numKeys) || (numKeys <= 0))
{
var err = string.Format(CmdStrings.GenericParamShouldBeGreaterThanZero, "numkeys");
return AbortWithErrorMessage(Encoding.ASCII.GetBytes(err));
Expand Down Expand Up @@ -1685,7 +1685,7 @@ private unsafe bool SortedSetBlockingMPop()
while (!RespWriteUtils.TryWriteArrayLength(result.Items.Length, ref dcurr, dend))
SendAndReset();

for (var i = 0; i < result.Items.Length; i += 2)
for (var i = 0; i < result.Items.Length; ++i)
{
while (!RespWriteUtils.TryWriteArrayLength(2, ref dcurr, dend))
SendAndReset();
Expand Down
25 changes: 25 additions & 0 deletions test/Garnet.test/RespBlockingCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Garnet.server;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using StackExchange.Redis;

namespace Garnet.test
{
Expand Down Expand Up @@ -413,6 +414,30 @@ public void BasicBzmpopTest(string mode, string expectedValue, double expectedSc
ClassicAssert.AreEqual(expectedResponse, actualValue);
}

[Test]
public void BzmpopReturnTest()
{
using var redis = ConnectionMultiplexer.Connect(TestUtils.GetConfig());
var db = redis.GetDatabase(0);

var res = db.SortedSetAdd("a", [new SortedSetEntry("one", 1)]);
ClassicAssert.AreEqual(1, res);
res = db.SortedSetAdd("a", [new SortedSetEntry("two", 2)]);
ClassicAssert.AreEqual(1, res);
res = db.SortedSetAdd("b", [new SortedSetEntry("three", 3)]);
ClassicAssert.AreEqual(1, res);
var pop = db.Execute("BZMPOP", "10", "2", "a", "b", "MIN", "COUNT", "2");
ClassicAssert.AreEqual(2, pop.Length);
ClassicAssert.AreEqual("a", pop[0].ToString());
ClassicAssert.AreEqual(2, pop[1].Length);
ClassicAssert.AreEqual(2, pop[1][0].Length);
ClassicAssert.AreEqual("one", pop[1][0][0].ToString());
ClassicAssert.AreEqual(1, (int)(RedisValue)pop[1][0][1]);
ClassicAssert.AreEqual(2, pop[1][1].Length);
ClassicAssert.AreEqual("two", pop[1][1][0].ToString());
ClassicAssert.AreEqual(2, (int)(RedisValue)pop[1][1][1]);
}

[Test]
[TestCase(1, "key1", "value1", 1.5, Description = "First key has minimum value")]
[TestCase(2, "key2", "value2", 2.5, Description = "Second key has minimum value")]
Expand Down