Exemplo n.º 1
0
        void RetryExponentialServerBusyShouldSelfResetTest()
        {
            var retryExponential = (RetryExponential)RetryPolicy.Default;
            var retryCount       = 0;
            var duration         = Constants.DefaultOperationTimeout;
            var exception        = new ServerBusyException(string.Empty);

            // First ServerBusy exception
            Assert.False(retryExponential.IsServerBusy, "policy1.IsServerBusy should start with false");
            Assert.True(retryExponential.ShouldRetry(duration, retryCount, exception, out _), "We should retry, but it returned false");
            Assert.True(retryExponential.IsServerBusy, "policy1.IsServerBusy should be true");

            System.Threading.Thread.Sleep(3000);

            // Setting it a second time should not prolong the call.
            Assert.True(retryExponential.IsServerBusy, "policy1.IsServerBusy should be true");
            Assert.True(retryExponential.ShouldRetry(duration, retryCount, exception, out _), "We should retry, but it return false");
            Assert.True(retryExponential.IsServerBusy, "policy1.IsServerBusy should be true");

            System.Threading.Thread.Sleep(8000); // 3 + 8 = 11s
            Assert.False(retryExponential.IsServerBusy, "policy1.IsServerBusy should stay false after 11s");

            // Setting ServerBusy for second time.
            Assert.True(retryExponential.ShouldRetry(duration, retryCount, exception, out _), "We should retry, but it return false");
            Assert.True(retryExponential.IsServerBusy, "policy1.IsServerBusy is not true");
        }
        public async Task RetryExponentialServerBusyShouldSelfResetTest()
        {
            var retryExponential = (RetryExponential)RetryPolicy.Default;
            var retryCount       = 0;
            var attempts         = 0;
            var duration         = Constants.DefaultOperationTimeout;
            var exception        = new ServerBusyException(string.Empty);

            // First ServerBusy exception
            Assert.False(retryExponential.IsServerBusy, "policy1.IsServerBusy should start with false");
            Assert.True(retryExponential.ShouldRetry(duration, retryCount, exception, out _), "We should retry, but it returned false");
            Assert.True(retryExponential.IsServerBusy, "policy1.IsServerBusy should be true");

            await Task.Delay(3000);

            // Setting it a second time should not prolong the call.
            Assert.True(retryExponential.IsServerBusy, "policy1.IsServerBusy should be true");
            Assert.True(retryExponential.ShouldRetry(duration, retryCount, exception, out _), "We should retry, but it return false");
            Assert.True(retryExponential.IsServerBusy, "policy1.IsServerBusy should be true");

            // The server busy flag should reset; the basereset time is 10 seconds (see RetryPolicy, line 19).
            // Since there was already a 3 second delay, allow for the additional time needed to reset (with some padding).
            // This check is timing sensitive and has shown to behave differently in different build environments.  Allow for some timing
            // variation and extend the window for rest, if needed.
            await Task.Delay(9000);

            while (retryExponential.IsServerBusy && Interlocked.Increment(ref attempts) <= TestConstants.MaxAttemptsCount)
            {
                await Task.Delay(2000);
            }

            // If the busy flag hasn't reset by now, consider it a failure.
            Assert.False(retryExponential.IsServerBusy, "policy1.IsServerBusy should reset to false after 11s");

            // Setting ServerBusy for second time.
            Assert.True(retryExponential.ShouldRetry(duration, retryCount, exception, out _), "We should retry, but it return false");
            Assert.True(retryExponential.IsServerBusy, "policy1.IsServerBusy is not true");
        }