diff --git a/packages/Storage/Storage.Tests/Options/RetryTests.cs b/packages/Storage/Storage.Tests/Options/RetryTests.cs new file mode 100644 index 00000000..f490d3b1 --- /dev/null +++ b/packages/Storage/Storage.Tests/Options/RetryTests.cs @@ -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; + +/// +/// reaching the transport: metadata calls are retried, uploads are sent once. +/// +[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(); + 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) } }); +} diff --git a/packages/Storage/Storage/ClientOptions.cs b/packages/Storage/Storage/ClientOptions.cs index c4d53d16..40aa2cec 100644 --- a/packages/Storage/Storage/ClientOptions.cs +++ b/packages/Storage/Storage/ClientOptions.cs @@ -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; @@ -55,6 +56,7 @@ public class ClientOptions /// the resumable upload APIs for interrupted transfers instead. Default ( 0) /// sends once, unretried. /// + [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(); ///