Skip to content

Commit 15042a4

Browse files
author
Simon Ejsing
committed
renamed class and wired up the retry operation to the cache write with 6 retries and 31.5 seconds total timeout. Switched exception handling to catch AggregateException now since that is what is thrown in cache write timeout.
1 parent 0ad410c commit 15042a4

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

src/GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
<Compile Include="GitVersionContextTests.cs" />
115115
<Compile Include="Helpers\DirectoryHelper.cs" />
116116
<Compile Include="Mocks\MockThreadSleep.cs" />
117-
<Compile Include="RetryOperationExponentialBackoffTests.cs" />
117+
<Compile Include="OperationWithExponentialBackoffTests.cs" />
118118
<Compile Include="Init\InitScenarios.cs" />
119119
<Compile Include="Init\InitStepsDefaultResponsesDoNotThrow.cs" />
120120
<Compile Include="Init\TestConsole.cs" />

src/GitVersionCore.Tests/RetryOperationExponentialBackoffTests.cs renamed to src/GitVersionCore.Tests/OperationWithExponentialBackoffTests.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
using Shouldly;
66

77
[TestFixture]
8-
public class RetryOperationExponentialBackoffTests
8+
public class OperationWithExponentialBackoffTests
99
{
1010
[Test]
1111
public void RetryOperationThrowsWhenNegativeMaxRetries()
1212
{
13-
Action action = () => new RetryOperationExponentialBackoff<IOException>(new MockThreadSleep(), () => { }, -1);
13+
Action action = () => new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), () => { }, -1);
1414
action.ShouldThrow<ArgumentOutOfRangeException>();
1515
}
1616

1717
[Test]
1818
public void RetryOperationThrowsWhenThreadSleepIsNull()
1919
{
20-
Action action = () => new RetryOperationExponentialBackoff<IOException>(null, () => { });
20+
Action action = () => new OperationWithExponentialBackoff<IOException>(null, () => { });
2121
action.ShouldThrow<ArgumentNullException>();
2222
}
2323

@@ -29,7 +29,7 @@ public void OperationIsNotRetriedOnInvalidException()
2929
throw new Exception();
3030
};
3131

32-
var retryOperation = new RetryOperationExponentialBackoff<IOException>(new MockThreadSleep(), operation);
32+
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation);
3333
Action action = () => retryOperation.Execute();
3434
action.ShouldThrow<Exception>();
3535
}
@@ -48,7 +48,7 @@ public void OperationIsRetriedOnIOException()
4848
}
4949
};
5050

51-
var retryOperation = new RetryOperationExponentialBackoff<IOException>(new MockThreadSleep(), operation);
51+
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation);
5252
retryOperation.Execute();
5353

5454
operationCount.ShouldBe(2);
@@ -66,7 +66,7 @@ public void OperationIsRetriedAMaximumNumberOfTimes()
6666
throw new IOException();
6767
};
6868

69-
var retryOperation = new RetryOperationExponentialBackoff<IOException>(new MockThreadSleep(), operation, numberOfRetries);
69+
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), operation, numberOfRetries);
7070
Action action = () => retryOperation.Execute();
7171
action.ShouldThrow<AggregateException>();
7272

@@ -92,7 +92,7 @@ public void OperationDelayDoublesBetweenRetries()
9292
expectedSleepMSec *= 2;
9393
};
9494

95-
var retryOperation = new RetryOperationExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries);
95+
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries);
9696
Action action = () => retryOperation.Execute();
9797
action.ShouldThrow<AggregateException>();
9898

@@ -115,7 +115,7 @@ public void TotalSleepTimeForSixRetriesIsAboutThirtySeconds()
115115
totalSleep += u;
116116
};
117117

118-
var retryOperation = new RetryOperationExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries);
118+
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(validator), operation, numberOfRetries);
119119
Action action = () => retryOperation.Execute();
120120
action.ShouldThrow<AggregateException>();
121121

src/GitVersionCore/ExecuteCore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public VersionVariables ExecuteGitVersion(string targetUrl, string dynamicReposi
6262
{
6363
gitVersionCache.WriteVariablesToDiskCache(repo, dotGitDirectory, versionVariables);
6464
}
65-
catch (IOException e)
65+
catch (AggregateException e)
6666
{
67-
Logger.WriteWarning(string.Format("I/O exception during cache write:{0}{1}", Environment.NewLine, e));
67+
Logger.WriteWarning(string.Format("One or more exceptions during cache write:{0}{1}", Environment.NewLine, e));
6868
}
6969
}
7070

src/GitVersionCore/GitVersionCache.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,23 @@ public void WriteVariablesToDiskCache(IRepository repo, string gitDir, VersionVa
3030
dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
3131
}
3232

33-
using (var stream = fileSystem.OpenWrite(cacheFileName))
33+
Action writeCacheOperation = () =>
3434
{
35-
using (var sw = new StreamWriter(stream))
35+
using (var stream = fileSystem.OpenWrite(cacheFileName))
3636
{
37-
using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
37+
using (var sw = new StreamWriter(stream))
3838
{
39-
var serializer = new Serializer();
40-
serializer.Serialize(sw, dictionary);
39+
using (Logger.IndentLog("Storing version variables to cache file " + cacheFileName))
40+
{
41+
var serializer = new Serializer();
42+
serializer.Serialize(sw, dictionary);
43+
}
4144
}
4245
}
43-
}
46+
};
47+
48+
var retryOperation = new OperationWithExponentialBackoff<IOException>(new ThreadSleep(), writeCacheOperation, maxRetries: 6);
49+
retryOperation.Execute();
4450
}
4551

4652
public VersionVariables LoadVersionVariablesFromDiskCache(IRepository repo, string gitDir)

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
<Compile Include="Helpers\FileSystem.cs" />
124124
<Compile Include="Helpers\IFileSystem.cs" />
125125
<Compile Include="Helpers\IThreadSleep.cs" />
126-
<Compile Include="Helpers\RetryOperationExponentialBackoff.cs" />
126+
<Compile Include="Helpers\OperationWithExponentialBackoff.cs" />
127127
<Compile Include="Helpers\ServiceMessageEscapeHelper.cs" />
128128
<Compile Include="Helpers\ThreadSleep.cs" />
129129
<Compile Include="IncrementStrategyFinder.cs" />

src/GitVersionCore/Helpers/RetryOperationExponentialBackoff.cs renamed to src/GitVersionCore/Helpers/OperationWithExponentialBackoff.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
53

64
namespace GitVersion.Helpers
75
{
8-
internal class RetryOperationExponentialBackoff<T> where T : Exception
6+
internal class OperationWithExponentialBackoff<T> where T : Exception
97
{
108
private IThreadSleep ThreadSleep;
119
private Action Operation;
1210
private int MaxRetries;
1311

14-
public RetryOperationExponentialBackoff(IThreadSleep threadSleep, Action operation, int maxRetries = 5)
12+
public OperationWithExponentialBackoff(IThreadSleep threadSleep, Action operation, int maxRetries = 5)
1513
{
1614
if (threadSleep == null)
1715
throw new ArgumentNullException("threadSleep");

0 commit comments

Comments
 (0)