public void WriteVariablesToDiskCache(GitPreparer gitPreparer, GitVersionCacheKey cacheKey, VersionVariables variablesFromCache) { var cacheDir = PrepareCacheDirectory(gitPreparer); var cacheFileName = GetCacheFileName(cacheKey, cacheDir); variablesFromCache.FileName = cacheFileName; Dictionary <string, string> dictionary; using (Logger.IndentLog("Creating dictionary")) { dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value); } Action writeCacheOperation = () => { using (var stream = fileSystem.OpenWrite(cacheFileName)) { using (var sw = new StreamWriter(stream)) { using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName)) { var serializer = new Serializer(); serializer.Serialize(sw, dictionary); } } } }; var retryOperation = new OperationWithExponentialBackoff <IOException>(new ThreadSleep(), writeCacheOperation, maxRetries: 6); retryOperation.ExecuteAsync().Wait(); }
public async Task TotalSleepTimeForSixRetriesIsAboutThirtySecondsAsync() { const int numberOfRetries = 6; int totalSleep = 0; Action operation = () => { throw new IOException(); }; Func <int, Task> validator = u => { return(Task.Run(() => { totalSleep += u; })); }; var retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(validator), operation, numberOfRetries); Task action = retryOperation.ExecuteAsync(); await action.ShouldThrowAsync <AggregateException>(); // Action action = () => retryOperation.ExecuteAsync(); // action.ShouldThrow<AggregateException>(); // Exact number is 31,5 seconds totalSleep.ShouldBe(31500); }
public async Task OperationDelayDoublesBetweenRetries() { const int numberOfRetries = 3; var expectedSleepMSec = 500; var sleepCount = 0; Action operation = () => throw new IOException(); Func <int, Task> validator = u => { return(Task.Run(() => { sleepCount++; u.ShouldBe(expectedSleepMSec); expectedSleepMSec *= 2; })); }; var retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(validator), operation, numberOfRetries); Task action = retryOperation.ExecuteAsync(); await action.ShouldThrowAsync <AggregateException>(); // action.ShouldThrow<AggregateException>(); sleepCount.ShouldBe(numberOfRetries); }
public void Execute(VersionVariables variables, OutputContext context) { var gitVersionOptions = options.Value; if (gitVersionOptions.Output.Contains(OutputType.BuildServer)) { buildAgent?.WriteIntegration(console.WriteLine, variables, context.UpdateBuildNumber ?? true); } if (gitVersionOptions.Output.Contains(OutputType.File)) { var retryOperation = new OperationWithExponentialBackoff <IOException>(new ThreadSleep(), log, () => fileSystem.WriteAllText(context.OutputFile, variables.ToString())); retryOperation.ExecuteAsync().Wait(); } if (gitVersionOptions.Output.Contains(OutputType.Json)) { switch (gitVersionOptions.ShowVariable) { case null: console.WriteLine(variables.ToString()); break; default: if (!variables.TryGetValue(gitVersionOptions.ShowVariable, out var part)) { throw new WarningException($"'{gitVersionOptions.ShowVariable}' variable does not exist"); } console.WriteLine(part); break; } } }
public async Task TotalSleepTimeForSixRetriesIsAboutThirtySecondsAsync() { const int numberOfRetries = 6; var totalSleep = 0; void Operation() { throw new IOException(); } Task Validator(int u) { return(Task.Run(() => { totalSleep += u; })); } var retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(Validator), new NullLog(), Operation, numberOfRetries); var action = retryOperation.ExecuteAsync(); await action.ShouldThrowAsync <AggregateException>(); // Action action = () => retryOperation.ExecuteAsync(); // action.ShouldThrow<AggregateException>(); // Exact number is 31,5 seconds totalSleep.ShouldBe(31500); }
public async Task OperationIsNotRetriedOnInvalidException() { void Operation() { throw new Exception(); } var retryOperation = new OperationWithExponentialBackoff <IOException>(threadSleep, log, Operation); var action = retryOperation.ExecuteAsync(); await action.ShouldThrowAsync <Exception>(); }
public async Task OperationIsNotRetriedOnInvalidException() { Action operation = () => { throw new Exception(); }; var retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(), operation); Task action = retryOperation.ExecuteAsync(); await action.ShouldThrowAsync <Exception>(); }
public async Task OperationIsRetriedAMaximumNumberOfTimesAsync() { const int numberOfRetries = 3; var operationCount = 0; void Operation() { operationCount++; throw new IOException(); } var retryOperation = new OperationWithExponentialBackoff <IOException>(threadSleep, log, Operation, numberOfRetries); var action = retryOperation.ExecuteAsync(); await action.ShouldThrowAsync <AggregateException>(); operationCount.ShouldBe(numberOfRetries + 1); }
public async Task OperationIsRetriedOnIoException() { var operationCount = 0; void Operation() { operationCount++; if (operationCount < 2) { throw new IOException(); } } var retryOperation = new OperationWithExponentialBackoff <IOException>(threadSleep, log, Operation); await retryOperation.ExecuteAsync(); operationCount.ShouldBe(2); }
public async Task OperationIsRetriedOnIOException() { var operationCount = 0; Action operation = () => { operationCount++; if (operationCount < 2) { throw new IOException(); } }; var retryOperation = new OperationWithExponentialBackoff <IOException>(new MockThreadSleep(), operation); await retryOperation.ExecuteAsync(); operationCount.ShouldBe(2); }
public static VersionVariables FromFile(string filePath, IFileSystem fileSystem, ILog log) { try { if (log == null) { return(FromFileInternal(filePath, fileSystem)); } var retryOperation = new OperationWithExponentialBackoff <IOException, VersionVariables>(new ThreadSleep(), log, () => FromFileInternal(filePath, fileSystem)); var versionVariables = retryOperation.ExecuteAsync().Result; return(versionVariables); } catch (AggregateException ex) { var lastException = ex.InnerExceptions?.LastOrDefault() ?? ex.InnerException; if (lastException != null) { throw lastException; } throw; } }