|
15 | 15 |
|
16 | 16 | using System; |
17 | 17 | using System.Collections.Generic; |
| 18 | +using System.Linq; |
18 | 19 | using System.Net; |
| 20 | +using System.Reflection; |
| 21 | +using System.Threading; |
19 | 22 | using FluentAssertions; |
20 | 23 | using MongoDB.Bson; |
21 | 24 | using MongoDB.Bson.TestHelpers; |
22 | 25 | using MongoDB.Driver.Core.Bindings; |
23 | 26 | using MongoDB.Driver.Core.Clusters; |
| 27 | +using MongoDB.Driver.Core.Misc; |
24 | 28 | using MongoDB.Driver.Core.Operations; |
25 | 29 | using MongoDB.Driver.Core.Servers; |
| 30 | +using MongoDB.Driver.Core.TestHelpers; |
26 | 31 | using Moq; |
27 | 32 | using Xunit; |
28 | 33 |
|
@@ -85,6 +90,42 @@ public void IsOperationAcknowledged_should_return_expected_result(bool? isAcknow |
85 | 90 | result.Should().Be(expectedResult); |
86 | 91 | } |
87 | 92 |
|
| 93 | + [Theory] |
| 94 | + [InlineData(1, 50, 0, 50)] |
| 95 | + [InlineData(2, 50, 0, 100)] |
| 96 | + [InlineData(1, 10000, 0, 10000)] |
| 97 | + [InlineData(2, 20000, 0, 10000)] |
| 98 | + public void ShouldRetry_with_retryAfterMs_should_use_it_as_backoff_base( |
| 99 | + int attempt, |
| 100 | + int retryAfterMs, |
| 101 | + int expectedRangeMinMs, |
| 102 | + int expectedRangeMaxMs) |
| 103 | + { |
| 104 | + var context = CreateContext(retryRequested: true, areRetryableWritesSupported: true, hasSessionId: true, isInTransaction: false); |
| 105 | + var result = BsonDocument.Parse($"{{ ok : 0, code : 2, retryAfterMS : {retryAfterMs} }}"); |
| 106 | + var exception = CoreExceptionHelper.CreateMongoCommandExceptionWithLabels(result, "SystemOverloadedError", "RetryableError"); |
| 107 | + var operationContext = new OperationContext(null, CancellationToken.None); |
| 108 | + var randomMock = new Mock<IRandom>(); |
| 109 | + randomMock.Setup(r => r.NextDouble()).Returns(1.0); |
| 110 | + |
| 111 | + var didRetry = RetryableWriteOperationExecutorReflector.ShouldRetry( |
| 112 | + operationContext, |
| 113 | + errorDuringChannelAcquisition: false, |
| 114 | + context.ChannelSource.ServerDescription, |
| 115 | + WriteConcern.Acknowledged, |
| 116 | + context, |
| 117 | + exception, |
| 118 | + attempt, |
| 119 | + randomMock.Object, |
| 120 | + isEndTransactionOperation: false, |
| 121 | + isOperationRetryable: true, |
| 122 | + overloadErrorSeen: false, |
| 123 | + out var backoff); |
| 124 | + |
| 125 | + didRetry.Should().BeTrue(); |
| 126 | + backoff.TotalMilliseconds.Should().BeInRange(expectedRangeMinMs, expectedRangeMaxMs); |
| 127 | + } |
| 128 | + |
88 | 129 | // private methods |
89 | 130 | private IWriteBinding CreateBinding(bool areRetryableWritesSupported, bool hasSessionId, bool isInTransaction) |
90 | 131 | { |
@@ -144,5 +185,37 @@ public static bool DoesContextAllowRetries(OperationContext operationContext, Re |
144 | 185 |
|
145 | 186 | public static bool IsOperationAcknowledged(WriteConcern writeConcern) |
146 | 187 | => (bool)Reflector.InvokeStatic(typeof(RetryableWriteOperationExecutor), nameof(IsOperationAcknowledged), writeConcern); |
| 188 | + |
| 189 | + public static bool ShouldRetry( |
| 190 | + OperationContext operationContext, |
| 191 | + bool errorDuringChannelAcquisition, |
| 192 | + ServerDescription server, |
| 193 | + WriteConcern writeConcern, |
| 194 | + RetryableWriteContext context, |
| 195 | + Exception exception, |
| 196 | + int attempt, |
| 197 | + IRandom random, |
| 198 | + bool isEndTransactionOperation, |
| 199 | + bool isOperationRetryable, |
| 200 | + bool overloadErrorSeen, |
| 201 | + out TimeSpan backoff) |
| 202 | + { |
| 203 | + var methodInfo = typeof(RetryableWriteOperationExecutor) |
| 204 | + .GetMethods(BindingFlags.NonPublic | BindingFlags.Static) |
| 205 | + .Single(m => m.Name == nameof(ShouldRetry) && m.GetParameters().Length == 12); |
| 206 | + |
| 207 | + var args = new object[] { operationContext, errorDuringChannelAcquisition, server, writeConcern, context, exception, attempt, random, isEndTransactionOperation, isOperationRetryable, overloadErrorSeen, default(TimeSpan) }; |
| 208 | + |
| 209 | + try |
| 210 | + { |
| 211 | + var result = (bool)methodInfo.Invoke(null, args); |
| 212 | + backoff = (TimeSpan)args[11]; |
| 213 | + return result; |
| 214 | + } |
| 215 | + catch (TargetInvocationException ex) |
| 216 | + { |
| 217 | + throw ex.InnerException; |
| 218 | + } |
| 219 | + } |
147 | 220 | } |
148 | 221 | } |
0 commit comments