Skip to content

Commit 9d114ab

Browse files
committed
replaced MockThreadSleep with Substitute.For
1 parent 3ab17e9 commit 9d114ab

File tree

2 files changed

+17
-44
lines changed

2 files changed

+17
-44
lines changed

src/GitVersionCore.Tests/Mocks/MockThreadSleep.cs

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/GitVersionCore.Tests/OperationWithExponentialBackoffTests.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
using System;
22
using System.IO;
33
using System.Threading.Tasks;
4+
using GitVersion;
45
using GitVersion.Helpers;
56
using GitVersion.Logging;
67
using GitVersionCore.Tests.Helpers;
7-
using GitVersionCore.Tests.Mocks;
88
using Microsoft.Extensions.DependencyInjection;
9+
using NSubstitute;
910
using NUnit.Framework;
1011
using Shouldly;
1112

@@ -15,17 +16,19 @@ namespace GitVersionCore.Tests
1516
public class OperationWithExponentialBackoffTests : TestBase
1617
{
1718
private ILog log;
19+
private IThreadSleep threadSleep;
1820

1921
public OperationWithExponentialBackoffTests()
2022
{
2123
var sp = ConfigureServices();
2224
log = sp.GetService<ILog>();
25+
threadSleep = Substitute.For<IThreadSleep>();
2326
}
2427

2528
[Test]
2629
public void RetryOperationThrowsWhenNegativeMaxRetries()
2730
{
28-
Action action = () => new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), log, () => { }, -1);
31+
Action action = () => new OperationWithExponentialBackoff<IOException>(threadSleep, log, () => { }, -1);
2932
action.ShouldThrow<ArgumentOutOfRangeException>();
3033
}
3134

@@ -44,7 +47,7 @@ void Operation()
4447
throw new Exception();
4548
}
4649

47-
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), log, Operation);
50+
var retryOperation = new OperationWithExponentialBackoff<IOException>(threadSleep, log, Operation);
4851
var action = retryOperation.ExecuteAsync();
4952
await action.ShouldThrowAsync<Exception>();
5053
}
@@ -63,7 +66,7 @@ void Operation()
6366
}
6467
}
6568

66-
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), log, Operation);
69+
var retryOperation = new OperationWithExponentialBackoff<IOException>(threadSleep, log, Operation);
6770
await retryOperation.ExecuteAsync();
6871

6972
operationCount.ShouldBe(2);
@@ -81,7 +84,7 @@ void Operation()
8184
throw new IOException();
8285
}
8386

84-
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(), log, Operation, numberOfRetries);
87+
var retryOperation = new OperationWithExponentialBackoff<IOException>(threadSleep, log, Operation, numberOfRetries);
8588
var action = retryOperation.ExecuteAsync();
8689
await action.ShouldThrowAsync<AggregateException>();
8790

@@ -95,7 +98,7 @@ public async Task OperationDelayDoublesBetweenRetries()
9598
var expectedSleepMSec = 500;
9699
var sleepCount = 0;
97100

98-
void Operation() => throw new IOException();
101+
static void Operation() => throw new IOException();
99102

100103
Task Validator(int u)
101104
{
@@ -107,12 +110,12 @@ Task Validator(int u)
107110
});
108111
}
109112

110-
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(Validator), log, Operation, numberOfRetries);
113+
var mockThreadSleep = Substitute.For<IThreadSleep>();
114+
mockThreadSleep.SleepAsync(Arg.Any<int>()).Returns(x => Validator(x.Arg<int>()));
115+
var retryOperation = new OperationWithExponentialBackoff<IOException>(mockThreadSleep, log, Operation, numberOfRetries);
111116
var action = retryOperation.ExecuteAsync();
112117
await action.ShouldThrowAsync<AggregateException>();
113118

114-
// action.ShouldThrow<AggregateException>();
115-
116119
sleepCount.ShouldBe(numberOfRetries);
117120
}
118121

@@ -122,22 +125,16 @@ public async Task TotalSleepTimeForSixRetriesIsAboutThirtySecondsAsync()
122125
const int numberOfRetries = 6;
123126
var totalSleep = 0;
124127

125-
void Operation()
126-
{
127-
throw new IOException();
128-
}
128+
static void Operation() => throw new IOException();
129129

130-
Task Validator(int u)
131-
{
132-
return Task.Run(() => { totalSleep += u; });
133-
}
130+
Task Validator(int u) => Task.Run(() => { totalSleep += u; });
134131

135-
var retryOperation = new OperationWithExponentialBackoff<IOException>(new MockThreadSleep(Validator), log, Operation, numberOfRetries);
132+
var mockThreadSleep = Substitute.For<IThreadSleep>();
133+
mockThreadSleep.SleepAsync(Arg.Any<int>()).Returns(x => Validator(x.Arg<int>()));
134+
var retryOperation = new OperationWithExponentialBackoff<IOException>(mockThreadSleep, log, Operation, numberOfRetries);
136135

137136
var action = retryOperation.ExecuteAsync();
138137
await action.ShouldThrowAsync<AggregateException>();
139-
// Action action = () => retryOperation.ExecuteAsync();
140-
// action.ShouldThrow<AggregateException>();
141138

142139
// Exact number is 31,5 seconds
143140
totalSleep.ShouldBe(31500);

0 commit comments

Comments
 (0)