This commit is contained in:
jimmy committed 2025-11-14 16:56:57 +08:00
1 parent 237cbd8788
commit ab3721912d
102 files changed
+7929 -20

No files matched your search

@@ -0,0 +1,48 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using NetClient.Abstractions;
using NetClient.DependencyInjection;
namespace NetClient.Test.Tests;
[TestClass]
public class NetClientRetryTests
{
[TestMethod]
public async Task Retry_On_429_Then_Succeed()
{
var services = new ServiceCollection();
var attempts = 0;
var builder = services.AddNetClient(opt =>
{
opt.BaseAddress = new Uri("https://retry.test");
opt.Retry.Enabled = true;
opt.Retry.MaxRetryAttempts = 2;
opt.Retry.FirstDelay = TimeSpan.FromMilliseconds(5);
opt.Retry.Handle429 = true;
});
builder.ConfigurePrimaryHttpMessageHandler(() => new TestDoubles.TestHttpMessageHandler(req =>
{
attempts++;
if (attempts == 1)
{
return Task.FromResult(TestDoubles.TestHttpMessageHandler.Text(HttpStatusCode.TooManyRequests, "busy"));
}
return Task.FromResult(TestDoubles.TestHttpMessageHandler.Text(HttpStatusCode.OK, "ok"));
}));
var sp = services.BuildServiceProvider();
var client = sp.GetRequiredService<IHttpClient>();
var result = await client.GetStringAsync("/ping");
Assert.AreEqual("ok", result);
Assert.AreEqual(2, attempts);
}
}