Код: Выделить всё
// Using Polly for retry logic
private readonly ResiliencePipeline _retryPipeline = new ResiliencePipelineBuilder { TimeProvider = timeProvider }
.AddRetry(new RetryStrategyOptions
{
ShouldHandle = new PredicateBuilder().Handle(),
Delay = TimeSpan.FromMilliseconds(_backoffFactor),
MaxRetryAttempts = _maxAttempts - 1,
// Linear backoff increases the delay each time by the backoff factor
BackoffType = DelayBackoffType.Linear,
OnRetry = onRetryArguments =>
{
logger.LogWarning(
"Failed to acquire lock. Retrying. {@LogContext}",
new { onRetryArguments });
return ValueTask.CompletedTask;
}
})
.Build();
Код: Выделить всё
// Attempt to store the lock with backoff retry
LockResult result = await _retryPipeline.ExecuteAsync(
async _ => await AttemptLockStorageAsync(lockId, expiryMilliseconds, attempts++),
cancellationTokenSource.Token);
Код: Выделить всё
// Act
Func func = async () =>
{
Task result = _distributedLockService.AcquireLockAsync(lockId);
for (int i = 1; i
Подробнее здесь: [url]https://stackoverflow.com/questions/77876331/why-is-task-delay1-necessary-to-advance-clock-when-unit-testing-with-net-time[/url]