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

Commit acb8f3a

Browse files
Making RepositoryWatcherTests faster
1 parent 9a866b5 commit acb8f3a

File tree

2 files changed

+121
-28
lines changed

2 files changed

+121
-28
lines changed

src/GitHub.Api/Events/RepositoryWatcher.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ private void HandleEventInDotGit(Event fileEvent, NPath fileA, NPath fileB = nul
213213

214214
if (fileEvent.Type == EventType.DELETED)
215215
{
216+
if (fileA.ExtensionWithDot == ".lock")
217+
{
218+
return;
219+
}
220+
216221
var branch = string.Join(@"/", relativePathElements.Skip(1).ToArray());
217222

218223
Logger.Trace("RemoteBranchDeleted: {0}/{1}", origin, branch);

src/IntegrationTests/Events/RepositoryWatcherTests.cs

Lines changed: 116 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading;
1+
using System;
2+
using System.Threading;
23
using FluentAssertions;
34
using GitHub.Unity;
45
using NSubstitute;
@@ -18,8 +19,10 @@ public void ShouldDetectFileChanges()
1819

1920
var repositoryWatcher = CreateRepositoryWatcher(TestRepoMasterCleanSynchronized);
2021

22+
var watcherAutoResetEvent = new RepositoryWatcherAutoResetEvent();
23+
2124
var repositoryWatcherListener = Substitute.For<IRepositoryWatcherListener>();
22-
repositoryWatcherListener.AttachListener(repositoryWatcher);
25+
repositoryWatcherListener.AttachListener(repositoryWatcher, watcherAutoResetEvent);
2326

2427
repositoryWatcher.Initialize();
2528
repositoryWatcher.Start();
@@ -29,7 +32,8 @@ public void ShouldDetectFileChanges()
2932
var foobarTxt = TestRepoMasterCleanSynchronized.Combine("foobar.txt");
3033
foobarTxt.WriteAllText("foobar");
3134

32-
Thread.Sleep(ThreadSleepTimeout);
35+
watcherAutoResetEvent.IndexChanged.WaitOne(TimeSpan.FromSeconds(2));
36+
watcherAutoResetEvent.RepositoryChanged.WaitOne(TimeSpan.FromSeconds(2));
3337

3438
repositoryWatcherListener.DidNotReceive().ConfigChanged();
3539
repositoryWatcherListener.DidNotReceive().HeadChanged(Args.String);
@@ -55,8 +59,10 @@ public void ShouldDetectBranchChange()
5559

5660
var repositoryWatcher = CreateRepositoryWatcher(TestRepoMasterCleanSynchronized);
5761

62+
var watcherAutoResetEvent = new RepositoryWatcherAutoResetEvent();
63+
5864
var repositoryWatcherListener = Substitute.For<IRepositoryWatcherListener>();
59-
repositoryWatcherListener.AttachListener(repositoryWatcher);
65+
repositoryWatcherListener.AttachListener(repositoryWatcher, watcherAutoResetEvent);
6066

6167
repositoryWatcher.Initialize();
6268
repositoryWatcher.Start();
@@ -65,7 +71,9 @@ public void ShouldDetectBranchChange()
6571
{
6672
SwitchBranch("feature/document");
6773

68-
Thread.Sleep(ThreadSleepTimeout);
74+
watcherAutoResetEvent.HeadChanged.WaitOne(TimeSpan.FromSeconds(2));
75+
watcherAutoResetEvent.IndexChanged.WaitOne(TimeSpan.FromSeconds(2));
76+
watcherAutoResetEvent.RepositoryChanged.WaitOne(TimeSpan.FromSeconds(2));
6977

7078
repositoryWatcherListener.DidNotReceive().ConfigChanged();
7179
repositoryWatcherListener.Received(1).HeadChanged("ref: refs/heads/feature/document");
@@ -91,8 +99,10 @@ public void ShouldDetectBranchDelete()
9199

92100
var repositoryWatcher = CreateRepositoryWatcher(TestRepoMasterCleanSynchronized);
93101

102+
var watcherAutoResetEvent = new RepositoryWatcherAutoResetEvent();
103+
94104
var repositoryWatcherListener = Substitute.For<IRepositoryWatcherListener>();
95-
repositoryWatcherListener.AttachListener(repositoryWatcher);
105+
repositoryWatcherListener.AttachListener(repositoryWatcher, watcherAutoResetEvent);
96106

97107
repositoryWatcher.Initialize();
98108
repositoryWatcher.Start();
@@ -101,7 +111,8 @@ public void ShouldDetectBranchDelete()
101111
{
102112
DeleteBranch("feature/document");
103113

104-
Thread.Sleep(ThreadSleepTimeout);
114+
watcherAutoResetEvent.ConfigChanged.WaitOne(TimeSpan.FromSeconds(2));
115+
watcherAutoResetEvent.LocalBranchDeleted.WaitOne(TimeSpan.FromSeconds(2));
105116

106117
repositoryWatcherListener.Received(1).ConfigChanged();
107118
repositoryWatcherListener.DidNotReceive().HeadChanged(Args.String);
@@ -127,8 +138,10 @@ public void ShouldDetectBranchCreate()
127138

128139
var repositoryWatcher = CreateRepositoryWatcher(TestRepoMasterCleanSynchronized);
129140

141+
var watcherAutoResetEvent = new RepositoryWatcherAutoResetEvent();
142+
130143
var repositoryWatcherListener = Substitute.For<IRepositoryWatcherListener>();
131-
repositoryWatcherListener.AttachListener(repositoryWatcher);
144+
repositoryWatcherListener.AttachListener(repositoryWatcher, watcherAutoResetEvent);
132145

133146
repositoryWatcher.Initialize();
134147
repositoryWatcher.Start();
@@ -137,7 +150,7 @@ public void ShouldDetectBranchCreate()
137150
{
138151
CreateBranch("feature/document2", "feature/document");
139152

140-
Thread.Sleep(ThreadSleepTimeout);
153+
watcherAutoResetEvent.LocalBranchCreated.WaitOne(TimeSpan.FromSeconds(2));
141154

142155
repositoryWatcherListener.DidNotReceive().ConfigChanged();
143156
repositoryWatcherListener.DidNotReceive().HeadChanged(Args.String);
@@ -154,7 +167,7 @@ public void ShouldDetectBranchCreate()
154167

155168
CreateBranch("feature2/document2", "feature/document");
156169

157-
Thread.Sleep(ThreadSleepTimeout);
170+
watcherAutoResetEvent.LocalBranchCreated.WaitOne(TimeSpan.FromSeconds(2));
158171

159172
repositoryWatcherListener.DidNotReceive().ConfigChanged();
160173
repositoryWatcherListener.DidNotReceive().HeadChanged(Args.String);
@@ -182,8 +195,10 @@ public void ShouldDetectChangesToRemotes()
182195

183196
var repositoryWatcher = CreateRepositoryWatcher(TestRepoMasterCleanSynchronized);
184197

198+
var watcherAutoResetEvent = new RepositoryWatcherAutoResetEvent();
199+
185200
var repositoryWatcherListener = Substitute.For<IRepositoryWatcherListener>();
186-
repositoryWatcherListener.AttachListener(repositoryWatcher);
201+
repositoryWatcherListener.AttachListener(repositoryWatcher, watcherAutoResetEvent);
187202

188203
repositoryWatcher.Initialize();
189204
repositoryWatcher.Start();
@@ -192,7 +207,10 @@ public void ShouldDetectChangesToRemotes()
192207
{
193208
GitRemoteRemove("origin");
194209

195-
Thread.Sleep(ThreadSleepTimeout);
210+
watcherAutoResetEvent.ConfigChanged.WaitOne(TimeSpan.FromSeconds(2));
211+
watcherAutoResetEvent.RemoteBranchDeleted.WaitOne(TimeSpan.FromSeconds(2));
212+
watcherAutoResetEvent.RemoteBranchDeleted.WaitOne(TimeSpan.FromSeconds(2));
213+
watcherAutoResetEvent.RemoteBranchDeleted.WaitOne(TimeSpan.FromSeconds(2));
196214

197215
repositoryWatcherListener.Received().ConfigChanged();
198216
repositoryWatcherListener.DidNotReceive().HeadChanged(Args.String);
@@ -209,7 +227,7 @@ public void ShouldDetectChangesToRemotes()
209227

210228
GitRemoteAdd("origin", "https://github.com/EvilStanleyGoldman/IOTestsRepo.git");
211229

212-
Thread.Sleep(ThreadSleepTimeout);
230+
watcherAutoResetEvent.ConfigChanged.WaitOne(TimeSpan.FromSeconds(2));
213231

214232
repositoryWatcherListener.Received().ConfigChanged();
215233
repositoryWatcherListener.DidNotReceive().HeadChanged(Args.String);
@@ -235,8 +253,10 @@ public void ShouldDetectGitPull()
235253

236254
var repositoryWatcher = CreateRepositoryWatcher(TestRepoMasterCleanSynchronized);
237255

256+
var watcherAutoResetEvent = new RepositoryWatcherAutoResetEvent();
257+
238258
var repositoryWatcherListener = Substitute.For<IRepositoryWatcherListener>();
239-
repositoryWatcherListener.AttachListener(repositoryWatcher);
259+
repositoryWatcherListener.AttachListener(repositoryWatcher, watcherAutoResetEvent);
240260

241261
repositoryWatcher.Initialize();
242262
repositoryWatcher.Start();
@@ -245,7 +265,9 @@ public void ShouldDetectGitPull()
245265
{
246266
GitPull("origin", "master");
247267

248-
Thread.Sleep(ThreadSleepTimeout);
268+
watcherAutoResetEvent.IndexChanged.WaitOne(TimeSpan.FromSeconds(2));
269+
watcherAutoResetEvent.LocalBranchChanged.WaitOne(TimeSpan.FromSeconds(2));
270+
watcherAutoResetEvent.RepositoryChanged.WaitOne(TimeSpan.FromSeconds(2));
249271

250272
repositoryWatcherListener.DidNotReceive().ConfigChanged();
251273
repositoryWatcherListener.DidNotReceive().HeadChanged(Args.String);
@@ -271,8 +293,10 @@ public void ShouldDetectGitFetch()
271293

272294
var repositoryWatcher = CreateRepositoryWatcher(TestRepoMasterCleanUnsynchronized);
273295

296+
var watcherAutoResetEvent = new RepositoryWatcherAutoResetEvent();
297+
274298
var repositoryWatcherListener = Substitute.For<IRepositoryWatcherListener>();
275-
repositoryWatcherListener.AttachListener(repositoryWatcher);
299+
repositoryWatcherListener.AttachListener(repositoryWatcher, watcherAutoResetEvent);
276300

277301
repositoryWatcher.Initialize();
278302
repositoryWatcher.Start();
@@ -281,7 +305,8 @@ public void ShouldDetectGitFetch()
281305
{
282306
GitFetch("origin");
283307

284-
Thread.Sleep(ThreadSleepTimeout);
308+
watcherAutoResetEvent.RemoteBranchCreated.WaitOne(TimeSpan.FromSeconds(2));
309+
watcherAutoResetEvent.RemoteBranchCreated.WaitOne(TimeSpan.FromSeconds(2));
285310

286311
repositoryWatcherListener.DidNotReceive().ConfigChanged();
287312
repositoryWatcherListener.DidNotReceive().HeadChanged(Args.String);
@@ -417,18 +442,67 @@ public interface IRepositoryWatcherListener
417442

418443
static class RepositoryWatcherListenerExtensions
419444
{
420-
public static void AttachListener(this IRepositoryWatcherListener listener, IRepositoryWatcher repositoryWatcher)
445+
public static void AttachListener(this IRepositoryWatcherListener listener, IRepositoryWatcher repositoryWatcher, RepositoryWatcherAutoResetEvent autoResetEvent = null)
421446
{
422-
repositoryWatcher.HeadChanged += listener.HeadChanged;
423-
repositoryWatcher.ConfigChanged += listener.ConfigChanged;
424-
repositoryWatcher.IndexChanged += listener.IndexChanged;
425-
repositoryWatcher.LocalBranchChanged += listener.LocalBranchChanged;
426-
repositoryWatcher.LocalBranchCreated += listener.LocalBranchCreated;
427-
repositoryWatcher.LocalBranchDeleted += listener.LocalBranchDeleted;
428-
repositoryWatcher.RemoteBranchChanged += listener.RemoteBranchChanged;
429-
repositoryWatcher.RemoteBranchCreated += listener.RemoteBranchCreated;
430-
repositoryWatcher.RemoteBranchDeleted += listener.RemoteBranchDeleted;
431-
repositoryWatcher.RepositoryChanged += listener.RepositoryChanged;
447+
repositoryWatcher.HeadChanged += s =>
448+
{
449+
listener.HeadChanged(s);
450+
autoResetEvent?.HeadChanged.Set();
451+
};
452+
453+
repositoryWatcher.ConfigChanged += () =>
454+
{
455+
listener.ConfigChanged();
456+
autoResetEvent?.ConfigChanged.Set();
457+
};
458+
459+
repositoryWatcher.IndexChanged += () =>
460+
{
461+
listener.IndexChanged();
462+
autoResetEvent?.IndexChanged.Set();
463+
};
464+
465+
repositoryWatcher.LocalBranchChanged += s =>
466+
{
467+
listener.LocalBranchChanged(s);
468+
autoResetEvent?.LocalBranchChanged.Set();
469+
};
470+
471+
repositoryWatcher.LocalBranchCreated += s =>
472+
{
473+
listener.LocalBranchCreated(s);
474+
autoResetEvent?.LocalBranchCreated.Set();
475+
};
476+
477+
repositoryWatcher.LocalBranchDeleted += s =>
478+
{
479+
listener.LocalBranchDeleted(s);
480+
autoResetEvent?.LocalBranchDeleted.Set();
481+
};
482+
483+
repositoryWatcher.RemoteBranchChanged += (s, s1) =>
484+
{
485+
listener.RemoteBranchChanged(s, s1);
486+
autoResetEvent?.RemoteBranchChanged.Set();
487+
};
488+
489+
repositoryWatcher.RemoteBranchCreated += (s, s1) =>
490+
{
491+
listener.RemoteBranchCreated(s, s1);
492+
autoResetEvent?.RemoteBranchCreated.Set();
493+
};
494+
495+
repositoryWatcher.RemoteBranchDeleted += (s, s1) =>
496+
{
497+
listener.RemoteBranchDeleted(s, s1);
498+
autoResetEvent?.RemoteBranchDeleted.Set();
499+
};
500+
501+
repositoryWatcher.RepositoryChanged += () =>
502+
{
503+
listener.RepositoryChanged();
504+
autoResetEvent?.RepositoryChanged.Set();
505+
};
432506
}
433507

434508
public static void AssertDidNotReceiveAnyCalls(this IRepositoryWatcherListener repositoryWatcherListener)
@@ -445,4 +519,18 @@ public static void AssertDidNotReceiveAnyCalls(this IRepositoryWatcherListener r
445519
repositoryWatcherListener.DidNotReceive().RepositoryChanged();
446520
}
447521
}
522+
523+
class RepositoryWatcherAutoResetEvent
524+
{
525+
public AutoResetEvent HeadChanged { get; } = new AutoResetEvent(false);
526+
public AutoResetEvent ConfigChanged { get; } = new AutoResetEvent(false);
527+
public AutoResetEvent IndexChanged { get; } = new AutoResetEvent(false);
528+
public AutoResetEvent LocalBranchChanged { get; } = new AutoResetEvent(false);
529+
public AutoResetEvent LocalBranchCreated { get; } = new AutoResetEvent(false);
530+
public AutoResetEvent LocalBranchDeleted { get; } = new AutoResetEvent(false);
531+
public AutoResetEvent RemoteBranchChanged { get; } = new AutoResetEvent(false);
532+
public AutoResetEvent RemoteBranchCreated { get; } = new AutoResetEvent(false);
533+
public AutoResetEvent RemoteBranchDeleted { get; } = new AutoResetEvent(false);
534+
public AutoResetEvent RepositoryChanged { get; } = new AutoResetEvent(false);
535+
}
448536
}

0 commit comments

Comments
 (0)