Skip to content

Commit

Permalink
Few more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TalZaccai committed May 28, 2024
1 parent df6cea8 commit f674749
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 62 deletions.
93 changes: 34 additions & 59 deletions libs/client/GarnetClientAPI/GarnetClientListCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Garnet.client
Expand All @@ -15,34 +14,24 @@ public sealed partial class GarnetClient
private static readonly Memory<byte> RPUSH = "$5\r\nRPUSH\r\n"u8.ToArray();

/// <summary>
/// Add the specified element to the head of the list stored at key.
/// Add the specified element to the head of the list stored at key
/// </summary>
/// <param name="key">The key of the list.</param>
/// <param name="element">The element to be added.</param>
/// <param name="callback">The callback function when operation completes.</param>
/// <param name="context">An optional context to correlate request to callback.</param>
/// <param name="key">The key of the list</param>
/// <param name="element">The element to be added</param>
/// <param name="callback">The callback function when operation completes</param>
/// <param name="context">An optional context to correlate request to callback</param>
public void ListLeftPush(string key, string element, Action<long, long, string> callback, long context = 0)
{
ArgumentNullException.ThrowIfNull(key);
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(callback);

var args = new List<Memory<byte>>
{
Encoding.ASCII.GetBytes(key),
Encoding.ASCII.GetBytes(element)
};

ExecuteForLongResult(callback, context, LPUSH, args);
ListLeftPush(key, new[] { element }, callback, context);
}

/// <summary>
/// Add the specified elements to the head of the list stored at key.
/// Add the specified elements to the head of the list stored at key
/// </summary>
/// <param name="key">The key of the list.</param>
/// <param name="elements">The elements to be added.</param>
/// <param name="callback">The callback function when operation completes.</param>
/// <param name="context">An optional context to correlate request to callback.</param>
/// <param name="key">The key of the list</param>
/// <param name="elements">The elements to be added</param>
/// <param name="callback">The callback function when operation completes</param>
/// <param name="context">An optional context to correlate request to callback</param>
public void ListLeftPush(string key, IEnumerable<string> elements, Action<long, long, string> callback, long context = 0)
{
ArgumentNullException.ThrowIfNull(key);
Expand All @@ -53,62 +42,50 @@ public void ListLeftPush(string key, IEnumerable<string> elements, Action<long,

if (arrElem.Length == 1)
{
throw new ArgumentException("The elements can't be empty.", nameof(elements));
throw new ArgumentException("Elements collection cannot be empty.", nameof(elements));
}

ExecuteForLongResult(callback, context, nameof(LPUSH), arrElem);
}

/// <summary>
/// Add the specified elements to the head of the list stored at key in asynchronous fashion.
/// Asynchronously add the specified elements to the head of the list stored at key
/// </summary>
/// <param name="key">The key of the list.</param>
/// <param name="elements">The elements to be added.</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <param name="key">The key of the list</param>
/// <param name="elements">The elements to be added</param>
/// <returns>The number of list elements after the addition</returns>
public async Task<long> ListLeftPushAsync(string key, params string[] elements)
{
ArgumentNullException.ThrowIfNull(key);
ArgumentNullException.ThrowIfNull(elements);

if (elements.Length == 0)
{
throw new ArgumentException("The elements can't be empty.", nameof(elements));
throw new ArgumentException("Elements collection cannot be empty.", nameof(elements));
}

return await ExecuteForLongResultAsync(nameof(LPUSH), [key, .. elements]);
}

/// <summary>
/// Add the specified element to the tail of the list stored at key.
/// Add the specified element to the tail of the list stored at key
/// </summary>
/// <param name="key">The key of the list.</param>
/// <param name="element">The element to be added.</param>
/// <param name="callback">The callback function when operation completes.</param>
/// <param name="context">An optional context to correlate request to callback.</param>
/// <param name="key">The key of the list</param>
/// <param name="element">The element to be added</param>
/// <param name="callback">The callback function when operation completes</param>
/// <param name="context">An optional context to correlate request to callback</param>
public void ListRightPush(string key, string element, Action<long, long, string> callback, long context = 0)
{
ArgumentNullException.ThrowIfNull(key);
ArgumentNullException.ThrowIfNull(element);
ArgumentNullException.ThrowIfNull(callback);

var args = new List<Memory<byte>>
{
Encoding.ASCII.GetBytes(key),
Encoding.ASCII.GetBytes(element)
};

ExecuteForLongResult(callback, context, RPUSH, args);
ListRightPush(key, new[] { element }, callback, context);
}

/// <summary>
/// Add the specified elements to the tail of the list stored at key.
/// Add the specified elements to the tail of the list stored at key
/// </summary>
/// <param name="key">The key of the list.</param>
/// <param name="elements">The elements to be added.</param>
/// <param name="callback">The callback function when operation completes.</param>
/// <param name="context">An optional context to correlate request to callback.</param>
/// <param name="key">The key of the list</param>
/// <param name="elements">The elements to be added</param>
/// <param name="callback">The callback function when operation completes</param>
/// <param name="context">An optional context to correlate request to callback</param>
public void ListRightPush(string key, IEnumerable<string> elements, Action<long, long, string> callback, long context = 0)
{
ArgumentNullException.ThrowIfNull(key);
Expand All @@ -119,28 +96,26 @@ public void ListRightPush(string key, IEnumerable<string> elements, Action<long,

if (arrElem.Length == 1)
{
throw new ArgumentException("The elements can't be empty.", nameof(elements));
throw new ArgumentException("Elements collection cannot be empty.", nameof(elements));
}

ExecuteForLongResult(callback, context, nameof(RPUSH), arrElem);
}

/// <summary>
/// Add the specified elements to the tail of the list stored at key in asynchronous fashion.
/// Asynchronously add the specified elements to the tail of the list stored at key
/// </summary>
/// <param name="key">The key of the list.</param>
/// <param name="elements">The elements to be added.</param>
/// <returns>The number of the list elements.</returns>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="ArgumentNullException"></exception>
/// <param name="key">The key of the list</param>
/// <param name="elements">The elements to be added</param>
/// <returns>The number of list elements after the addition</returns>
public async Task<long> ListRightPushAsync(string key, params string[] elements)
{
ArgumentNullException.ThrowIfNull(key);
ArgumentNullException.ThrowIfNull(elements);

if (elements.Length == 0)
{
throw new ArgumentException("The elements can't be empty.", nameof(elements));
throw new ArgumentException("Elements collection cannot be empty.", nameof(elements));
}

return await ExecuteForLongResultAsync(nameof(RPUSH), [key, .. elements]);
Expand Down
75 changes: 72 additions & 3 deletions test/Garnet.test/RespListGarnetClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ private static string GetTestKey(string key)

[Test]
[TestCaseSource(nameof(LeftPushTestCases))]

public void AddElementsToTheListHead_WithCallback(string key, string[] elements, string[] expectedList)
public void AddElementsToTheListHeadInBulk_WithCallback(string key, string[] elements, string[] expectedList)
{
// Arrange
using var db = new GarnetClient(TestUtils.Address, TestUtils.Port);
Expand Down Expand Up @@ -78,6 +77,41 @@ public void AddElementsToTheListHead_WithCallback(string key, string[] elements,
ValidateListContent(testKey, expectedList);
}

[Test]
[TestCaseSource(nameof(LeftPushTestCases))]
public void AddElementsToTheListHead_WithCallback(string key, string[] elements, string[] expectedList)
{
// Arrange
using var db = new GarnetClient(TestUtils.Address, TestUtils.Port);
db.Connect();

using ManualResetEventSlim e = new();

var isResultSet = false;
var actualListLength = 0L;

// Act & Assert
var testKey = GetTestKey(key);

foreach (var element in elements)
{
db.ListLeftPush(testKey, element, (_, returnValue, _) =>
{
actualListLength = returnValue;
isResultSet = true;
e.Set();
});

e.Wait();
e.Reset();
}

Assert.IsTrue(isResultSet);
Assert.AreEqual(expectedList.Length, actualListLength);

ValidateListContent(testKey, expectedList);
}

[Test]
[TestCaseSource(nameof(LeftPushTestCases))]
public async Task AddElementsToTheListHead_WithAsync(string key, string[] elements, string[] expectedList)
Expand All @@ -97,7 +131,7 @@ public async Task AddElementsToTheListHead_WithAsync(string key, string[] elemen

[Test]
[TestCaseSource(nameof(RightPushTestCases))]
public void AddElementsToListTail_WithCallback(string key, string[] elements, string[] expectedList)
public void AddElementsToListTailInBulk_WithCallback(string key, string[] elements, string[] expectedList)
{
// Arrange
using var db = new GarnetClient(TestUtils.Address, TestUtils.Port);
Expand Down Expand Up @@ -125,6 +159,41 @@ public void AddElementsToListTail_WithCallback(string key, string[] elements, st
ValidateListContent(testKey, expectedList);
}

[Test]
[TestCaseSource(nameof(RightPushTestCases))]
public void AddElementsToListTail_WithCallback(string key, string[] elements, string[] expectedList)
{
// Arrange
using var db = new GarnetClient(TestUtils.Address, TestUtils.Port);
db.Connect();

using ManualResetEventSlim e = new();

var isResultSet = false;
var actualListLength = 0L;

// Act & Assert
var testKey = GetTestKey(key);

foreach (var element in elements)
{
db.ListRightPush(testKey, element, (_, returnValue, _) =>
{
actualListLength = returnValue;
isResultSet = true;
e.Set();
});

e.Wait();
e.Reset();
}

Assert.IsTrue(isResultSet);
Assert.AreEqual(expectedList.Length, actualListLength);

ValidateListContent(testKey, expectedList);
}

[Test]
[TestCaseSource(nameof(RightPushTestCases))]
public async Task AddElementsToTheListTail_WithAsync(string key, string[] elements, string[] expectedList)
Expand Down

0 comments on commit f674749

Please sign in to comment.