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

feat: add stub parameter to ListPush to truncate after size. #123

Merged
merged 1 commit into from
Aug 26, 2022
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
24 changes: 24 additions & 0 deletions IncubatingIntegrationTest/ListTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public async Task ListPushFrontFetch_ValueIsByteArray_RefreshTtl()
Assert.Equal(2, response.ByteArrayList!.Count);
}

[Fact]
public async Task ListPushFrontAsync_ValueIsByteArrayTruncateTailToSizeIsZero_ThrowsException()
{
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await client.ListPushFrontAsync("myCache", "listName", new byte[] { 0x00 }, false, truncateTailToSize: 0));
}

[Theory]
[InlineData(null, "my-list", "my-value")]
[InlineData("cache", null, "my-value")]
Expand Down Expand Up @@ -139,6 +145,12 @@ public async Task ListPushFrontFetch_ValueIsString_RefreshTtl()
Assert.Equal(2, response.StringList()!.Count);
}

[Fact]
public async Task ListPushFrontAsync_ValueIsStringTruncateTailToSizeIsZero_ThrowsException()
{
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await client.ListPushFrontAsync("myCache", "listName", "value", false, truncateTailToSize: 0));
eaddingtonwhite marked this conversation as resolved.
Show resolved Hide resolved
}

[Theory]
[InlineData(null, "my-list", new byte[] { 0x00 })]
[InlineData("cache", null, new byte[] { 0x00 })]
Expand Down Expand Up @@ -204,6 +216,12 @@ public async Task ListPushBackFetch_ValueIsByteArray_RefreshTtl()
Assert.Equal(2, response.ByteArrayList!.Count);
}

[Fact]
public async Task ListPushBackAsync_ValueIsByteArrayTruncateHeadToSizeIsZero_ThrowsException()
{
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await client.ListPushBackAsync("myCache", "listName", new byte[] { 0x00 }, false, truncateHeadToSize: 0));
}

[Theory]
[InlineData(null, "my-list", "my-value")]
[InlineData("cache", null, "my-value")]
Expand Down Expand Up @@ -269,6 +287,12 @@ public async Task ListPushBackFetch_ValueIsString_RefreshTtl()
Assert.Equal(2, response.StringList()!.Count);
}

[Fact]
public async Task ListPushBackAsync_ValueIsStringTruncateHeadToSizeIsZero_ThrowsException()
{
await Assert.ThrowsAsync<ArgumentOutOfRangeException>(async () => await client.ListPushBackAsync("myCache", "listName", "value", false, truncateHeadToSize: 0));
}

[Theory]
[InlineData(null, "my-list")]
[InlineData("cache", null)]
Expand Down
20 changes: 16 additions & 4 deletions Momento/Incubating/SimpleCacheClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,16 @@ public async Task<CacheSetDeleteResponse> SetDeleteAsync(string cacheName, strin
/// <param name="value">The value to push to the front of the list.</param>
/// <param name="refreshTtl">Update `listName`'s TTL if it already exists.</param>
/// <param name="ttlSeconds">TTL for the list in cache. This TTL takes precedence over the TTL used when initializing a cache client. Defaults to client TTL.</param>
/// <param name="truncateTailToSize">Ensure the list does not exceed this length. Remove excess from tail of list. Must be a positive number.</param>
/// <returns>Task representing the result of the push operation.</returns>
/// <exception cref="ArgumentNullException">Any of `cacheName` or `listName` or `value` is `null`.</exception>
public async Task<CacheListPushFrontResponse> ListPushFrontAsync(string cacheName, string listName, byte[] value, bool refreshTtl, uint? ttlSeconds = null)
/// <exception cref="ArgumentOutOfRangeException">`truncateTailToSize` is zero.</exception>
public async Task<CacheListPushFrontResponse> ListPushFrontAsync(string cacheName, string listName, byte[] value, bool refreshTtl, uint? ttlSeconds = null, uint? truncateTailToSize = null)
{
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(listName, nameof(listName));
Utils.ArgumentNotNull(value, nameof(value));
Utils.ArgumentStrictlyPositive(truncateTailToSize, nameof(truncateTailToSize));

return await this.dataClient.ListPushFrontAsync(cacheName, listName, value, refreshTtl, ttlSeconds);
}
Expand All @@ -697,13 +700,16 @@ public async Task<CacheListPushFrontResponse> ListPushFrontAsync(string cacheNam
/// <param name="value">The value to push to the front of the list.</param>
/// <param name="refreshTtl">Update `listName`'s TTL if it already exists.</param>
/// <param name="ttlSeconds">TTL for the list in cache. This TTL takes precedence over the TTL used when initializing a cache client. Defaults to client TTL.</param>
/// <param name="truncateTailToSize">Ensure the list does not exceed this length. Remove excess from tail of list. Must be a positive number.</param>
/// <returns>Task representing the result of the push operation.</returns>
/// <exception cref="ArgumentNullException">Any of `cacheName` or `listName` or `value` is `null`.</exception>
public async Task<CacheListPushFrontResponse> ListPushFrontAsync(string cacheName, string listName, string value, bool refreshTtl, uint? ttlSeconds = null)
/// <exception cref="ArgumentOutOfRangeException">`truncateTailToSize` is zero.</exception>
public async Task<CacheListPushFrontResponse> ListPushFrontAsync(string cacheName, string listName, string value, bool refreshTtl, uint? ttlSeconds = null, uint? truncateTailToSize = null)
{
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(listName, nameof(listName));
Utils.ArgumentNotNull(value, nameof(value));
Utils.ArgumentStrictlyPositive(truncateTailToSize, nameof(truncateTailToSize));

return await this.dataClient.ListPushFrontAsync(cacheName, listName, value, refreshTtl, ttlSeconds);
}
Expand All @@ -720,13 +726,16 @@ public async Task<CacheListPushFrontResponse> ListPushFrontAsync(string cacheNam
/// <param name="value">The value to push to the back of the list.</param>
/// <param name="refreshTtl">Update `listName`'s TTL if it already exists.</param>
/// <param name="ttlSeconds">TTL for the list in cache. This TTL takes precedence over the TTL used when initializing a cache client. Defaults to client TTL.</param>
/// <param name="truncateHeadToSize">Ensure the list does not exceed this length. Remove excess from head of list. Must be a positive number.</param>
/// <returns>Task representing the result of the push operation.</returns>
/// <exception cref="ArgumentNullException">Any of `cacheName` or `listName` or `value` is `null`.</exception>
public async Task<CacheListPushBackResponse> ListPushBackAsync(string cacheName, string listName, byte[] value, bool refreshTtl, uint? ttlSeconds = null)
/// <exception cref="ArgumentOutOfRangeException">`truncateHeadToSize` is zero.</exception>
public async Task<CacheListPushBackResponse> ListPushBackAsync(string cacheName, string listName, byte[] value, bool refreshTtl, uint? ttlSeconds = null, uint? truncateHeadToSize = null)
{
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(listName, nameof(listName));
Utils.ArgumentNotNull(value, nameof(value));
Utils.ArgumentStrictlyPositive(truncateHeadToSize, nameof(truncateHeadToSize));

return await this.dataClient.ListPushBackAsync(cacheName, listName, value, refreshTtl, ttlSeconds);
}
Expand All @@ -743,13 +752,16 @@ public async Task<CacheListPushBackResponse> ListPushBackAsync(string cacheName,
/// <param name="value">The value to push to the back of the list.</param>
/// <param name="refreshTtl">Update `listName`'s TTL if it already exists.</param>
/// <param name="ttlSeconds">TTL for the list in cache. This TTL takes precedence over the TTL used when initializing a cache client. Defaults to client TTL.</param>
/// <param name="truncateHeadToSize">Ensure the list does not exceed this length. Remove excess from head of list. Must be a positive number.</param>
/// <returns>Task representing the result of the push operation.</returns>
/// <exception cref="ArgumentNullException">Any of `cacheName` or `listName` or `value` is `null`.</exception>
public async Task<CacheListPushBackResponse> ListPushBackAsync(string cacheName, string listName, string value, bool refreshTtl, uint? ttlSeconds = null)
/// <exception cref="ArgumentOutOfRangeException">`truncateHeadToSize` is zero.</exception>
public async Task<CacheListPushBackResponse> ListPushBackAsync(string cacheName, string listName, string value, bool refreshTtl, uint? ttlSeconds = null, uint? truncateHeadToSize = null)
{
Utils.ArgumentNotNull(cacheName, nameof(cacheName));
Utils.ArgumentNotNull(listName, nameof(listName));
Utils.ArgumentNotNull(value, nameof(value));
Utils.ArgumentStrictlyPositive(truncateHeadToSize, nameof(truncateHeadToSize));

return await this.dataClient.ListPushBackAsync(cacheName, listName, value, refreshTtl, ttlSeconds);
}
Expand Down
14 changes: 14 additions & 0 deletions Momento/Internal/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,20 @@ public static void ElementsNotNull<T>(IEnumerable<T> argument, string paramName)
}
}

/// <summary>
/// Throw an exception if the argument is zero.
/// </summary>
/// <param name="argument">The integer to zero test.</param>
/// <param name="paramName">Name of the integer to propagate to the exception.</param>
/// <exception cref="ArgumentOutOfRangeException">`argument` is zero.</exception>
public static void ArgumentStrictlyPositive(uint? argument, string paramName)
{
if (argument == 0)
{
throw new ArgumentOutOfRangeException(paramName, "Number must be strictly positive.");
}
}

/// <summary>
/// Defines methods to support comparing containers of reference items by their
/// contents (structure) instead of by reference.
Expand Down