Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit ae352ef

Browse files
committed
Add smarter logging to tests
1 parent 8ac24f5 commit ae352ef

File tree

1 file changed

+91
-22
lines changed

1 file changed

+91
-22
lines changed

src/tests/IntegrationTests/Events/RepositoryManagerTests.cs

Lines changed: 91 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
5+
using System.Runtime.CompilerServices;
46
using FluentAssertions;
57
using GitHub.Unity;
68
using NSubstitute;
79
using NUnit.Framework;
810
using TestUtils;
911
using TestUtils.Events;
1012
using System.Threading.Tasks;
13+
using GitHub.Logging;
1114

1215
namespace IntegrationTests
1316
{
@@ -23,10 +26,38 @@ public override void OnSetup()
2326
repositoryManagerEvents = new RepositoryManagerEvents();
2427
}
2528

29+
private void StartTest(out Stopwatch watch, out ILogging logger, [CallerMemberName] string testName = "test")
30+
{
31+
watch = new Stopwatch();
32+
logger = LogHelper.GetLogger(testName);
33+
logger.Trace("Starting test");
34+
}
35+
36+
private void EndTest(ILogging logger)
37+
{
38+
logger.Trace("Ending test");
39+
}
40+
41+
private void StartTrackTime(Stopwatch watch, ILogging logger, string message = "")
42+
{
43+
if (!String.IsNullOrEmpty(message))
44+
logger.Trace(message);
45+
watch.Reset();
46+
watch.Start();
47+
}
48+
49+
private void StopTrackTimeAndLog(Stopwatch watch, ILogging logger)
50+
{
51+
watch.Stop();
52+
logger.Trace($"Time: {watch.ElapsedMilliseconds}");
53+
}
54+
2655
[Test]
2756
public void ShouldPerformBasicInitialize()
2857
{
29-
Logger.Trace("Starting ShouldPerformBasicInitialize");
58+
Stopwatch watch = null;
59+
ILogging logger = null;
60+
StartTest(out watch, out logger);
3061

3162
try
3263
{
@@ -56,14 +87,16 @@ public void ShouldPerformBasicInitialize()
5687
}
5788
finally
5889
{
59-
Logger.Trace("Ending ShouldPerformBasicInitialize");
90+
EndTest(logger);
6091
}
6192
}
6293

6394
[Test]
6495
public async Task ShouldDetectFileChanges()
6596
{
66-
Logger.Trace("Starting ShouldDetectFileChanges");
97+
Stopwatch watch = null;
98+
ILogging logger = null;
99+
StartTest(out watch, out logger);
67100

68101
try
69102
{
@@ -90,8 +123,13 @@ public async Task ShouldDetectFileChanges()
90123

91124
await TaskManager.Wait();
92125

126+
StartTrackTime(watch, logger, "RepositoryManager.WaitForEvents()");
93127
RepositoryManager.WaitForEvents();
128+
StopTrackTimeAndLog(watch, logger);
129+
130+
StartTrackTime(watch, logger, "repositoryManagerEvents.WaitForNotBusy()");
94131
repositoryManagerEvents.WaitForNotBusy();
132+
StopTrackTimeAndLog(watch, logger);
95133

96134
repositoryManagerEvents.GitStatusUpdated.WaitOne(Timeout).Should().BeTrue();
97135

@@ -106,14 +144,16 @@ public async Task ShouldDetectFileChanges()
106144
}
107145
finally
108146
{
109-
Logger.Trace("Ending ShouldDetectFileChanges");
147+
EndTest(logger);
110148
}
111149
}
112150

113151
[Test]
114152
public async Task ShouldAddAndCommitFiles()
115153
{
116-
Logger.Trace("Starting ShouldAddAndCommitFiles");
154+
Stopwatch watch = null;
155+
ILogging logger = null;
156+
StartTest(out watch, out logger);
117157

118158
try
119159
{
@@ -184,14 +224,16 @@ await RepositoryManager
184224
}
185225
finally
186226
{
187-
Logger.Trace("Ending ShouldAddAndCommitFiles");
227+
EndTest(logger);
188228
}
189229
}
190230

191231
[Test]
192232
public async Task ShouldAddAndCommitAllFiles()
193233
{
194-
Logger.Trace("Starting ShouldAddAndCommitAllFiles");
234+
Stopwatch watch = null;
235+
ILogging logger = null;
236+
StartTest(out watch, out logger);
195237

196238
try
197239
{
@@ -221,8 +263,13 @@ public async Task ShouldAddAndCommitAllFiles()
221263

222264
await TaskManager.Wait();
223265

266+
StartTrackTime(watch, logger, "RepositoryManager.WaitForEvents()");
224267
RepositoryManager.WaitForEvents();
268+
StopTrackTimeAndLog(watch, logger);
269+
270+
StartTrackTime(watch, logger, "repositoryManagerEvents.WaitForNotBusy()");
225271
repositoryManagerEvents.WaitForNotBusy();
272+
StopTrackTimeAndLog(watch, logger);
226273

227274
repositoryManagerEvents.GitStatusUpdated.WaitOne(Timeout).Should().BeTrue();
228275

@@ -238,13 +285,21 @@ public async Task ShouldAddAndCommitAllFiles()
238285
repositoryManagerListener.ClearReceivedCalls();
239286
repositoryManagerEvents.Reset();
240287

288+
StartTrackTime(watch, logger, "CommitAllFiles");
241289
await RepositoryManager
242290
.CommitAllFiles("IntegrationTest Commit", string.Empty)
243291
.StartAsAsync();
292+
293+
StopTrackTimeAndLog(watch, logger);
244294
await TaskManager.Wait();
245295

296+
StartTrackTime(watch, logger, "RepositoryManager.WaitForEvents()");
246297
RepositoryManager.WaitForEvents();
298+
StopTrackTimeAndLog(watch, logger);
299+
300+
StartTrackTime(watch, logger, "repositoryManagerEvents.WaitForNotBusy()");
247301
repositoryManagerEvents.WaitForNotBusy();
302+
StopTrackTimeAndLog(watch, logger);
248303

249304
repositoryManagerEvents.GitStatusUpdated.WaitOne(Timeout).Should().BeTrue();
250305
repositoryManagerEvents.GitStatusUpdated.WaitOne(Timeout).Should().BeTrue();
@@ -262,14 +317,16 @@ await RepositoryManager
262317
}
263318
finally
264319
{
265-
Logger.Trace("Ending ShouldAddAndCommitAllFiles");
320+
EndTest(logger);
266321
}
267322
}
268323

269324
[Test]
270325
public async Task ShouldDetectBranchChange()
271326
{
272-
Logger.Trace("Starting ShouldDetectBranchChange");
327+
Stopwatch watch = null;
328+
ILogging logger = null;
329+
StartTest(out watch, out logger);
273330

274331
try
275332
{
@@ -312,14 +369,16 @@ public async Task ShouldDetectBranchChange()
312369
}
313370
finally
314371
{
315-
Logger.Trace("Ending ShouldDetectBranchChange");
372+
EndTest(logger);
316373
}
317374
}
318375

319376
[Test]
320377
public async Task ShouldDetectBranchDelete()
321378
{
322-
Logger.Trace("Starting ShouldDetectBranchDelete");
379+
Stopwatch watch = null;
380+
ILogging logger = null;
381+
StartTest(out watch, out logger);
323382

324383
try
325384
{
@@ -364,14 +423,16 @@ public async Task ShouldDetectBranchDelete()
364423
}
365424
finally
366425
{
367-
Logger.Trace("Ending ShouldDetectBranchDelete");
426+
EndTest(logger);
368427
}
369428
}
370429

371430
[Test]
372431
public async Task ShouldDetectBranchCreate()
373432
{
374-
Logger.Trace("Starting ShouldDetectBranchCreate");
433+
Stopwatch watch = null;
434+
ILogging logger = null;
435+
StartTest(out watch, out logger);
375436

376437
try
377438
{
@@ -433,14 +494,16 @@ public async Task ShouldDetectBranchCreate()
433494
}
434495
finally
435496
{
436-
Logger.Trace("Ending ShouldDetectBranchCreate");
497+
EndTest(logger);
437498
}
438499
}
439500

440501
[Test]
441502
public async Task ShouldDetectChangesToRemotes()
442503
{
443-
Logger.Trace("Starting ShouldDetectChangesToRemotes");
504+
Stopwatch watch = null;
505+
ILogging logger = null;
506+
StartTest(out watch, out logger);
444507

445508
try
446509
{
@@ -509,14 +572,16 @@ public async Task ShouldDetectChangesToRemotes()
509572
}
510573
finally
511574
{
512-
Logger.Trace("Ending ShouldDetectChangesToRemotes");
575+
EndTest(logger);
513576
}
514577
}
515578

516579
[Test]
517580
public async Task ShouldDetectChangesToRemotesWhenSwitchingBranches()
518581
{
519-
Logger.Trace("Starting ShouldDetectChangesToRemotesWhenSwitchingBranches");
582+
Stopwatch watch = null;
583+
ILogging logger = null;
584+
StartTest(out watch, out logger);
520585

521586
try
522587
{
@@ -585,14 +650,16 @@ await RepositoryManager.SwitchBranch("branch2")
585650
}
586651
finally
587652
{
588-
Logger.Trace("Ending ShouldDetectChangesToRemotesWhenSwitchingBranches");
653+
EndTest(logger);
589654
}
590655
}
591656

592657
[Test]
593658
public async Task ShouldDetectGitPull()
594659
{
595-
Logger.Trace("Starting ShouldDetectGitPull");
660+
Stopwatch watch = null;
661+
ILogging logger = null;
662+
StartTest(out watch, out logger);
596663

597664
try
598665
{
@@ -634,14 +701,16 @@ public async Task ShouldDetectGitPull()
634701
}
635702
finally
636703
{
637-
Logger.Trace("Ending ShouldDetectGitPull");
704+
EndTest(logger);
638705
}
639706
}
640707

641708
[Test]
642709
public async Task ShouldDetectGitFetch()
643710
{
644-
Logger.Trace("Starting ShouldDetectGitFetch");
711+
Stopwatch watch = null;
712+
ILogging logger = null;
713+
StartTest(out watch, out logger);
645714

646715
try
647716
{
@@ -684,7 +753,7 @@ public async Task ShouldDetectGitFetch()
684753
}
685754
finally
686755
{
687-
Logger.Trace("Ending ShouldDetectGitFetch");
756+
EndTest(logger);
688757
}
689758
}
690759
}

0 commit comments

Comments
 (0)