mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-07-12 00:25:03 +01:00
9e8bdde30c
Now implement mocked `HttpMessageHandler`s as recommended: https://stackoverflow.com/a/36427274/3976486
54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using System;
|
|
using System.Net.Http;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
using Tgstation.Server.Api;
|
|
using Tgstation.Server.Client;
|
|
using Tgstation.Server.Common.Http;
|
|
|
|
namespace Tgstation.Server.Tests.Live
|
|
{
|
|
sealed class RateLimitRetryingApiClient : ApiClient
|
|
{
|
|
public RateLimitRetryingApiClient(
|
|
HttpClient httpClient,
|
|
Uri url,
|
|
ApiHeaders apiHeaders,
|
|
ApiHeaders tokenRefreshHeaders,
|
|
bool authless)
|
|
: base(
|
|
httpClient,
|
|
url,
|
|
apiHeaders,
|
|
tokenRefreshHeaders,
|
|
authless)
|
|
{
|
|
}
|
|
|
|
protected override async ValueTask<TResult> RunRequest<TResult>(string route, HttpContent content, HttpMethod method, long? instanceId, bool tokenRefresh, CancellationToken cancellationToken)
|
|
{
|
|
var hasGitHubToken = !String.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TGS_TEST_GITHUB_TOKEN"));
|
|
while (true)
|
|
try
|
|
{
|
|
return await base.RunRequest<TResult>(route, content, method, instanceId, tokenRefresh, cancellationToken);
|
|
}
|
|
catch (RateLimitException ex) when (hasGitHubToken && ex.RetryAfter.HasValue)
|
|
{
|
|
var now = DateTimeOffset.UtcNow;
|
|
|
|
Console.WriteLine($"TEST ERROR RATE LIMITED: {ex}");
|
|
if (!TestingUtils.RunningInGitHubActions)
|
|
Assert.Inconclusive("Rate limited by GitHub!");
|
|
|
|
var sleepTime = ex.RetryAfter.Value - now;
|
|
Console.WriteLine($"Sleeping for {sleepTime.TotalMinutes} minutes and retrying...");
|
|
await Task.Delay(sleepTime, cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
}
|