Skip to content

Commit

Permalink
fix: SPOP with count bug (#239)
Browse files Browse the repository at this point in the history
* fix SPOP with count bug

* fix SPOP with count bug

---------

Co-authored-by: Tal Zaccai <talzacc@microsoft.com>
  • Loading branch information
Zzhiter and TalZaccai committed Apr 10, 2024
1 parent b311568 commit a149323
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions libs/server/Objects/Set/SetObjectImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ private void SetPop(byte* input, int length, ref SpanByteAndMemory output)
ObjectUtils.ReallocateOutput(ref output, ref isMemory, ref ptr, ref ptrHandle, ref curr, ref end);
countDone++;
}

countDone += count - countDone;
}
else if (count == int.MinValue) // no count parameter is present, we just pop and return a random item of the set
{
Expand Down
31 changes: 31 additions & 0 deletions test/Garnet.test/RespSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,37 @@ public void CanDoSPOPWithCountCommandLC()
Assert.AreEqual(2, arrLen);
}

[Test]
public void CanDoSPOPWithMoreCountThanSetSizeCommandLC()
{
CreateLongSet();

var lightClientRequest = TestUtils.CreateRequest();

var response = lightClientRequest.SendCommand("SPOP myset 10", 5);

var strResponse = Encoding.ASCII.GetString(response);
Assert.AreEqual('*', strResponse[0]);

var arrLenEndIdx = strResponse.IndexOf("\r\n", StringComparison.InvariantCultureIgnoreCase);
Assert.IsTrue(arrLenEndIdx > 1);

var strArrLen = Encoding.ASCII.GetString(response).Substring(1, arrLenEndIdx - 1);
Assert.IsTrue(int.TryParse(strArrLen, out var arrLen));
Assert.IsTrue(arrLen == 5);

var lightClientRequest2 = TestUtils.CreateRequest();
var response2 = lightClientRequest2.SendCommand("SADD myset one");
var expectedResponse = ":1\r\n";
strResponse = Encoding.ASCII.GetString(response2).Substring(0, expectedResponse.Length);
Assert.AreEqual(expectedResponse, strResponse);

response2 = lightClientRequest2.SendCommand("SCARD myset");
expectedResponse = ":1\r\n";
strResponse = Encoding.ASCII.GetString(response2).Substring(0, expectedResponse.Length);
Assert.AreEqual(expectedResponse, strResponse);
}

[Test]
public void MultiWithNonExistingSet()
{
Expand Down

0 comments on commit a149323

Please sign in to comment.