Improve test stability by disposing correctly

This commit is contained in:
Victor Hurdugaci 2016-06-02 18:50:15 -07:00
Родитель d7206dee8d
Коммит 599a41b380
1 изменённых файлов: 42 добавлений и 16 удалений

Просмотреть файл

@ -58,11 +58,17 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
{ {
var filesChanged = new HashSet<string>(); var filesChanged = new HashSet<string>();
watcher.OnFileChange += (_, f) => EventHandler<string> handler = null;
handler = (_, f) =>
{ {
watcher.EnableRaisingEvents = false;
watcher.OnFileChange -= handler;
filesChanged.Add(f); filesChanged.Add(f);
changedEv.Set(); changedEv.Set();
}; };
watcher.OnFileChange += handler;
watcher.EnableRaisingEvents = true; watcher.EnableRaisingEvents = true;
// On Unix the file write time is in 1s increments; // On Unix the file write time is in 1s increments;
@ -94,18 +100,21 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
{ {
var filesChanged = new HashSet<string>(); var filesChanged = new HashSet<string>();
var changeCount = 0; EventHandler<string> handler = null;
watcher.OnFileChange += (_, f) => handler = (_, f) =>
{ {
filesChanged.Add(f); filesChanged.Add(f);
changeCount++; if (filesChanged.Count >= 2)
if (changeCount >= 2)
{ {
watcher.EnableRaisingEvents = false;
watcher.OnFileChange -= handler;
changedEv.Set(); changedEv.Set();
} }
}; };
watcher.OnFileChange += handler;
watcher.EnableRaisingEvents = true; watcher.EnableRaisingEvents = true;
File.Move(srcFile, dstFile); File.Move(srcFile, dstFile);
@ -133,17 +142,20 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
{ {
var filesChanged = new HashSet<string>(); var filesChanged = new HashSet<string>();
var totalChanges = 0; EventHandler<string> handler = null;
watcher.OnFileChange += (_, f) => handler = (_, f) =>
{ {
filesChanged.Add(f); filesChanged.Add(f);
totalChanges++; if (filesChanged.Count >= 2)
if (totalChanges >= 2)
{ {
watcher.EnableRaisingEvents = false;
watcher.OnFileChange -= handler;
changedEv.Set(); changedEv.Set();
} }
}; };
watcher.OnFileChange += handler;
watcher.EnableRaisingEvents = true; watcher.EnableRaisingEvents = true;
// On Unix the file write time is in 1s increments; // On Unix the file write time is in 1s increments;
@ -235,11 +247,16 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
{ {
var filesChanged = new HashSet<string>(); var filesChanged = new HashSet<string>();
watcher.OnFileChange += (_, f) => EventHandler<string> handler = null;
handler = (_, f) =>
{ {
watcher.EnableRaisingEvents = false;
watcher.OnFileChange -= handler;
filesChanged.Add(f); filesChanged.Add(f);
changedEv.Set(); changedEv.Set();
}; };
watcher.OnFileChange += handler;
watcher.EnableRaisingEvents = true; watcher.EnableRaisingEvents = true;
// On Unix the file write time is in 1s increments; // On Unix the file write time is in 1s increments;
@ -267,11 +284,14 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
{ {
var filesChanged = new HashSet<string>(); var filesChanged = new HashSet<string>();
watcher.OnFileChange += (_, f) => EventHandler<string> handler = null;
handler = (_, f) =>
{ {
filesChanged.Add(f); filesChanged.Add(f);
changedEv.Set(); changedEv.Set();
}; };
watcher.OnFileChange += handler;
watcher.EnableRaisingEvents = true; watcher.EnableRaisingEvents = true;
// On Unix the file write time is in 1s increments; // On Unix the file write time is in 1s increments;
@ -315,6 +335,9 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
File.WriteAllText(testFileFullPath, string.Empty); File.WriteAllText(testFileFullPath, string.Empty);
Assert.True(changedEv.WaitOne(DefaultTimeout)); Assert.True(changedEv.WaitOne(DefaultTimeout));
Assert.Equal(testFileFullPath, filesChanged.Single()); Assert.Equal(testFileFullPath, filesChanged.Single());
watcher.EnableRaisingEvents = false;
watcher.OnFileChange -= handler;
} }
}); });
} }
@ -342,17 +365,20 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests
{ {
var filesChanged = new HashSet<string>(); var filesChanged = new HashSet<string>();
var totalChanges = 0; EventHandler<string> handler = null;
watcher.OnFileChange += (_, f) => handler = (_, f) =>
{ {
filesChanged.Add(f); filesChanged.Add(f);
totalChanges++; if (filesChanged.Count >= 4)
if (totalChanges >= 4)
{ {
watcher.EnableRaisingEvents = false;
watcher.OnFileChange -= handler;
changedEv.Set(); changedEv.Set();
} }
}; };
watcher.OnFileChange += handler;
watcher.EnableRaisingEvents = true; watcher.EnableRaisingEvents = true;
Directory.Delete(subdir, recursive: true); Directory.Delete(subdir, recursive: true);