1
1
using System ;
2
2
using System . IO ;
3
3
using System . Threading . Tasks ;
4
+ using GitVersion ;
4
5
using GitVersion . Helpers ;
5
6
using GitVersion . Logging ;
6
7
using GitVersionCore . Tests . Helpers ;
7
- using GitVersionCore . Tests . Mocks ;
8
8
using Microsoft . Extensions . DependencyInjection ;
9
+ using NSubstitute ;
9
10
using NUnit . Framework ;
10
11
using Shouldly ;
11
12
@@ -15,17 +16,19 @@ namespace GitVersionCore.Tests
15
16
public class OperationWithExponentialBackoffTests : TestBase
16
17
{
17
18
private ILog log ;
19
+ private IThreadSleep threadSleep ;
18
20
19
21
public OperationWithExponentialBackoffTests ( )
20
22
{
21
23
var sp = ConfigureServices ( ) ;
22
24
log = sp . GetService < ILog > ( ) ;
25
+ threadSleep = Substitute . For < IThreadSleep > ( ) ;
23
26
}
24
27
25
28
[ Test ]
26
29
public void RetryOperationThrowsWhenNegativeMaxRetries ( )
27
30
{
28
- Action action = ( ) => new OperationWithExponentialBackoff < IOException > ( new MockThreadSleep ( ) , log , ( ) => { } , - 1 ) ;
31
+ Action action = ( ) => new OperationWithExponentialBackoff < IOException > ( threadSleep , log , ( ) => { } , - 1 ) ;
29
32
action . ShouldThrow < ArgumentOutOfRangeException > ( ) ;
30
33
}
31
34
@@ -44,7 +47,7 @@ void Operation()
44
47
throw new Exception ( ) ;
45
48
}
46
49
47
- var retryOperation = new OperationWithExponentialBackoff < IOException > ( new MockThreadSleep ( ) , log , Operation ) ;
50
+ var retryOperation = new OperationWithExponentialBackoff < IOException > ( threadSleep , log , Operation ) ;
48
51
var action = retryOperation . ExecuteAsync ( ) ;
49
52
await action . ShouldThrowAsync < Exception > ( ) ;
50
53
}
@@ -63,7 +66,7 @@ void Operation()
63
66
}
64
67
}
65
68
66
- var retryOperation = new OperationWithExponentialBackoff < IOException > ( new MockThreadSleep ( ) , log , Operation ) ;
69
+ var retryOperation = new OperationWithExponentialBackoff < IOException > ( threadSleep , log , Operation ) ;
67
70
await retryOperation . ExecuteAsync ( ) ;
68
71
69
72
operationCount . ShouldBe ( 2 ) ;
@@ -81,7 +84,7 @@ void Operation()
81
84
throw new IOException ( ) ;
82
85
}
83
86
84
- var retryOperation = new OperationWithExponentialBackoff < IOException > ( new MockThreadSleep ( ) , log , Operation , numberOfRetries ) ;
87
+ var retryOperation = new OperationWithExponentialBackoff < IOException > ( threadSleep , log , Operation , numberOfRetries ) ;
85
88
var action = retryOperation . ExecuteAsync ( ) ;
86
89
await action . ShouldThrowAsync < AggregateException > ( ) ;
87
90
@@ -95,7 +98,7 @@ public async Task OperationDelayDoublesBetweenRetries()
95
98
var expectedSleepMSec = 500 ;
96
99
var sleepCount = 0 ;
97
100
98
- void Operation ( ) => throw new IOException ( ) ;
101
+ static void Operation ( ) => throw new IOException ( ) ;
99
102
100
103
Task Validator ( int u )
101
104
{
@@ -107,12 +110,12 @@ Task Validator(int u)
107
110
} ) ;
108
111
}
109
112
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 ) ;
111
116
var action = retryOperation . ExecuteAsync ( ) ;
112
117
await action . ShouldThrowAsync < AggregateException > ( ) ;
113
118
114
- // action.ShouldThrow<AggregateException>();
115
-
116
119
sleepCount . ShouldBe ( numberOfRetries ) ;
117
120
}
118
121
@@ -122,22 +125,16 @@ public async Task TotalSleepTimeForSixRetriesIsAboutThirtySecondsAsync()
122
125
const int numberOfRetries = 6 ;
123
126
var totalSleep = 0 ;
124
127
125
- void Operation ( )
126
- {
127
- throw new IOException ( ) ;
128
- }
128
+ static void Operation ( ) => throw new IOException ( ) ;
129
129
130
- Task Validator ( int u )
131
- {
132
- return Task . Run ( ( ) => { totalSleep += u ; } ) ;
133
- }
130
+ Task Validator ( int u ) => Task . Run ( ( ) => { totalSleep += u ; } ) ;
134
131
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 ) ;
136
135
137
136
var action = retryOperation . ExecuteAsync ( ) ;
138
137
await action . ShouldThrowAsync < AggregateException > ( ) ;
139
- // Action action = () => retryOperation.ExecuteAsync();
140
- // action.ShouldThrow<AggregateException>();
141
138
142
139
// Exact number is 31,5 seconds
143
140
totalSleep . ShouldBe ( 31500 ) ;
0 commit comments