单元测试睡眠持续时间 polly 等待重试策略

Unit test sleep durations polly wait retry policy

我想编写一个单元测试来查看执行是否在指定的持续时间内进入休眠状态。我遇到了 SystemClock,它是 Polly.Utilities 的一部分,但我正在寻找类似于此处提到的 Polly 单元测试的实现 WaitAndRetrySpecs,它看起来像这样

 [Fact]
 public void Should_sleep_for_the_specified_duration_each_retry_when_specified_exception_thrown_same_number_of_times_as_there_are_sleep_durations()
 {
      var totalTimeSlept = 0;

      var policy = Policy
            .Handle<DivideByZeroException>()
            .WaitAndRetry(new[]
            {
               1.Seconds(),
               2.Seconds(),
               3.Seconds()
            });

      SystemClock.Sleep = span => totalTimeSlept += span.Seconds;

      policy.RaiseException<DivideByZeroException>(3);

      totalTimeSlept.Should()
                      .Be(1 + 2 + 3);
  }

目前我的政策看起来像

var customPolicy = Policy
                     .Handle<SqlException>(x => IsTransientError(x))
                     .WaitAndRetryAsync(
                      3,
                      (retryAttempt) => getSleepDurationByRetryAtempt(retryAttempt)
                     );

我想测试政策的整体睡眠时间。对于每个重试尝试 [1,2,3],睡眠持续时间为 [1,2,3]。 重试 3 次后,总睡眠时间应为 1 + 2 + 3 = 6。此测试与上面 link 中提到的 Polly 规范非常相似。

问题:如何为 customPolicy 编写单元测试来测试类似于 polly 规范的总睡眠持续时间。我想查看编写单元测试的实现或说明。

您可以使用 onRetry 函数来实现。

为了简单起见,让我像这样定义 IsTransientErrorGetSleepDurationByRetryAttempt 方法:

public TimeSpan GetSleepDurationByRetryAttempt(int attempt) => TimeSpan.FromSeconds(attempt);
public bool IsTransientError(SqlException ex) => true;

顺便说一句,您可以通过避免(不必要的)匿名 lambda 来缩短策略定义:

var customPolicy = Policy
    .Handle<SqlException>(IsTransientError)
    .WaitAndRetryAsync(3, GetSleepDurationByRetryAttempt)

所以,回到onRetry。 an overload 具有以下签名:Action<Exception, TimeSpan, Context>。这里第二个参数是睡眠时长。

我们需要做的就是在这里提供一个函数,它可以累计睡眠时间。

var totalSleepDuration = TimeSpan.Zero;
...
onRetry: (ex, duration, ctx) => { totalSleepDuration = totalSleepDuration.Add(duration); }

让我们把所有这些放在一起:

[Fact]
public async Task GivenACustomSleepDurationProvider_WhenIUseItInARetryPolicy_ThenTheAccumulatedDurationIsAsExpected()
{
    //Arrange
    var totalSleepDuration = TimeSpan.Zero;
    var customPolicy = Policy
        .Handle<SqlException>(IsTransientError)
        .WaitAndRetryAsync(3, GetSleepDurationByRetryAttempt,
            onRetry: (ex, duration, ctx) => { totalSleepDuration = totalSleepDuration.Add(duration); }
        );

    //Act
    Func<Task> actionWithRetry = async() => await customPolicy.ExecuteAsync(() => throw new SqlException());
    
    //Assert
    _ = await Assert.ThrowsAsync<SqlException>(actionWithRetry);
    Assert.Equal(6, totalSleepDuration.Seconds);
}

更新 #1:减少延迟并引入理论

根据您的要求,运行 具有不同参数的相同测试用例可能有意义。这就是 TheoryInlineData 可以帮助您的地方:

[Theory]
[InlineData(3, 600)]
[InlineData(4, 1000)]
[InlineData(5, 1500)]
public async Task GivenACustomSleepDurationProvider_WhenIUseItInARetryPolicy_ThenTheAccumulatedDurationIsAsExpected(int retryCount, int expectedTotalSleepInMs)
{
    //Arrange
    var totalSleepDuration = TimeSpan.Zero;
    var customPolicy = Policy
        .Handle<SqlException>(IsTransientError)
        .WaitAndRetryAsync(retryCount, GetSleepDurationByRetryAttempt,
            onRetry: (ex, duration, ctx) => { totalSleepDuration = totalSleepDuration.Add(duration); }
        );

    //Act
    Func<Task> actionWithRetry = async () => await customPolicy.ExecuteAsync(() => throw new SqlException());

    //Assert
    _ = await Assert.ThrowsAsync<SqlException>(actionWithRetry);
    Assert.Equal(TimeSpan.FromMilliseconds(expectedTotalSleepInMs), totalSleepDuration);
}

public static TimeSpan GetSleepDurationByRetryAttempt(int attempt) => TimeSpan.FromMilliseconds(attempt * 100);

更新 #2:通过 Context

传递时间跨度

为了实现TimeSpana bit more type-safe的传输和检索,我们可以为此创建两个扩展方法:

public static class ContextExtensions
{
    private const string Accumulator = "DurationAccumulator";

    public static Context SetAccumulator(this Context context, TimeSpan durationAccumulator)
    {
        context[Accumulator] = durationAccumulator;
        return context;
    }

    public static TimeSpan? GetAccumulator(this Context context)
    {
        if (!context.TryGetValue(Accumulator, out var ts))
            return null;

        if (ts is TimeSpan accumulator) 
            return accumulator;

        return null;
    }
}

我们还可以提取策略创建逻辑:

private AsyncPolicy GetCustomPolicy(int retryCount)
    => Policy
        .Handle<SqlException>(IsTransientError)
        .WaitAndRetryAsync(retryCount, GetSleepDurationByRetryAttempt,
            onRetry: (ex, duration, ctx) =>
            {
                var totalSleepDuration = ctx.GetAccumulator();
                if (!totalSleepDuration.HasValue) return;
                totalSleepDuration = totalSleepDuration.Value.Add(duration);
                ctx.SetAccumulator(totalSleepDuration.Value);
            });

现在让我们把所有这些放在一起(再一次):

[Theory]
[InlineData(3, 600)]
[InlineData(4, 1000)]
[InlineData(5, 1500)]
public async Task GivenACustomSleepDurationProvider_WhenIUseItInARetryPolicy_ThenTheAccumulatedDurationIsAsExpected(
        int retryCount, int expectedTotalSleepInMs)
{
    //Arrange
    var totalSleepDuration = TimeSpan.Zero;
    var customPolicy = GetCustomPolicy(retryCount);
    var context = new Context().SetAccumulator(totalSleepDuration);

    //Act
    Func<Task> actionWithRetry = async () => await customPolicy.ExecuteAsync(ctx => throw new SqlException(), context);

    //Assert
    _ = await Assert.ThrowsAsync<SqlException>(actionWithRetry);
    var accumulator = context.GetAccumulator();
    Assert.NotNull(accumulator);
    Assert.Equal(TimeSpan.FromMilliseconds(expectedTotalSleepInMs), accumulator.Value);
}

测试没有进行,因为您使用的是 WaitAndRetryAsync 而不是 WaitAndRetry。我遇到了类似的问题并通过使用

解决了
 SystemClock.SleepAsync = (s, cts) =>
        {
            totalTimeSlept += s.Seconds;
            return Task.CompletedTask;
        };