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: spop set index get error #191

Merged
merged 8 commits into from
Apr 4, 2024
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
2 changes: 1 addition & 1 deletion libs/server/Objects/Set/SetObjectImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void SetPop(byte* input, int length, ref SpanByteAndMemory output)
for (int i = 0; i < countParameter; i++)
{
// Generate a new index based on the elements left in the set
var index = RandomNumberGenerator.GetInt32(0, set.Count - 1);
var index = RandomNumberGenerator.GetInt32(0, set.Count);
var item = set.ElementAt(index);
set.Remove(item);
this.UpdateSize(item, false);
Expand Down
37 changes: 24 additions & 13 deletions test/Garnet.test/RespSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,26 +368,37 @@ public void CanDoSPOPCommandLC()
[Test]
public void CanDoSPOPWithCountCommandLC()
{
var myset = new HashSet<string>();
myset.Add("one");
myset.Add("two");
myset.Add("three");
myset.Add("four");
myset.Add("five");

CreateLongSet();

using var lightClientRequest = TestUtils.CreateRequest();
var response = lightClientRequest.SendCommand("SPOP myset 3", 4);
var strLen = Encoding.ASCII.GetString(response).Substring(1, 1);
Assert.AreEqual(3, Int32.Parse(strLen));
var lightClientRequest = TestUtils.CreateRequest();
var response = lightClientRequest.SendCommand("SPOP myset 3", 3);
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.AreEqual(3, arrLen);

var secondResponse = lightClientRequest.SendCommands("SCARD myset", "PING", 1, 1);
var expectedResponse = ":2\r\n+PONG\r\n";
var strResponse = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
strResponse = Encoding.ASCII.GetString(secondResponse).Substring(0, expectedResponse.Length);
Assert.AreEqual(expectedResponse, strResponse);
}

// Test for popping set until empty
response = lightClientRequest.SendCommand("SPOP myset 2", 2);
Zzhiter marked this conversation as resolved.
Show resolved Hide resolved
strResponse = Encoding.ASCII.GetString(response);
Assert.AreEqual('*', strResponse[0]);

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

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

[Test]
public void MultiWithNonExistingSet()
Expand Down