Skip to content
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
58 changes: 58 additions & 0 deletions packages/Storage/Storage.Tests/Options/RetryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Supabase.Core.Http;
using Supabase.Storage;
using Supabase.Storage.Exceptions;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;

namespace Storage.Tests.Options;

/// <summary>
/// <see cref="ClientOptions.Retry"/> reaching the transport: metadata calls are retried, uploads are sent once.
/// </summary>
[TestClass]
[TestCategory("Contract")]
public class RetryTests
{
private WireMockServer server = null!;

[TestInitialize]
public void SetUp() => this.server = WireMockServer.Start();

[TestCleanup]
public void TearDown() => this.server.Stop();

[TestMethod]
public async Task ListBuckets_ShouldRetryUntilSuccess_GivenRetryableStatus()
{
this.server.Given(Request.Create().WithPath("/bucket").UsingGet())
.InScenario("retry").WillSetStateTo("retried")
.RespondWith(Response.Create().WithStatusCode(503));
this.server.Given(Request.Create().WithPath("/bucket").UsingGet())
.InScenario("retry").WhenStateIs("retried")
.RespondWith(Response.Create().WithStatusCode(200).WithHeader("Content-Type", "application/json").WithBody("[{\"id\":\"a\",\"name\":\"a\"}]"));

var buckets = await this.RetryingClient().ListBuckets();

buckets.Should().ContainSingle().Which.Name.Should().Be("a");
}

[TestMethod]
public async Task Upload_ShouldNotRetry_GivenRetryOptions()
{
this.server.Given(Request.Create().WithPath("/object/photos/a.bin").UsingPost())
.RespondWith(Response.Create().WithStatusCode(503));

var act = () => this.RetryingClient().From("photos").Upload(new byte[] { 1 }, "a.bin");

await act.Should().ThrowAsync<SupabaseStorageException>();
this.server.LogEntries.Should().ContainSingle("an upload is sent once, its body is not replayed");
}

private Client RetryingClient() =>
new(this.server.Url!, new ClientOptions { Retry = new RetryOptions { MaxRetries = 1, BaseDelay = TimeSpan.FromMilliseconds(1) } });
}
2 changes: 2 additions & 0 deletions packages/Storage/Storage/ClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Supabase.Core.Attributes;
using Supabase.Core.Http;

namespace Supabase.Storage;
Expand Down Expand Up @@ -55,6 +56,7 @@ public class ClientOptions
/// the resumable upload APIs for interrupted transfers instead. Default (<see cref="RetryOptions.MaxRetries"/> 0)
/// sends once, unretried.
/// </summary>
[ImplementedCapability("storage.configuration.auto_retry", "Applied to bucket and object metadata calls, writes included; uploads and downloads are sent once.")]
public RetryOptions Retry { get; set; } = new RetryOptions();

/// <summary>
Expand Down