|
| 1 | +using System.Data; |
| 2 | +using Xunit; |
| 3 | +using Ydb.Sdk.Ado.Retry; |
| 4 | + |
| 5 | +namespace Ydb.Sdk.Ado.Tests; |
| 6 | + |
| 7 | +public class DefaultRetryPolicyTests : TestBase |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void GetDelay_WhenAttemptOne_UsesDefaultDelayDelegate() |
| 11 | + { |
| 12 | + var config = new RetryConfig |
| 13 | + { |
| 14 | + DefaultDelay = (_, __) => TimeSpan.FromMilliseconds(100), |
| 15 | + MaxDelay = TimeSpan.FromMilliseconds(500) |
| 16 | + }; |
| 17 | + |
| 18 | + var policy = new DefaultRetryPolicy(config); |
| 19 | + |
| 20 | + var delay = policy.GetDelay(new Exception("test"), 1); |
| 21 | + Assert.Equal(TimeSpan.FromMilliseconds(100), delay); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public void GetDelay_WhenAttemptOne_ReturnsBaseDelay_NoJitter() |
| 26 | + { |
| 27 | + var config = new RetryConfig |
| 28 | + { |
| 29 | + BaseDelay = TimeSpan.FromMilliseconds(100), |
| 30 | + Exponent = 2.0, |
| 31 | + JitterFraction = 0.0, |
| 32 | + MaxDelay = TimeSpan.FromMilliseconds(500), |
| 33 | + DefaultDelay = (ex, attempt) => |
| 34 | + { |
| 35 | + attempt = Math.Max(1, attempt); |
| 36 | + var baseMs = 100.0 * Math.Pow(2.0, attempt - 1); |
| 37 | + var ms = Math.Min(baseMs, 500.0); |
| 38 | + return TimeSpan.FromMilliseconds(ms); |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + var policy = new DefaultRetryPolicy(config); |
| 43 | + |
| 44 | + var delay = policy.GetDelay(new Exception("test"), 1); |
| 45 | + Assert.Equal(TimeSpan.FromMilliseconds(100), delay); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void GetDelay_WhenStatusHasOverride_ReturnsPerStatusDelay() |
| 50 | + { |
| 51 | + var config = new RetryConfig(); |
| 52 | + config.PerStatusDelay[StatusCode.Unavailable] = attempt => TimeSpan.FromMilliseconds(123); |
| 53 | + var policy = new DefaultRetryPolicy(config); |
| 54 | + |
| 55 | + var ex = new YdbException(StatusCode.Unavailable, "unavailable"); |
| 56 | + var delay = policy.GetDelay(ex, 2); |
| 57 | + |
| 58 | + Assert.Equal(TimeSpan.FromMilliseconds(123), delay); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void CanRetry_WhenTransientYdbException_ReturnsTrue() |
| 63 | + { |
| 64 | + var config = new RetryConfig(); |
| 65 | + var policy = new DefaultRetryPolicy(config); |
| 66 | + var ex = new YdbException(StatusCode.Aborted, "transient"); |
| 67 | + |
| 68 | + Assert.True(policy.CanRetry(ex, isIdempotent: false)); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void CanRetry_WhenOverloaded_RetriesRegardlessOfIdempotency() |
| 73 | + { |
| 74 | + var policy = new DefaultRetryPolicy(new RetryConfig()); |
| 75 | + var ex = new YdbException(StatusCode.Overloaded, "overloaded"); |
| 76 | + |
| 77 | + Assert.True(policy.CanRetry(ex, isIdempotent: false)); |
| 78 | + Assert.True(policy.CanRetry(ex, isIdempotent: true)); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void CanRetry_WhenTimeoutException_ReturnsTrue() |
| 83 | + { |
| 84 | + var config = new RetryConfig(); |
| 85 | + var policy = new DefaultRetryPolicy(config); |
| 86 | + |
| 87 | + Assert.True(policy.CanRetry(new TimeoutException(), isIdempotent: true)); |
| 88 | + } |
| 89 | + |
| 90 | + [Fact] |
| 91 | + public void CanRetry_WhenUserCancelled_ReturnsFalse() |
| 92 | + { |
| 93 | + using var cts = new CancellationTokenSource(); |
| 94 | + cts.Cancel(); |
| 95 | + var config = new RetryConfig(); |
| 96 | + var policy = new DefaultRetryPolicy(config); |
| 97 | + |
| 98 | + var ex = new OperationCanceledException(cts.Token); |
| 99 | + Assert.False(policy.CanRetry(ex, isIdempotent: true)); |
| 100 | + } |
| 101 | + |
| 102 | + [Fact] |
| 103 | + public void IsStreaming_WhenSequentialAccess_ReturnsTrue() |
| 104 | + { |
| 105 | + var config = new RetryConfig(); |
| 106 | + var policy = new DefaultRetryPolicy(config); |
| 107 | + var cmd = new DummyCommand(); |
| 108 | + |
| 109 | + Assert.True(policy.IsStreaming(cmd, CommandBehavior.SequentialAccess)); |
| 110 | + } |
| 111 | + |
| 112 | + [Fact] |
| 113 | + public void IsStreaming_WhenCustomConfigDelegateUsed_ReturnsTrue() |
| 114 | + { |
| 115 | + var config = new RetryConfig { IsStreaming = (c, b) => true }; |
| 116 | + var policy = new DefaultRetryPolicy(config); |
| 117 | + var cmd = new DummyCommand(); |
| 118 | + |
| 119 | + Assert.True(policy.IsStreaming(cmd, CommandBehavior.Default)); |
| 120 | + } |
| 121 | + |
| 122 | + [Fact] |
| 123 | + public void GetDelay_WhenDelayExceedsMaxDelay_IsCappedToMaxDelay() |
| 124 | + { |
| 125 | + var config = new RetryConfig |
| 126 | + { |
| 127 | + MaxDelay = TimeSpan.FromMilliseconds(500), |
| 128 | + DefaultDelay = (_, __) => TimeSpan.FromMilliseconds(1000) |
| 129 | + }; |
| 130 | + var policy = new DefaultRetryPolicy(config); |
| 131 | + |
| 132 | + var delay = policy.GetDelay(new Exception("test"), 1); |
| 133 | + |
| 134 | + Assert.Equal(TimeSpan.FromMilliseconds(500), delay); |
| 135 | + } |
| 136 | + |
| 137 | + [Fact] |
| 138 | + public void CanRetry_WhenOperationCanceledWithoutToken_ReturnsTrue() |
| 139 | + { |
| 140 | + var config = new RetryConfig(); |
| 141 | + var policy = new DefaultRetryPolicy(config); |
| 142 | + |
| 143 | + var ex = new OperationCanceledException(); // токен не передаём |
| 144 | + Assert.True(policy.CanRetry(ex, isIdempotent: true)); |
| 145 | + } |
| 146 | + |
| 147 | + private class DummyCommand : System.Data.Common.DbCommand |
| 148 | + { |
| 149 | + public override string CommandText { get; set; } = string.Empty; |
| 150 | + public override int CommandTimeout { get; set; } |
| 151 | + public override CommandType CommandType { get; set; } = CommandType.Text; |
| 152 | + protected override System.Data.Common.DbConnection DbConnection { get; set; } |
| 153 | + protected override System.Data.Common.DbParameterCollection DbParameterCollection { get; } = null!; |
| 154 | + protected override System.Data.Common.DbTransaction DbTransaction { get; set; } |
| 155 | + public override bool DesignTimeVisible { get; set; } |
| 156 | + public override UpdateRowSource UpdatedRowSource { get; set; } |
| 157 | + public override void Cancel() { } |
| 158 | + public override int ExecuteNonQuery() => 0; |
| 159 | + public override object ExecuteScalar() => null!; |
| 160 | + public override void Prepare() { } |
| 161 | + protected override System.Data.Common.DbParameter CreateDbParameter() => throw new NotImplementedException(); |
| 162 | + protected override System.Data.Common.DbDataReader ExecuteDbDataReader(CommandBehavior behavior) => throw new NotImplementedException(); |
| 163 | + } |
| 164 | +} |
0 commit comments