Exemple #1
0
        public void FileSystemWatcher_File_NotifyFilter_CreationTime(NotifyFilters filter)
        {
            FileSystemWatcherTest.Execute(() =>
            {
                string file = CreateTestFile(TestDirectory, "file");
                using (var watcher = new FileSystemWatcher(TestDirectory, Path.GetFileName(file)))
                {
                    watcher.NotifyFilter = filter;
                    Action action        = () => File.SetCreationTime(file, DateTime.Now + TimeSpan.FromSeconds(10));

                    WatcherChangeTypes expected = 0;
                    if (filter == NotifyFilters.CreationTime)
                    {
                        expected |= WatcherChangeTypes.Changed;
                    }
                    else if (OperatingSystem.IsLinux() && ((filter & LinuxFiltersForAttribute) > 0))
                    {
                        expected |= WatcherChangeTypes.Changed;
                    }
                    else if (OperatingSystem.IsMacOS() && ((filter & OSXFiltersForModify) > 0))
                    {
                        expected |= WatcherChangeTypes.Changed;
                    }

                    ExpectEvent(watcher, expected, action, expectedPath: file);
                }
            }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
        }
        public void FileSystemWatcher_File_Create_EnablingDisablingNotAffectRaisingEvent()
        {
            FileSystemWatcherTest.Execute(() =>
            {
                using (var watcher = new FileSystemWatcher(TestDirectory))
                {
                    string fileName = Path.Combine(TestDirectory, "file");
                    watcher.Filter  = Path.GetFileName(fileName);

                    int numberOfRaisedEvents       = 0;
                    AutoResetEvent autoResetEvent  = new AutoResetEvent(false);
                    FileSystemEventHandler handler = (o, e) =>
                    {
                        Interlocked.Increment(ref numberOfRaisedEvents);
                        autoResetEvent.Set();
                    };

                    watcher.Created += handler;

                    for (int i = 0; i < 100; i++)
                    {
                        watcher.EnableRaisingEvents = true;
                        watcher.EnableRaisingEvents = false;
                    }

                    watcher.EnableRaisingEvents = true;

                    // this should raise one and only one event
                    File.Create(fileName).Dispose();
                    Assert.True(autoResetEvent.WaitOne(WaitForExpectedEventTimeout_NoRetry));
                    Assert.False(autoResetEvent.WaitOne(SubsequentExpectedWait));
                    Assert.True(numberOfRaisedEvents == 1);
                }
            }, DefaultAttemptsForExpectedEvent, (iteration) => RetryDelayMilliseconds);
        }
        public void FileSystemWatcher_Directory_NotifyFilter_Attributes(NotifyFilters filter)
        {
            FileSystemWatcherTest.Execute(() =>
            {
                string dir = CreateTestDirectory(TestDirectory, "dir");
                using (var watcher = new FileSystemWatcher(TestDirectory, Path.GetFileName(dir)))
                {
                    watcher.NotifyFilter = filter;
                    var attributes       = File.GetAttributes(dir);

                    Action action  = () => File.SetAttributes(dir, attributes | FileAttributes.ReadOnly);
                    Action cleanup = () => File.SetAttributes(dir, attributes);

                    WatcherChangeTypes expected = 0;
                    if (filter == NotifyFilters.Attributes)
                    {
                        expected |= WatcherChangeTypes.Changed;
                    }
                    else if (OperatingSystem.IsLinux() && ((filter & LinuxFiltersForAttribute) > 0))
                    {
                        expected |= WatcherChangeTypes.Changed;
                    }
                    else if (OperatingSystem.IsMacOS() && ((filter & OSXFiltersForModify) > 0))
                    {
                        expected |= WatcherChangeTypes.Changed;
                    }
                    else if (OperatingSystem.IsMacOS() && ((filter & NotifyFilters.Security) > 0))
                    {
                        expected |= WatcherChangeTypes.Changed; // Attribute change on OSX is a ChangeOwner operation which passes the Security NotifyFilter.
                    }
                    ExpectEvent(watcher, expected, action, cleanup, dir);
                }
            }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
        }
Exemple #4
0
        public void FileSystemWatcher_File_Delete_MultipleFilters()
        {
            FileSystemWatcherTest.Execute(() =>
            {
                // Check delete events against multiple filters

                DirectoryInfo directory = Directory.CreateDirectory(GetTestFilePath());
                FileInfo fileOne        = new FileInfo(Path.Combine(directory.FullName, GetTestFileName()));
                FileInfo fileTwo        = new FileInfo(Path.Combine(directory.FullName, GetTestFileName()));
                FileInfo fileThree      = new FileInfo(Path.Combine(directory.FullName, GetTestFileName()));
                fileOne.Create().Dispose();
                fileTwo.Create().Dispose();
                fileThree.Create().Dispose();

                using (var watcher = new FileSystemWatcher(directory.FullName))
                {
                    watcher.Filters.Add(fileOne.Name);
                    watcher.Filters.Add(fileTwo.Name);

                    ExpectEvent(watcher, WatcherChangeTypes.Deleted, () => fileOne.Delete(), cleanup: null, expectedPath: fileOne.FullName);
                    ExpectEvent(watcher, WatcherChangeTypes.Deleted, () => fileTwo.Delete(), cleanup: null, expectedPath: fileTwo.FullName);
                    ExpectNoEvent(watcher, WatcherChangeTypes.Deleted, () => fileThree.Delete(), cleanup: null, expectedPath: fileThree.FullName);
                }
            }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
        }
Exemple #5
0
        public void FileSystemWatcher_Directory_Changed_LastWrite()
        {
            FileSystemWatcherTest.Execute(() =>
            {
                string dir = CreateTestDirectory(TestDirectory, "dir");
                using (var watcher = new FileSystemWatcher(TestDirectory, Path.GetFileName(dir)))
                {
                    Action action = () => Directory.SetLastWriteTime(dir, DateTime.Now + TimeSpan.FromSeconds(10));

                    WatcherChangeTypes expected = WatcherChangeTypes.Changed;
                    ExpectEvent(watcher, expected, action, expectedPath: dir);
                }
            }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
        }
        public void FileSystemWatcher_File_Delete_SymLink()
        {
            FileSystemWatcherTest.Execute(() =>
            {
                string dir  = CreateTestDirectory(TestDirectory, "dir");
                string temp = CreateTestFile();
                using (var watcher = new FileSystemWatcher(dir, "*"))
                {
                    // Make the symlink in our path (to the temp file) and make sure an event is raised
                    string symLinkPath = Path.Combine(dir, GetRandomLinkName());
                    Action action      = () => File.Delete(symLinkPath);
                    Action cleanup     = () => Assert.True(MountHelper.CreateSymbolicLink(symLinkPath, temp, false));
                    cleanup();

                    ExpectEvent(watcher, WatcherChangeTypes.Deleted, action, cleanup, symLinkPath);
                }
            }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
        }
Exemple #7
0
 public void EndInit_ResumesPausedEnableRaisingEvents(bool setBeforeBeginInit)
 {
     FileSystemWatcherTest.Execute(() =>
     {
         using (var watcher = new TestFileSystemWatcher(TestDirectory, "*"))
         {
             if (setBeforeBeginInit)
             {
                 watcher.EnableRaisingEvents = true;
             }
             watcher.BeginInit();
             if (!setBeforeBeginInit)
             {
                 watcher.EnableRaisingEvents = true;
             }
             watcher.EndInit();
             ExpectEvent(watcher, WatcherChangeTypes.Created | WatcherChangeTypes.Deleted, () => new TempFile(Path.Combine(TestDirectory, GetTestFileName())).Dispose(), null);
         }
     }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
 }
        public void FileSystemWatcher_File_Create_SynchronizingObject()
        {
            FileSystemWatcherTest.Execute(() =>
            {
                using (var watcher = new FileSystemWatcher(TestDirectory))
                {
                    TestISynchronizeInvoke invoker = new TestISynchronizeInvoke();
                    watcher.SynchronizingObject    = invoker;

                    string fileName = Path.Combine(TestDirectory, "file");
                    watcher.Filter  = Path.GetFileName(fileName);

                    Action action  = () => File.Create(fileName).Dispose();
                    Action cleanup = () => File.Delete(fileName);

                    ExpectEvent(watcher, WatcherChangeTypes.Created, action, cleanup, fileName);
                    Assert.True(invoker.BeginInvoke_Called);
                }
            }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
        }
Exemple #9
0
        public void FileSystemWatcher_Directory_Create_Filter_Ctor()
        {
            FileSystemWatcherTest.Execute(() =>
            {
                // Check create events against multiple filters

                DirectoryInfo directory = Directory.CreateDirectory(GetTestFilePath());
                string directoryOne     = Path.Combine(directory.FullName, GetTestFileName());
                string directoryTwo     = Path.Combine(directory.FullName, GetTestFileName());
                string directoryThree   = Path.Combine(directory.FullName, GetTestFileName());

                using (var watcher = new FileSystemWatcher(directory.FullName, Path.GetFileName(directoryOne)))
                {
                    watcher.Filters.Add(Path.GetFileName(directoryTwo));

                    ExpectEvent(watcher, WatcherChangeTypes.Created, () => Directory.CreateDirectory(directoryOne), cleanup: null, expectedPath: directoryOne);
                    ExpectEvent(watcher, WatcherChangeTypes.Created, () => Directory.CreateDirectory(directoryTwo), cleanup: null, expectedPath: directoryTwo);
                    ExpectNoEvent(watcher, WatcherChangeTypes.Created, () => Directory.CreateDirectory(directoryThree), cleanup: null, expectedPath: directoryThree);
                }
            }, maxAttempts: DefaultAttemptsForExpectedEvent, backoffFunc: (iteration) => RetryDelayMilliseconds, retryWhen: e => e is XunitException);
        }