示例#1
0
        public void BatchMultipleEvents()
        {
            List <string>        events = new List <string>();
            FakeDirectoryWatcher inner  = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            Queue <TaskCompletionSource <bool> > pending = new Queue <TaskCompletionSource <bool> >();
            IDirectoryWatcher watcher = new DirectoryWatcherWithBatching(
                inner,
                @"X:\root",
                new BatchedEvents <FileInfo>(() => pending.Dequeue().Task));
            TaskCompletionSource <bool> pending1 = new TaskCompletionSource <bool>();
            TaskCompletionSource <bool> pending2 = new TaskCompletionSource <bool>();

            pending.Enqueue(pending1);
            pending.Enqueue(pending2);

            IDisposable sub1 = watcher.Subscribe("file1.txt", f => events.Add("(Updated1) " + f.FullName));

            watcher.Subscribe("file2.txt", f => events.Add("(Updated2) " + f.FullName));
            inner.Update(@"X:\root\file1.txt");
            inner.Update(@"X:\root\file2.txt");
            sub1.Dispose();
            inner.Update(@"X:\root\file1.txt");
            pending1.SetResult(true);

            events.Should().BeEmpty();

            inner.Update(@"X:\root\file2.txt");
            pending2.SetResult(true);
            inner.Update(@"X:\root\file1.txt");
            watcher.Dispose();
            inner.Update(@"X:\root\file2.txt");

            events.Should().ContainSingle().Which.Should().Be(@"(Updated2) X:\root\file2.txt");
        }
示例#2
0
            protected override IDirectoryWatcher Create(DirectoryInfo path)
            {
                FakeDirectoryWatcher watcher = new FakeDirectoryWatcher(path);

                this.Watchers.Add(watcher);
                return(watcher);
            }
        public void CreateAndDispose()
        {
            DirectoryWatcherBase watcher = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));

            Action act = () => watcher.Dispose();

            act.Should().NotThrow();
        }
        public void UpdateZeroSubscriptions()
        {
            FakeDirectoryWatcher watcher = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));

            Action act = () => watcher.Update(@"X:\root\file1.txt");

            act.Should().NotThrow();
        }
        public void SubscribeNullCallback()
        {
            DirectoryWatcherBase watcherBase = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            Action <FileInfo>    onUpdate    = null;

            Action act = () => watcherBase.Subscribe("file1.txt", onUpdate);

            act.Should().Throw <ArgumentNullException>().Which.ParamName.Should().Be("onUpdate");
        }
        public void SubscribeFileRootedPath()
        {
            DirectoryWatcherBase watcherBase = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            Action <FileInfo>    onUpdate    = f => { };

            Action act = () => watcherBase.Subscribe(@"D:\path.txt", onUpdate);

            ArgumentException ae = act.Should().Throw <ArgumentException>().Which;

            ae.Message.Should().Contain(@"Invalid file name 'D:\path.txt'");
            ae.ParamName.Should().Be("file");
        }
        public void SubscribeDotFileName()
        {
            DirectoryWatcherBase watcherBase = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            Action <FileInfo>    onUpdate    = f => { };

            Action act = () => watcherBase.Subscribe(".", onUpdate);

            ArgumentException ae = act.Should().Throw <ArgumentException>().Which;

            ae.Message.Should().Contain(@"The file '.' is not directly within directory 'X:\root'");
            ae.ParamName.Should().Be("file");
        }
        public void SubscribeEmptyFileName()
        {
            DirectoryWatcherBase watcherBase = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            Action <FileInfo>    onUpdate    = f => { };

            Action act = () => watcherBase.Subscribe(string.Empty, onUpdate);

            ArgumentException ae = act.Should().Throw <ArgumentException>().Which;

            ae.Message.Should().Contain("File name cannot be empty");
            ae.ParamName.Should().Be("file");
        }
        public void UpdateIrrelevantFileOneSubscription()
        {
            List <string>        updates     = new List <string>();
            FakeDirectoryWatcher watcher     = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            DirectoryWatcherBase watcherBase = watcher;

            watcherBase.Subscribe("file1.txt", f => updates.Add(f.FullName));

            watcher.Update(@"X:\root\not-relevant.txt");

            updates.Should().BeEmpty();
        }
        public void SubscribeSameFileTwiceDifferentCase()
        {
            DirectoryWatcherBase watcherBase = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            Action <FileInfo>    onUpdate    = f => { };

            watcherBase.Subscribe("file1.txt", onUpdate);

            Action act = () => watcherBase.Subscribe("FILE1.txt", onUpdate);

            act.Should().Throw <InvalidOperationException>()
            .WithMessage(@"A subscription for 'X:\root\FILE1.txt' already exists.");
        }
        public void UpdateRelevantFileOneSubscriptionDifferentCase()
        {
            List <string>        updates     = new List <string>();
            FakeDirectoryWatcher watcher     = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            DirectoryWatcherBase watcherBase = watcher;

            watcherBase.Subscribe("file1.txt", f => updates.Add(f.FullName));

            watcher.Update(@"X:\root\FILE1.txt");

            updates.Should().ContainSingle().Which.Should().Be(@"X:\root\file1.txt");
        }
        public void SubscribeBadFileName()
        {
            DirectoryWatcherBase watcherBase = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            Action <FileInfo>    onUpdate    = f => { };

            Action act = () => watcherBase.Subscribe("**BADNAME??", onUpdate);

            ArgumentException ae = act.Should().Throw <ArgumentException>().Which;

            ae.Message.Should().Contain("Invalid file name '**BADNAME??'");
            ae.ParamName.Should().Be("file");
        }
        public void UpdateAfterDispose()
        {
            List <string>        updates = new List <string>();
            FakeDirectoryWatcher watcher = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));

            using (DirectoryWatcherBase watcherBase = watcher)
            {
                watcherBase.Subscribe("file1.txt", f => updates.Add(f.FullName));
            }

            watcher.Update(@"X:\root\file1.txt");

            updates.Should().BeEmpty();
        }
        public void UpdateTwoAfterOneSubscriptionDispose()
        {
            List <string>        updates     = new List <string>();
            FakeDirectoryWatcher watcher     = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            DirectoryWatcherBase watcherBase = watcher;

            using (watcherBase.Subscribe("file1.txt", f => updates.Add(f.FullName)))
            {
            }

            watcherBase.Subscribe("file2.txt", f => updates.Add(f.FullName));
            watcher.Update(@"X:\root\file1.txt");
            watcher.Update(@"X:\root\file2.txt");

            updates.Should().ContainSingle().Which.Should().Be(@"X:\root\file2.txt");
        }
        public void UpdateTwoFilesTwoSubscriptions()
        {
            List <string>        updates     = new List <string>();
            FakeDirectoryWatcher watcher     = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            DirectoryWatcherBase watcherBase = watcher;

            watcherBase.Subscribe("file1.txt", f => updates.Add(f.FullName));
            watcherBase.Subscribe("file2.txt", f => updates.Add(f.FullName));

            watcher.Update(@"X:\root\file2.txt");
            watcher.Update(@"X:\root\file1.txt");

            updates.Should().HaveCount(2).And.ContainInOrder(
                @"X:\root\file2.txt",
                @"X:\root\file1.txt");
        }
示例#16
0
        public void UpdateTwoFilesTwoSubscriptionsSameDirDifferentCase()
        {
            List <string>            updates     = new List <string>();
            FakeDirectoryTreeWatcher watcher     = new FakeDirectoryTreeWatcher(new DirectoryInfo(@"X:\root"));
            DirectoryTreeWatcherBase watcherBase = watcher;

            watcherBase.Subscribe(@"inner\file1.txt", f => updates.Add(f.FullName));
            watcherBase.Subscribe(@"INNER\file2.txt", f => updates.Add(f.FullName));

            FakeDirectoryWatcher innerWatcher = watcher.Watchers.Should().ContainSingle().Which;

            innerWatcher.Update(@"X:\root\inner\file2.txt");
            innerWatcher.Update(@"X:\root\inner\file1.txt");

            updates.Should().HaveCount(2).And.ContainInOrder(
                @"X:\root\inner\file2.txt",
                @"X:\root\inner\file1.txt");
        }
示例#17
0
        public void AfterSubscriptionDisposeSubscribeAgainAndUpdate()
        {
            List <string>            updates     = new List <string>();
            FakeDirectoryTreeWatcher watcher     = new FakeDirectoryTreeWatcher(new DirectoryInfo(@"X:\root"));
            DirectoryTreeWatcherBase watcherBase = watcher;

            using (watcherBase.Subscribe("file1.txt", f => updates.Add(f.FullName)))
            {
            }

            watcherBase.Subscribe("file1.txt", f => updates.Add(f.FullName));
            watcherBase.Subscribe("file2.txt", f => updates.Add(f.FullName));
            FakeDirectoryWatcher innerWatcher = watcher.Watchers.Should().ContainSingle().Which;

            innerWatcher.Update(@"X:\root\file1.txt");
            innerWatcher.Update(@"X:\root\file2.txt");

            updates.Should().HaveCount(2).And.ContainInOrder(
                @"X:\root\file1.txt",
                @"X:\root\file2.txt");
        }
        public void LogOnSubscribeUnsubscribeDispose()
        {
            List <string>        logLines = new List <string>();
            FakeDirectoryWatcher inner    = new FakeDirectoryWatcher(new DirectoryInfo(@"X:\root"));
            IDirectoryWatcher    watcher  = new DirectoryWatcherWithLogging(inner, "[\\Path]", new FakeLogger(logLines));

            IDisposable sub1 = watcher.Subscribe("file1.txt", f => logLines.Add("(Updated1) " + f.FullName));

            watcher.Subscribe("file2.txt", f => logLines.Add("(Updated2) " + f.FullName));
            inner.Update(@"X:\root\file1.txt");
            sub1.Dispose();
            inner.Update(@"X:\root\file1.txt");
            inner.Update(@"X:\root\file2.txt");
            watcher.Dispose();
            inner.Update(@"X:\root\file2.txt");

            logLines.Should().HaveCount(6).And.ContainInOrder(
                @"Information: Subscribing '[\Path]\file1.txt' watcher",
                @"Information: Subscribing '[\Path]\file2.txt' watcher",
                @"(Updated1) X:\root\file1.txt",
                @"Information: Disposing '[\Path]\file1.txt' watcher",
                @"(Updated2) X:\root\file2.txt",
                @"Information: Disposing '[\Path]' watcher");
        }