From 599a41b380741fb7110162640d0a3ac434b892be Mon Sep 17 00:00:00 2001 From: Victor Hurdugaci Date: Thu, 2 Jun 2016 18:50:15 -0700 Subject: [PATCH] Improve test stability by disposing correctly --- .../FileWatcherTests.cs | 58 ++++++++++++++----- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/FileWatcherTests.cs b/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/FileWatcherTests.cs index 6de6151..dc696e6 100644 --- a/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/FileWatcherTests.cs +++ b/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/FileWatcherTests.cs @@ -58,11 +58,17 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests { var filesChanged = new HashSet(); - watcher.OnFileChange += (_, f) => + EventHandler handler = null; + handler = (_, f) => { + watcher.EnableRaisingEvents = false; + watcher.OnFileChange -= handler; + filesChanged.Add(f); changedEv.Set(); }; + + watcher.OnFileChange += handler; watcher.EnableRaisingEvents = true; // On Unix the file write time is in 1s increments; @@ -94,18 +100,21 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests { var filesChanged = new HashSet(); - var changeCount = 0; - watcher.OnFileChange += (_, f) => + EventHandler handler = null; + handler = (_, f) => { filesChanged.Add(f); - changeCount++; - - if (changeCount >= 2) + if (filesChanged.Count >= 2) { + watcher.EnableRaisingEvents = false; + watcher.OnFileChange -= handler; + changedEv.Set(); } }; + + watcher.OnFileChange += handler; watcher.EnableRaisingEvents = true; File.Move(srcFile, dstFile); @@ -133,17 +142,20 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests { var filesChanged = new HashSet(); - var totalChanges = 0; - watcher.OnFileChange += (_, f) => + EventHandler handler = null; + handler = (_, f) => { filesChanged.Add(f); - totalChanges++; - if (totalChanges >= 2) + if (filesChanged.Count >= 2) { + watcher.EnableRaisingEvents = false; + watcher.OnFileChange -= handler; changedEv.Set(); } }; + + watcher.OnFileChange += handler; watcher.EnableRaisingEvents = true; // On Unix the file write time is in 1s increments; @@ -235,11 +247,16 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests { var filesChanged = new HashSet(); - watcher.OnFileChange += (_, f) => + EventHandler handler = null; + handler = (_, f) => { + watcher.EnableRaisingEvents = false; + watcher.OnFileChange -= handler; filesChanged.Add(f); changedEv.Set(); }; + + watcher.OnFileChange += handler; watcher.EnableRaisingEvents = true; // On Unix the file write time is in 1s increments; @@ -267,11 +284,14 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests { var filesChanged = new HashSet(); - watcher.OnFileChange += (_, f) => + EventHandler handler = null; + handler = (_, f) => { filesChanged.Add(f); changedEv.Set(); }; + + watcher.OnFileChange += handler; watcher.EnableRaisingEvents = true; // 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); Assert.True(changedEv.WaitOne(DefaultTimeout)); 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(); - var totalChanges = 0; - watcher.OnFileChange += (_, f) => + EventHandler handler = null; + handler = (_, f) => { filesChanged.Add(f); - totalChanges++; - if (totalChanges >= 4) + if (filesChanged.Count >= 4) { + watcher.EnableRaisingEvents = false; + watcher.OnFileChange -= handler; changedEv.Set(); } }; + + watcher.OnFileChange += handler; watcher.EnableRaisingEvents = true; Directory.Delete(subdir, recursive: true);