Skip to content

[TestSuit] Fix CanDoSRANDMEMBERWithCountCommandLC test #1143

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

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
1 change: 1 addition & 0 deletions test/Garnet.test/GarnetServerConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ public async Task MultiTcpSocketTest()
var hostname = TestUtils.GetHostName();
var addresses = Dns.GetHostAddresses(hostname);
addresses = [.. addresses, IPAddress.IPv6Loopback, IPAddress.Loopback];
addresses = [.. addresses.Distinct()];

var endpoints = addresses.Select(address => new IPEndPoint(address, TestUtils.TestPort)).ToArray();
var server = TestUtils.CreateGarnetServer(TestUtils.MethodTestDir, endpoints: endpoints);
Expand Down
7 changes: 2 additions & 5 deletions test/Garnet.test/RespBlockingCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license.

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Garnet.server;
Expand Down Expand Up @@ -444,8 +443,7 @@ public async Task BasicBzmpopWithExpireItemsTest(string mode)
using var lcr = TestUtils.CreateRequest();
var response = lcr.SendCommand($"BZMPOP 1 1 {key} {mode}");
var expectedResponse = "$-1\r\n";
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

[Test]
Expand Down Expand Up @@ -551,8 +549,7 @@ public async Task BasicBzpopMinMaxWithExpireItemsTest(string command)
using var lcr = TestUtils.CreateRequest();
var response = lcr.SendCommand($"{command} {key} 1");
var expectedResponse = "$-1\r\n";
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

[Test]
Expand Down
75 changes: 20 additions & 55 deletions test/Garnet.test/RespSetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Garnet.common;
using Garnet.server;
using NUnit.Framework;
using NUnit.Framework.Legacy;
Expand Down Expand Up @@ -1007,7 +1008,7 @@ public void CanDoSCARDCommandsLC()
}

[Test]
public void CanDoSRANDMEMBERWithCountCommandLC()
public unsafe void CanDoSRANDMEMBERWithCountCommandLC()
{
var myset = new HashSet<string> { "one", "two", "three", "four", "five" };

Expand All @@ -1025,43 +1026,31 @@ public void CanDoSRANDMEMBERWithCountCommandLC()
CreateLongSet();

response = lightClientRequest.SendCommand("SRANDMEMBER myset", 1);
var strLen = Encoding.ASCII.GetString(response).Substring(1, 1);
var item = Encoding.ASCII.GetString(response).Substring(4, Int32.Parse(strLen));
var strLen = Encoding.ASCII.GetString(response, 1, 1);
var item = Encoding.ASCII.GetString(response, 4, int.Parse(strLen));
ClassicAssert.IsTrue(myset.Contains(item));

// Get three random members
response = lightClientRequest.SendCommand("SRANDMEMBER myset 3", 3);
TestUtils.AssertEqualUpToExpectedLength("*", response);

var strResponse = Encoding.ASCII.GetString(response);
var arrLenEndIdx = strResponse.IndexOf("\r\n", StringComparison.InvariantCultureIgnoreCase);
ClassicAssert.IsTrue(arrLenEndIdx > 1);

var strArrLen = strResponse.AsSpan().Slice(1, arrLenEndIdx - 1);
ClassicAssert.IsTrue(int.TryParse(strArrLen, out var arrLen));
ClassicAssert.AreEqual(3, arrLen);
TestUtils.AssertEqualUpToExpectedLength("*3\r\n", response);

// Get 6 random members and verify that at least two elements are the same
response = lightClientRequest.SendCommand("SRANDMEMBER myset -6", 6);
var strReponse = Encoding.ASCII.GetString(response);
arrLenEndIdx = strReponse.IndexOf("\r\n", StringComparison.InvariantCultureIgnoreCase);
strArrLen = strReponse.AsSpan().Slice(1, arrLenEndIdx - 1);
ClassicAssert.IsTrue(int.TryParse(strArrLen, out arrLen));

var members = new HashSet<string>();
var repeatedMembers = false;
for (var i = 0; i < arrLen; i++)
TestUtils.AssertEqualUpToExpectedLength("*6\r\n", response);

string[] results;

fixed (byte* p = &response[0])
{
var member = strReponse.Substring(arrLenEndIdx + 2, response.Length - arrLenEndIdx - 5);
if (members.Contains(member))
{
repeatedMembers = true;
break;
}
members.Add(member);
var ptr = p;
ClassicAssert.IsTrue(
RespReadUtils.TryReadStringArrayWithLengthHeader(out results, ref ptr,
p + (Garnet.common.NumUtils.MaximumFormatInt64Length * 10))
);
}

ClassicAssert.IsTrue(repeatedMembers, "At least two members are repeated.");
ClassicAssert.IsTrue(results.Distinct().Count() != results.Length,
"At least two members are repeated.");
}

[Test]
Expand Down Expand Up @@ -1096,31 +1085,15 @@ public void CanDoSPOPWithCountCommandLC()

var lightClientRequest = TestUtils.CreateRequest();
var response = lightClientRequest.SendCommand("SPOP myset 3", 3);
TestUtils.AssertEqualUpToExpectedLength("*", response);

var strResponse = Encoding.ASCII.GetString(response);
var arrLenEndIdx = strResponse.IndexOf("\r\n", StringComparison.InvariantCultureIgnoreCase);
ClassicAssert.IsTrue(arrLenEndIdx > 1);

var strArrLen = strResponse.AsSpan().Slice(1, arrLenEndIdx - 1);
ClassicAssert.IsTrue(int.TryParse(strArrLen, out var arrLen));
ClassicAssert.AreEqual(3, arrLen);
TestUtils.AssertEqualUpToExpectedLength("*3\r\n", response);

response = lightClientRequest.SendCommands("SCARD myset", "PING", 1, 1);
var expectedResponse = ":2\r\n+PONG\r\n";
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

// Test for popping set until empty
response = lightClientRequest.SendCommand("SPOP myset 2", 2);
TestUtils.AssertEqualUpToExpectedLength("*", response);

strResponse = Encoding.ASCII.GetString(response);
arrLenEndIdx = strResponse.IndexOf("\r\n", StringComparison.InvariantCultureIgnoreCase);
ClassicAssert.IsTrue(arrLenEndIdx > 1);

strArrLen = strResponse.AsSpan().Slice(1, arrLenEndIdx - 1);
ClassicAssert.IsTrue(int.TryParse(strArrLen, out arrLen));
ClassicAssert.AreEqual(2, arrLen);
TestUtils.AssertEqualUpToExpectedLength("*2\r\n", response);
}

[Test]
Expand All @@ -1131,15 +1104,7 @@ public void CanDoSPOPWithMoreCountThanSetSizeCommandLC()
var lightClientRequest = TestUtils.CreateRequest();

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

var strResponse = Encoding.ASCII.GetString(response);
var arrLenEndIdx = strResponse.IndexOf("\r\n", StringComparison.InvariantCultureIgnoreCase);
ClassicAssert.IsTrue(arrLenEndIdx > 1);

var strArrLen = strResponse.AsSpan().Slice(1, arrLenEndIdx - 1);
ClassicAssert.IsTrue(int.TryParse(strArrLen, out var arrLen));
ClassicAssert.IsTrue(arrLen == 5);
TestUtils.AssertEqualUpToExpectedLength("*5\r\n", response);

var lightClientRequest2 = TestUtils.CreateRequest();
response = lightClientRequest2.SendCommand("SADD myset one");
Expand Down
11 changes: 6 additions & 5 deletions test/Garnet.test/RespSortedSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4208,15 +4208,16 @@ public void CanUseZRandMember(int bytesSent)
_ = lightClientRequest.SendCommand("ZADD dadi 1 uno 2 due 3 tre 4 quattro 5 cinque 6 six 7 sept 8 huit 9 nough 10 dis");

// ZRANDMEMBER
var s = Encoding.ASCII.GetString(lightClientRequest.SendCommandChunks("ZRANDMEMBER dadi", bytesSent));
int startIndexField = s.IndexOf('\n') + 1;
int endIndexField = s.IndexOf('\n', startIndexField) - 1;
string memberValue = s.Substring(startIndexField, endIndexField - startIndexField);
var response = lightClientRequest.SendCommandChunks("ZRANDMEMBER dadi", bytesSent);
var s = Encoding.ASCII.GetString(response, 0, Garnet.common.NumUtils.MaximumFormatInt64Length);
var startIndexField = s.IndexOf('\n') + 1;
var endIndexField = s.IndexOf('\n', startIndexField) - 1;
var memberValue = s.Substring(startIndexField, endIndexField - startIndexField);
var foundInSet = ("uno due tre quattro cinque six sept huit nough dis").IndexOf(memberValue, StringComparison.InvariantCultureIgnoreCase);
ClassicAssert.IsTrue(foundInSet >= 0);

// ZRANDMEMBER count
var response = lightClientRequest.SendCommandChunks("ZRANDMEMBER dadi 5", bytesSent, 6);
response = lightClientRequest.SendCommandChunks("ZRANDMEMBER dadi 5", bytesSent, 6);
var expectedResponse = "*5\r\n"; // 5 values
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

Expand Down
15 changes: 10 additions & 5 deletions test/Garnet.test/RespTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4958,17 +4958,19 @@ public async Task ClientUnblockBasicTest(string clientType, string mode, bool ex

// Start blocking client
using var blockingClient = TestUtils.CreateRequest();
var clientIdResponse = Encoding.ASCII.GetString(blockingClient.SendCommand("CLIENT ID"));
var clientIdResponse = Encoding.ASCII.GetString(blockingClient.SendCommand("CLIENT ID"), 0,
Garnet.common.NumUtils.MaximumFormatInt64Length * 2);
var clientId = clientIdResponse.Substring(1, clientIdResponse.IndexOf("\r\n") - 1);
var isError = false;
Task blockingTask = null;
bool isError = false;

if (clientType == "BLOCKING")
{
blockingTask = taskFactory.StartNew(() =>
{
var startTime = Stopwatch.GetTimestamp();
var response = blockingClient.SendCommand("BLMPOP 10 1 keyA LEFT");
if (Encoding.ASCII.GetString(response).Substring(0, "-UNBLOCKED".Length) == "-UNBLOCKED")
if (Encoding.ASCII.GetString(response, 0, "-UNBLOCKED".Length) == "-UNBLOCKED")
{
isError = true;
}
Expand Down Expand Up @@ -5019,14 +5021,17 @@ public async Task MultipleClientsUnblockAndAddTest(int numberOfItems)

// Start blocking client
using var blockingClient = TestUtils.CreateRequest();
var clientIdResponse = Encoding.ASCII.GetString(blockingClient.SendCommand("CLIENT ID"));
var clientIdResponse = Encoding.ASCII.GetString(blockingClient.SendCommand("CLIENT ID"), 0,
Garnet.common.NumUtils.MaximumFormatInt64Length * 2);
var clientId = clientIdResponse.Substring(1, clientIdResponse.IndexOf("\r\n") - 1);
ClassicAssert.IsTrue(long.TryParse(clientId, out _));

string blockingResult = null;
var blockingTask = Task.Run(() =>
{
var response = blockingClient.SendCommand($"BLMPOP 10 1 {key} LEFT COUNT 30");
blockingResult = Encoding.ASCII.GetString(response);
blockingResult = Encoding.ASCII.GetString(response, 0,
Garnet.common.NumUtils.MaximumFormatInt64Length * (numberOfItems + 1));
});

// Wait for client to enter blocking state
Expand Down
1 change: 0 additions & 1 deletion test/Garnet.test/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ internal static class TestUtils
/// </summary>
static readonly bool useTestLogger = false;

private static int procId = Process.GetCurrentProcess().Id;
internal static string CustomRespCommandInfoJsonPath = "CustomRespCommandsInfo.json";
internal static string CustomRespCommandDocsJsonPath = "CustomRespCommandsDocs.json";

Expand Down