Skip to content

Commit

Permalink
Use consistent assert for lightClient responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
prvyk authored and prvyk committed Mar 7, 2025
1 parent 1f58916 commit c831a20
Show file tree
Hide file tree
Showing 14 changed files with 533 additions and 907 deletions.
4 changes: 2 additions & 2 deletions test/Garnet.test/GarnetBitmapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ public void BitmapSimpleSetGet_PCT(int bytesPerSend)

var expectedResponse = ":0\r\n";
var response = lightClientRequest.SendCommandChunks("SETBIT mykey 7 1", bytesPerSend);
ClassicAssert.AreEqual(response.AsSpan().Slice(0, expectedResponse.Length).ToArray(), expectedResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

expectedResponse = ":1\r\n";
response = lightClientRequest.SendCommandChunks("GETBIT mykey 7", bytesPerSend);
ClassicAssert.AreEqual(response.AsSpan().Slice(0, expectedResponse.Length).ToArray(), expectedResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

[Test, Order(5)]
Expand Down
26 changes: 9 additions & 17 deletions test/Garnet.test/HyperLogLogTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void HyperLogLogSimple_PCT(int bytesPerSend)
{
response = lightClientRequest.SendCommandChunks("PFADD mykey " + data[i], bytesPerSend);
expectedResponse = i == 0 || data[i - 1] != data[i] ? ":1\r\n" : ":0\r\n";
ClassicAssert.AreEqual(response.AsSpan().Slice(0, expectedResponse.Length).ToArray(), expectedResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}
lightClientRequest.Dispose();
}
Expand All @@ -222,50 +222,42 @@ public void HyperLogLogArraySimple_PCT(int bytesPerSend)
//1. PFADD mykey
response = lightClientRequest.SendCommandChunks("PFADD mykey h e l l o", bytesPerSend);
expectedResponse = ":1\r\n";
var actualResponse = Encoding.ASCII.GetString(response.AsSpan().Slice(0, expectedResponse.Length));
ClassicAssert.AreEqual(expectedResponse, actualResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

//2. PFCOUNT mykey
response = lightClientRequest.SendCommandChunks("PFCOUNT mykey", bytesPerSend);
expectedResponse = ":4\r\n";
actualResponse = Encoding.ASCII.GetString(response.AsSpan().Slice(0, expectedResponse.Length));
ClassicAssert.AreEqual(expectedResponse, actualResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

//3. PFADD mykey2
response = lightClientRequest.SendCommandChunks("PFADD mykey2 w o r l d", bytesPerSend);
expectedResponse = ":1\r\n";
actualResponse = Encoding.ASCII.GetString(response.AsSpan().Slice(0, expectedResponse.Length));
ClassicAssert.AreEqual(expectedResponse, actualResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

//4. PFCOUNT mykey mykey2
response = lightClientRequest.SendCommandChunks("PFCOUNT mykey mykey2", bytesPerSend);
expectedResponse = ":7\r\n";
actualResponse = Encoding.ASCII.GetString(response.AsSpan().Slice(0, expectedResponse.Length));
ClassicAssert.AreEqual(expectedResponse, actualResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

//5. PFMERGE mykey3
response = lightClientRequest.SendCommandChunks("PFMERGE mykey3 mykey", bytesPerSend);
expectedResponse = "+OK\r\n";
actualResponse = Encoding.ASCII.GetString(response.AsSpan().Slice(0, expectedResponse.Length));
ClassicAssert.AreEqual(expectedResponse, actualResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

//6. PFCOUNT mykey3
response = lightClientRequest.SendCommandChunks("PFCOUNT mykey3", bytesPerSend);
expectedResponse = ":4\r\n";
actualResponse = Encoding.ASCII.GetString(response.AsSpan().Slice(0, expectedResponse.Length));
ClassicAssert.AreEqual(expectedResponse, actualResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

//7. PFMERGE mykey4 mykey mykey2
response = lightClientRequest.SendCommandChunks("PFMERGE mykey4 mykey mykey2", bytesPerSend);
expectedResponse = "+OK\r\n";
actualResponse = Encoding.ASCII.GetString(response.AsSpan().Slice(0, expectedResponse.Length));
ClassicAssert.AreEqual(expectedResponse, actualResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

//8. PFCOUNT mykey4
response = lightClientRequest.SendCommandChunks("PFCOUNT mykey4", bytesPerSend);
expectedResponse = ":7\r\n";
actualResponse = Encoding.ASCII.GetString(response.AsSpan().Slice(0, expectedResponse.Length));
ClassicAssert.AreEqual(expectedResponse, actualResponse);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

private static unsafe ulong MurmurHash2x64A(byte* bString, int len, uint seed = 0)
Expand Down
3 changes: 1 addition & 2 deletions test/Garnet.test/Resp/ACL/SetUserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ public void PasswordlessDefaultUserTest()
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = "+default\r\n";
var response = lightClientRequest.SendCommand("ACL WHOAMI");
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);

// Correctness check
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

/// <summary>
Expand Down
33 changes: 11 additions & 22 deletions test/Garnet.test/RespAdminCommandsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public void PingTest()
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = "+PONG\r\n";
var response = lightClientRequest.SendCommand("PING");
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

[Test]
Expand All @@ -52,8 +51,7 @@ public void PingMessageTest()
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = "$5\r\nHELLO\r\n";
var response = lightClientRequest.SendCommand("PING HELLO");
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

[Test]
Expand All @@ -62,8 +60,7 @@ public void PingErrorMessageTest()
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = $"-{string.Format(CmdStrings.GenericErrWrongNumArgs, $"{nameof(RespCommand.PING)}")}\r\n";
var response = lightClientRequest.SendCommand("PING HELLO WORLD", 1);
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

[Test]
Expand All @@ -72,22 +69,19 @@ public void EchoWithNoMessageReturnErrorTest()
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = $"-{string.Format(CmdStrings.GenericErrWrongNumArgs, $"{nameof(RespCommand.ECHO)}")}\r\n";
var response = lightClientRequest.SendCommand("ECHO", 1);
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}


[Test]
public void EchoWithMessagesReturnErrorTest()
{
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = $"-{string.Format(CmdStrings.GenericErrWrongNumArgs, $"{nameof(RespCommand.ECHO)}")}\r\n";
var response = lightClientRequest.SendCommand("ECHO HELLO WORLD", 1);
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);

response = lightClientRequest.SendCommand("ECHO HELLO WORLD WORLD2", 1);
actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

[Test]
Expand All @@ -96,8 +90,7 @@ public void EchoWithMessageTest()
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = "$5\r\nHELLO\r\n";
var response = lightClientRequest.SendCommand("ECHO HELLO", 1);
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

[Test]
Expand All @@ -107,31 +100,27 @@ public void EchoTwoCommandsTest()
var wrongNumMessage = string.Format(CmdStrings.GenericErrWrongNumArgs, $"{nameof(RespCommand.ECHO)}");
var expectedResponse = $"-{wrongNumMessage}\r\n$5\r\nHELLO\r\n";
var response = lightClientRequest.SendCommands("ECHO HELLO WORLD WORLD2", "ECHO HELLO", 1, 1);
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}


[Test]
public void TimeCommandTest()
{
// this is an example, we just compare the length of the response with the expected one.
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = "*2\r\n$10\r\n1626282789\r\n$6\r\n621362\r\n";
var response = lightClientRequest.SendCommand("TIME", 3);
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
var actualValue = Encoding.ASCII.GetString(response, 0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse.Length, actualValue.Length);
}


[Test]
public void TimeWithReturnErrorTest()
{
using var lightClientRequest = TestUtils.CreateRequest();
var expectedResponse = $"-{string.Format(CmdStrings.GenericErrWrongNumArgs, nameof(RespCommand.TIME))}\r\n";
var response = lightClientRequest.SendCommand("TIME HELLO");
var actualValue = Encoding.ASCII.GetString(response).Substring(0, expectedResponse.Length);
ClassicAssert.AreEqual(expectedResponse, actualValue);
TestUtils.AssertEqualUpToExpectedLength(expectedResponse, response);
}

#endregion
Expand Down
Loading

0 comments on commit c831a20

Please sign in to comment.