6
6
using GitHub . Unity ;
7
7
using NUnit . Framework ;
8
8
using System . Diagnostics ;
9
+ using GitHub . Unity . Logs ;
10
+ using System . Runtime . CompilerServices ;
9
11
10
12
namespace IntegrationTests . Download
11
13
{
12
14
[ TestFixture ]
13
15
class DownloadTaskTests : BaseTaskManagerTest
14
16
{
17
+ const int Timeout = 30000 ;
18
+
15
19
public override void OnSetup ( )
16
20
{
17
21
base . OnSetup ( ) ;
@@ -34,10 +38,33 @@ public override void TestFixtureTearDown()
34
38
ApplicationConfiguration . WebTimeout = ApplicationConfiguration . DefaultWebTimeout ;
35
39
}
36
40
41
+ private void StartTest ( out Stopwatch watch , out ILogging logger , [ CallerMemberName ] string testName = "test" )
42
+ {
43
+ watch = new Stopwatch ( ) ;
44
+ logger = Logging . GetLogger ( testName ) ;
45
+ logger . Trace ( "Starting test" ) ;
46
+ }
47
+
48
+ private void StartTrackTime ( Stopwatch watch , ILogging logger = null , string message = "" )
49
+ {
50
+ if ( ! String . IsNullOrEmpty ( message ) )
51
+ logger . Trace ( message ) ;
52
+ watch . Reset ( ) ;
53
+ watch . Start ( ) ;
54
+ }
55
+
56
+ private void StopTrackTimeAndLog ( Stopwatch watch , ILogging logger )
57
+ {
58
+ watch . Stop ( ) ;
59
+ logger . Trace ( $ "Time: { watch . ElapsedMilliseconds } ") ;
60
+ }
61
+
37
62
[ Test ]
38
63
public void TestDownloadTask ( )
39
64
{
40
- Logger . Info ( "Starting Test: TestDownloadTask" ) ;
65
+ Stopwatch watch ;
66
+ ILogging logger ;
67
+ StartTest ( out watch , out logger ) ;
41
68
42
69
var fileSystem = Environment . FileSystem ;
43
70
@@ -47,26 +74,33 @@ public void TestDownloadTask()
47
74
var evtDone = new ManualResetEventSlim ( false ) ;
48
75
49
76
string md5 = null ;
77
+
78
+ StartTrackTime ( watch , logger , gitLfsMd5 ) ;
50
79
new DownloadTextTask ( TaskManager . Token , fileSystem , gitLfsMd5 , TestBasePath )
51
80
. Finally ( r => {
52
81
md5 = r ;
53
82
evtDone . Set ( ) ;
54
83
} )
55
84
. Start ( ) ;
56
85
57
- evtDone . Wait ( 10000 ) ;
86
+ evtDone . Wait ( Timeout ) . Should ( ) . BeTrue ( "Finally raised the signal" ) ;
87
+ StopTrackTimeAndLog ( watch , logger ) ;
88
+
58
89
evtDone . Reset ( ) ;
59
90
Assert . NotNull ( md5 ) ;
60
91
61
92
string downloadPath = null ;
93
+ StartTrackTime ( watch , logger , gitLfs ) ;
62
94
new DownloadTask ( TaskManager . Token , fileSystem , gitLfs , TestBasePath )
63
95
. Finally ( r => {
64
96
downloadPath = r ;
65
97
evtDone . Set ( ) ;
66
98
} )
67
99
. Start ( ) ;
68
100
69
- evtDone . Wait ( 10000 ) ;
101
+ evtDone . Wait ( Timeout ) . Should ( ) . BeTrue ( "Finally raised the signal" ) ; ;
102
+ StopTrackTimeAndLog ( watch , logger ) ;
103
+
70
104
evtDone . Reset ( ) ;
71
105
72
106
Assert . NotNull ( downloadPath ) ;
@@ -81,14 +115,17 @@ public void TestDownloadTask()
81
115
fileSystem . FileDelete ( downloadPath ) ;
82
116
fileSystem . WriteAllBytes ( downloadPath , cutDownloadPathBytes ) ;
83
117
118
+ StartTrackTime ( watch , logger , "resuming download" ) ;
84
119
new DownloadTask ( TaskManager . Token , fileSystem , gitLfs , TestBasePath )
85
120
. Finally ( r => {
86
121
downloadPath = r ;
87
122
evtDone . Set ( ) ;
88
123
} )
89
124
. Start ( ) ;
90
125
91
- evtDone . Wait ( 10000 ) ;
126
+ evtDone . Wait ( Timeout ) . Should ( ) . BeTrue ( "Finally raised the signal" ) ; ;
127
+ StopTrackTimeAndLog ( watch , logger ) ;
128
+
92
129
evtDone . Reset ( ) ;
93
130
94
131
var downloadHalfPathBytes = fileSystem . ReadAllBytes ( downloadPath ) ;
@@ -101,7 +138,9 @@ public void TestDownloadTask()
101
138
[ Test ]
102
139
public void TestDownloadFailure ( )
103
140
{
104
- Logger . Info ( "Starting Test: TestDownloadFailure" ) ;
141
+ Stopwatch watch ;
142
+ ILogging logger ;
143
+ StartTest ( out watch , out logger ) ;
105
144
106
145
var fileSystem = Environment . FileSystem ;
107
146
@@ -110,17 +149,21 @@ public void TestDownloadFailure()
110
149
111
150
var autoResetEvent = new AutoResetEvent ( false ) ;
112
151
113
- var downloadTask = new DownloadTask ( TaskManager . Token , fileSystem ,
114
- $ "http://localhost:{ server . Port } /nope", TestBasePath )
152
+ var downloadTask = new DownloadTask ( TaskManager . Token , fileSystem , $ "http://localhost:{ server . Port } /nope", TestBasePath ) ;
153
+
154
+ StartTrackTime ( watch ) ;
155
+ downloadTask
115
156
. Finally ( ( b , exception ) => {
116
157
taskFailed = ! b ;
117
158
exceptionThrown = exception ;
118
159
autoResetEvent . Set ( ) ;
119
- } ) ;
160
+ } )
161
+ . Start ( ) ;
120
162
121
- downloadTask . Start ( ) ;
163
+ var ret = autoResetEvent . WaitOne ( Timeout ) ;
164
+ StopTrackTimeAndLog ( watch , logger ) ;
122
165
123
- autoResetEvent . WaitOne ( 10000 ) ;
166
+ ret . Should ( ) . BeTrue ( "Finally raised the signal" ) ;
124
167
125
168
taskFailed . Should ( ) . BeTrue ( ) ;
126
169
exceptionThrown . Should ( ) . NotBeNull ( ) ;
@@ -129,7 +172,9 @@ public void TestDownloadFailure()
129
172
[ Test ]
130
173
public void TestDownloadTextTask ( )
131
174
{
132
- Logger . Info ( "Starting Test: TestDownloadTextTask" ) ;
175
+ Stopwatch watch ;
176
+ ILogging logger ;
177
+ StartTest ( out watch , out logger ) ;
133
178
134
179
var fileSystem = Environment . FileSystem ;
135
180
@@ -139,43 +184,55 @@ public void TestDownloadTextTask()
139
184
140
185
var autoResetEvent = new AutoResetEvent ( false ) ;
141
186
string result = null ;
187
+
188
+ StartTrackTime ( watch ) ;
142
189
downloadTask
143
190
. Finally ( r => {
144
191
result = r ;
145
192
autoResetEvent . Set ( ) ;
146
193
} )
147
194
. Start ( ) ;
148
195
149
- autoResetEvent . WaitOne ( 10000 ) ;
196
+ autoResetEvent . WaitOne ( Timeout ) . Should ( ) . BeTrue ( "Finally raised the signal" ) ; ;
197
+ StopTrackTimeAndLog ( watch , logger ) ;
198
+
150
199
result . Should ( ) . Be ( "105DF1302560C5F6AA64D1930284C126" ) ;
151
200
}
152
201
153
202
[ Test ]
154
203
public void TestDownloadTextFailure ( )
155
204
{
156
- Logger . Info ( "Starting Test: TestDownloadTextFailure" ) ;
205
+ Stopwatch watch ;
206
+ ILogging logger ;
207
+ StartTest ( out watch , out logger ) ;
157
208
158
209
var fileSystem = Environment . FileSystem ;
159
210
160
211
var downloadTask = new DownloadTextTask ( TaskManager . Token , fileSystem , "https://ggggithub.com/robots.txt" ) ;
161
212
var exceptionThrown = false ;
162
213
163
214
var autoResetEvent = new AutoResetEvent ( false ) ;
215
+
216
+ StartTrackTime ( watch ) ;
164
217
downloadTask
165
218
. Finally ( ( b , exception ) => {
166
219
exceptionThrown = exception != null ;
167
220
autoResetEvent . Set ( ) ;
168
221
} )
169
222
. Start ( ) ;
170
223
171
- autoResetEvent . WaitOne ( 10000 ) ;
224
+ autoResetEvent . WaitOne ( Timeout ) . Should ( ) . BeTrue ( "Finally raised the signal" ) ; ;
225
+ StopTrackTimeAndLog ( watch , logger ) ;
226
+
172
227
exceptionThrown . Should ( ) . BeTrue ( ) ;
173
228
}
174
229
175
230
[ Test ]
176
231
public void TestDownloadFileAndHash ( )
177
232
{
178
- Logger . Info ( "Starting Test: TestDownloadFileAndHash" ) ;
233
+ Stopwatch watch ;
234
+ ILogging logger ;
235
+ StartTest ( out watch , out logger ) ;
179
236
180
237
var fileSystem = Environment . FileSystem ;
181
238
@@ -190,6 +247,7 @@ public void TestDownloadFileAndHash()
190
247
191
248
var autoResetEvent = new AutoResetEvent ( false ) ;
192
249
250
+ StartTrackTime ( watch ) ;
193
251
downloadGitLfsMd5Task
194
252
. Then ( ( b , s ) =>
195
253
{
@@ -203,7 +261,8 @@ public void TestDownloadFileAndHash()
203
261
} )
204
262
. Start ( ) ;
205
263
206
- autoResetEvent . WaitOne ( 10000 ) ;
264
+ autoResetEvent . WaitOne ( Timeout ) . Should ( ) . BeTrue ( "Finally raised the signal" ) ; ;
265
+ StopTrackTimeAndLog ( watch , logger ) ;
207
266
208
267
result . Should ( ) . BeTrue ( ) ;
209
268
exception . Should ( ) . BeNull ( ) ;
@@ -212,7 +271,9 @@ public void TestDownloadFileAndHash()
212
271
[ Test ]
213
272
public void TestDownloadShutdownTimeWhenInterrupted ( )
214
273
{
215
- Logger . Info ( "Starting Test: TestDownloadShutdownTimeWhenInterrupted" ) ;
274
+ Stopwatch watch ;
275
+ ILogging logger ;
276
+ StartTest ( out watch , out logger ) ;
216
277
217
278
server . Delay = 100 ;
218
279
@@ -222,9 +283,9 @@ public void TestDownloadShutdownTimeWhenInterrupted()
222
283
var evtFinally = new AutoResetEvent ( false ) ;
223
284
Exception exception = null ;
224
285
225
- var watch = new Stopwatch ( ) ;
226
-
227
286
var gitLfs = new UriString ( $ "http://localhost:{ server . Port } /git-lfs.zip") ;
287
+
288
+ StartTrackTime ( watch , logger , gitLfs ) ;
228
289
var downloadGitTask = new DownloadTask ( TaskManager . Token , fileSystem , gitLfs , TestBasePath )
229
290
230
291
// An exception is thrown when we stop the task manager
@@ -243,19 +304,20 @@ public void TestDownloadShutdownTimeWhenInterrupted()
243
304
244
305
downloadGitTask . Start ( ) ;
245
306
246
- evtStop . WaitOne ( 10000 ) ;
307
+ evtStop . WaitOne ( Timeout ) . Should ( ) . BeTrue ( "Progress raised the signal" ) ;
308
+ StopTrackTimeAndLog ( watch , logger ) ;
247
309
248
- watch . Start ( ) ;
310
+
311
+ StartTrackTime ( watch , logger , "TaskManager.Dispose()" ) ;
249
312
TaskManager . Dispose ( ) ;
250
- evtFinally . WaitOne ( 10000 ) ;
251
- watch . Stop ( ) ;
313
+ evtFinally . WaitOne ( Timeout ) . Should ( ) . BeTrue ( "Catch raised the signal" ) ;
314
+ StopTrackTimeAndLog ( watch , logger ) ;
252
315
253
316
server . Delay = 0 ;
254
317
server . Abort ( ) ;
255
318
256
319
exception . Should ( ) . NotBeNull ( ) ;
257
320
watch . ElapsedMilliseconds . Should ( ) . BeLessThan ( 250 ) ;
258
-
259
321
}
260
322
}
261
323
}
0 commit comments