|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using GitVersion.Helpers; |
| 4 | +using NUnit.Framework; |
| 5 | +using Shouldly; |
| 6 | + |
| 7 | +[TestFixture] |
| 8 | +public class OperationWithExponentialBackoffTests |
| 9 | +{ |
| 10 | + [Test] |
| 11 | + public void RetryOperationThrowsWhenNegativeMaxRetries() |
| 12 | + { |
| 13 | + Action action = () => new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), () => { }, -1); |
| 14 | + action.ShouldThrow<ArgumentOutOfRangeException>(); |
| 15 | + } |
| 16 | + |
| 17 | + [Test] |
| 18 | + public void RetryOperationThrowsWhenThreadSleepIsNull() |
| 19 | + { |
| 20 | + Action action = () => new OperationWithExponentialBackoff<IOException>(null, () => { }); |
| 21 | + action.ShouldThrow<ArgumentNullException>(); |
| 22 | + } |
| 23 | + |
| 24 | + [Test] |
| 25 | + public void OperationIsNotRetriedOnInvalidException() |
| 26 | + { |
| 27 | + Action operation = () => |
| 28 | + { |
| 29 | + throw new Exception(); |
| 30 | + }; |
| 31 | + |
| 32 | + var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation); |
| 33 | + Action action = () => retryOperation.Execute(); |
| 34 | + action.ShouldThrow<Exception>(); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public void OperationIsRetriedOnIOException() |
| 39 | + { |
| 40 | + var operationCount = 0; |
| 41 | + |
| 42 | + Action operation = () => |
| 43 | + { |
| 44 | + operationCount++; |
| 45 | + if (operationCount < 2) |
| 46 | + { |
| 47 | + throw new IOException(); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation); |
| 52 | + retryOperation.Execute(); |
| 53 | + |
| 54 | + operationCount.ShouldBe(2); |
| 55 | + } |
| 56 | + |
| 57 | + [Test] |
| 58 | + public void OperationIsRetriedAMaximumNumberOfTimes() |
| 59 | + { |
| 60 | + const int numberOfRetries = 3; |
| 61 | + var operationCount = 0; |
| 62 | + |
| 63 | + Action operation = () => |
| 64 | + { |
| 65 | + operationCount++; |
| 66 | + throw new IOException(); |
| 67 | + }; |
| 68 | + |
| 69 | + var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation, numberOfRetries); |
| 70 | + Action action = () => retryOperation.Execute(); |
| 71 | + action.ShouldThrow<AggregateException>(); |
| 72 | + |
| 73 | + operationCount.ShouldBe(numberOfRetries + 1); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void OperationDelayDoublesBetweenRetries() |
| 78 | + { |
| 79 | + const int numberOfRetries = 3; |
| 80 | + var expectedSleepMSec = 500; |
| 81 | + var sleepCount = 0; |
| 82 | + |
| 83 | + Action operation = () => |
| 84 | + { |
| 85 | + throw new IOException(); |
| 86 | + }; |
| 87 | + |
| 88 | + Action<int> validator = u => |
| 89 | + { |
| 90 | + sleepCount++; |
| 91 | + u.ShouldBe(expectedSleepMSec); |
| 92 | + expectedSleepMSec *= 2; |
| 93 | + }; |
| 94 | + |
| 95 | + var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries); |
| 96 | + Action action = () => retryOperation.Execute(); |
| 97 | + action.ShouldThrow<AggregateException>(); |
| 98 | + |
| 99 | + sleepCount.ShouldBe(numberOfRetries); |
| 100 | + } |
| 101 | + |
| 102 | + [Test] |
| 103 | + public void TotalSleepTimeForSixRetriesIsAboutThirtySeconds() |
| 104 | + { |
| 105 | + const int numberOfRetries = 6; |
| 106 | + int totalSleep = 0; |
| 107 | + |
| 108 | + Action operation = () => |
| 109 | + { |
| 110 | + throw new IOException(); |
| 111 | + }; |
| 112 | + |
| 113 | + Action<int> validator = u => |
| 114 | + { |
| 115 | + totalSleep += u; |
| 116 | + }; |
| 117 | + |
| 118 | + var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries); |
| 119 | + Action action = () => retryOperation.Execute(); |
| 120 | + action.ShouldThrow<AggregateException>(); |
| 121 | + |
| 122 | + // Exact number is 31,5 seconds |
| 123 | + totalSleep.ShouldBe(31500); |
| 124 | + } |
| 125 | +} |
0 commit comments