public void TestStartup() { AutoResetEvent are = new AutoResetEvent(false); var archive = new LastModifiedArchive(monitorFolder); DirectoryScanningMonitor monitor = new DirectoryScanningMonitor(monitorFolder, archive); monitor.DirectoryAdded += (o, e) => { are.Set(); }; monitor.AddDirectory(testFolder); Assert.IsTrue(are.WaitOne(WaitInterval)); int count = 0; monitor.FileChanged += (o, e) => { if (e.EventType == FileEventType.FileAdded) { count++; if (count == numStartingFiles) { are.Set(); } } }; monitor.UpdateArchives(); Assert.IsTrue(are.WaitOne(WaitInterval)); Assert.AreEqual(numStartingFiles, archive.GetFiles().Count(), String.Format("only found {0} files in the archive", archive.GetFiles().Count())); foreach (var fileName in Directory.EnumerateFiles(testFolder)) { Assert.IsTrue(archive.ContainsFile(fileName)); Assert.IsFalse(archive.IsOutdated(fileName)); Assert.AreEqual(File.GetLastWriteTime(fileName), archive.GetLastModifiedTime(fileName)); } }
public void TestFileChanges() { var archive = new LastModifiedArchive(monitorFolder); DirectoryScanningMonitor monitor = new DirectoryScanningMonitor(monitorFolder, archive); monitor.ScanInterval = 1; monitor.AddDirectory(testFolder); monitor.UpdateArchives(); AutoResetEvent are = new AutoResetEvent(false); var expectedEventType = FileEventType.FileAdded; var expectedFileName = Path.GetFullPath(Path.Combine(testFolder, "new.txt")); monitor.FileChanged += (o, e) => { if (e.EventType == expectedEventType && e.FilePath == expectedFileName) { are.Set(); } }; monitor.StartMonitoring(); File.Create(expectedFileName).Close(); Assert.IsTrue(are.WaitOne(WaitInterval)); expectedEventType = FileEventType.FileChanged; var expectedLastWriteTime = DateTime.Now; File.SetLastWriteTime(expectedFileName, expectedLastWriteTime); Assert.IsTrue(are.WaitOne(WaitInterval)); Assert.AreEqual(expectedLastWriteTime, archive.GetLastModifiedTime(expectedFileName)); expectedEventType = FileEventType.FileDeleted; File.Delete(expectedFileName); Assert.IsTrue(are.WaitOne(WaitInterval)); }
public void TestEmptyMonitor() { using (var monitor = new DirectoryScanningMonitor(monitorFolder, DirectoryScanningMonitor.DEFAULT_SCAN_INTERVAL)) { monitor.AddDirectory(testFolder); monitor.FileChanged += (o, e) => Assert.Fail(e.FilePath); monitor.UpdateArchives(); } }
public void TestRemoveDirectory() { var archive = new LastModifiedArchive(monitorFolder); DirectoryScanningMonitor monitor = new DirectoryScanningMonitor(monitorFolder, archive); AutoResetEvent directoryResetEvent = new AutoResetEvent(false); monitor.DirectoryAdded += (o, e) => directoryResetEvent.Set(); monitor.DirectoryRemoved += (o, e) => directoryResetEvent.Set(); monitor.AddDirectory(testFolder); Assert.IsTrue(directoryResetEvent.WaitOne(WaitInterval)); monitor.UpdateArchives(); Assert.AreEqual(numStartingFiles, monitor.GetArchivedFiles().Count()); monitor.RemoveDirectory("test1"); Assert.IsFalse(directoryResetEvent.WaitOne(WaitInterval)); Assert.AreEqual(numStartingFiles, monitor.GetArchivedFiles().Count()); AutoResetEvent fileDeletionResetEvent = new AutoResetEvent(false); int count = numStartingFiles; monitor.FileChanged += (o, e) => { if (e.EventType == FileEventType.FileDeleted) { if (--count == 0) { fileDeletionResetEvent.Set(); } } }; monitor.RemoveDirectory(testFolder); Assert.IsTrue(directoryResetEvent.WaitOne(WaitInterval)); Assert.IsTrue(fileDeletionResetEvent.WaitOne(WaitInterval)); Assert.AreEqual(0, monitor.GetArchivedFiles().Count()); foreach (var fileName in Directory.EnumerateFiles(testFolder)) { Assert.IsTrue(File.Exists(fileName)); } }