Exemple #1
0
        public async Task TokensFiredOnFileDeleted()
        {
            using (var root = new DisposableFileSystem())
            {
                var fileName     = Guid.NewGuid().ToString();
                var fileLocation = Path.Combine(root.RootPath, fileName);

                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var token = provider.Watch(fileName);
                            Assert.NotNull(token);
                            Assert.False(token.HasChanged);
                            Assert.True(token.ActiveChangeCallbacks);

                            fileSystemWatcher.CallOnDeleted(new FileSystemEventArgs(WatcherChangeTypes.Deleted, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #2
0
        private static async Task Test1()
        {
            var tcs = new TaskCompletionSource <object>();

            string path = Directory.GetCurrentDirectory() + "\\data";

            using (var fileSystemWatcher = new FileSystemWatcher())
            {
                using (var physicalFilesWatcher = new PhysicalFilesWatcher(path + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                {
                    using (var provider = new PhysicalFileProvider(path))
                    {
                        var token = provider.Watch("*.txt");
                        //Assert.NotNull(token);
                        //Assert.False(token.HasChanged);
                        //Assert.True(token.ActiveChangeCallbacks);

                        WaitForChangedResult result = fileSystemWatcher.WaitForChanged(WatcherChangeTypes.All);


                        //fileSystemWatcher.Changed += new FileSystemEventArgs(WatcherChangeTypes.All, path, path));
                        //await Task.Delay(WaitTimeForTokenToFire);

                        //Assert.True(token.HasChanged);
                    }
                }
            }
            await tcs.Task.ConfigureAwait(false);
        }
Exemple #3
0
        public async Task CorrectTokensFiredForMultipleFiles()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var fileName1 = Guid.NewGuid().ToString();
                            var token1    = provider.Watch(fileName1);
                            var fileName2 = Guid.NewGuid().ToString();
                            var token2    = provider.Watch(fileName2);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName1));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token1.HasChanged);
                            Assert.False(token2.HasChanged);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName2));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token2.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #4
0
        public async Task WatcherWithPolling_ReturnsTrueForChangedFileWhenQueriedMultipleTimes()
        {
            using (var root = new DisposableFileSystem())
            {
                var fileName     = Path.GetRandomFileName();
                var fileLocation = Path.Combine(root.RootPath, fileName);
                PollingFileChangeToken.PollingInterval = TimeSpan.FromMilliseconds(10);

                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: true))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            root.CreateFile(fileName);
                            var token = provider.Watch(fileName);
                            File.Delete(fileLocation);

                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #5
0
        public async Task TokenNotAffectedByExceptions()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var fileName = Guid.NewGuid().ToString();
                            var token    = provider.Watch(fileName);

                            token.RegisterChangeCallback(_ =>
                            {
                                throw new Exception();
                            }, null);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #6
0
        public async Task TokensFiredForAllEntriesOnError()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var token1 = provider.Watch(Guid.NewGuid().ToString());
                            var token2 = provider.Watch(Guid.NewGuid().ToString());
                            var token3 = provider.Watch(Guid.NewGuid().ToString());

                            fileSystemWatcher.CallOnError(new ErrorEventArgs(new Exception()));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token1.HasChanged);
                            Assert.True(token2.HasChanged);
                            Assert.True(token3.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #7
0
        public async Task TokenFiredForGlobbingPatternsPointingToSubDirectory()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var subDirectoryName    = Guid.NewGuid().ToString();
                            var subSubDirectoryName = Guid.NewGuid().ToString();
                            var fileName            = Guid.NewGuid().ToString() + ".cshtml";

                            root.CreateFolder(subDirectoryName)
                            .CreateFolder(Path.Combine(subDirectoryName, subSubDirectoryName))
                            .CreateFile(Path.Combine(subDirectoryName, subSubDirectoryName, fileName));

                            var pattern = string.Format(Path.Combine(subDirectoryName, "**", "*.cshtml"));
                            var token   = provider.Watch(pattern);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, Path.Combine(root.RootPath, subDirectoryName, subSubDirectoryName), fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #8
0
        public async Task TokenFiredForFilesUnderPathEndingWithSlash()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var directoryName = Guid.NewGuid().ToString();
                            root.CreateFolder(directoryName)
                            .CreateFile(Path.Combine(directoryName, "some-file"));
                            var newDirectory = Path.GetRandomFileName();

                            var token = provider.Watch(directoryName + Path.DirectorySeparatorChar);

                            Directory.Move(
                                Path.Combine(root.RootPath, directoryName),
                                Path.Combine(root.RootPath, newDirectory));

                            fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(
                                                                WatcherChangeTypes.Renamed,
                                                                root.RootPath,
                                                                newDirectory,
                                                                directoryName));

                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #9
0
        public async Task FileChangeTokenNotNotifiedAfterExpiry()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var fileName        = Guid.NewGuid().ToString();
                            var changeToken     = provider.Watch(fileName);
                            var invocationCount = 0;
                            changeToken.RegisterChangeCallback(_ => { invocationCount++; }, null);

                            // Callback expected.
                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            // Callback not expected.
                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.Equal(1, invocationCount);
                        }
                    }
                }
            }
        }
Exemple #10
0
        public async Task TokensFiredForOldAndNewNamesOnRename()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var oldFileName = Guid.NewGuid().ToString();
                            var oldToken    = provider.Watch(oldFileName);

                            var newFileName = Guid.NewGuid().ToString();
                            var newToken    = provider.Watch(newFileName);

                            fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, root.RootPath, newFileName, oldFileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(oldToken.HasChanged);
                            Assert.True(newToken.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #11
0
        public void RaiseChangeEvents_CancelsCancellationTokenSourceForExpiredTokens()
        {
            // Arrange
            var cts1 = new CancellationTokenSource();
            var cts2 = new CancellationTokenSource();
            var cts3 = new CancellationTokenSource();

            var token1 = new TestPollingChangeToken {
                Id = 1, CancellationTokenSource = cts1
            };
            var token2 = new TestPollingChangeToken {
                Id = 2, HasChanged = true, CancellationTokenSource = cts2
            };
            var token3 = new TestPollingChangeToken {
                Id = 3, CancellationTokenSource = cts3
            };

            var tokens = new ConcurrentDictionary <IPollingChangeToken, IPollingChangeToken>
            {
                [token1] = token1,
                [token2] = token2,
                [token3] = token3,
            };

            // Act
            PhysicalFilesWatcher.RaiseChangeEvents(tokens);

            // Assert
            Assert.False(cts1.IsCancellationRequested);
            Assert.False(cts3.IsCancellationRequested);
            Assert.True(cts2.IsCancellationRequested);

            // Ensure token2 is removed from the collection.
            Assert.Equal(new[] { token1, token3, }, tokens.Keys.OfType <TestPollingChangeToken>().OrderBy(t => t.Id));
        }
Exemple #12
0
 public FileSystemContentBlobProvider(IOptions <FileSystemBlobContentOptions> options, IMemoryCache memoryCache)
 {
     _options     = options.Value;
     _memoryCache = memoryCache;
     //Create fileSystemWatcher instance only when rootFolder exist to prevent whole application crash on initialization phase.
     if (Directory.Exists(_options.Path))
     {
         _fileSystemWatcher = new PhysicalFilesWatcher(_options.Path, new FileSystemWatcher(_options.Path), _options.PollForChanges);
     }
 }
Exemple #13
0
 public FileSystemContentBlobProvider(IOptions <FileSystemBlobContentOptions> options, IStorefrontMemoryCache memoryCache)
 {
     _options     = options.Value;
     _memoryCache = memoryCache;
     //Create fileSystemWatcher instance only when rootFolder exist to prevent whole application crash on initialization phase.
     if (Directory.Exists(_options.Path))
     {
         //It is very important to have rootPath with leading slash '\' without this any changes won't reflected
         var rootPath = _options.Path.TrimEnd('\\') + '\\';
         _fileSystemWatcher = new PhysicalFilesWatcher(rootPath, new FileSystemWatcher(rootPath), false);
     }
 }
Exemple #14
0
        public void GetOrAddFilePathChangeToken_DoesNotAddsPollingChangeTokenWhenCallbackIsDisabled()
        {
            using (var root = new DisposableFileSystem())
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        var changeToken = physicalFilesWatcher.GetOrAddFilePathChangeToken("some-path");

                        Assert.IsType <CancellationChangeToken>(changeToken);
                        Assert.Empty(physicalFilesWatcher.PollingChangeTokens);
                    }
        }
Exemple #15
0
        public void RaiseChangeEvents_CancelsAndRemovesMultipleChangedTokens()
        {
            // Arrange
            var cts1 = new CancellationTokenSource();
            var cts2 = new CancellationTokenSource();
            var cts3 = new CancellationTokenSource();
            var cts4 = new CancellationTokenSource();
            var cts5 = new CancellationTokenSource();

            var token1 = new TestPollingChangeToken {
                Id = 1, HasChanged = true, CancellationTokenSource = cts1
            };
            var token2 = new TestPollingChangeToken {
                Id = 2, CancellationTokenSource = cts2
            };
            var token3 = new TestPollingChangeToken {
                Id = 3, CancellationTokenSource = cts3
            };
            var token4 = new TestPollingChangeToken {
                Id = 4, HasChanged = true, CancellationTokenSource = cts4
            };
            var token5 = new TestPollingChangeToken {
                Id = 5, HasChanged = true, CancellationTokenSource = cts5
            };

            var tokens = new ConcurrentDictionary <IPollingChangeToken, IPollingChangeToken>
            {
                [token1] = token1,
                [token2] = token2,
                [token3] = token3,
                [token4] = token4,
                [token5] = token5,
            };

            // Act
            PhysicalFilesWatcher.RaiseChangeEvents(tokens);

            // Assert
            Assert.False(cts2.IsCancellationRequested);
            Assert.False(cts3.IsCancellationRequested);

            Assert.True(cts1.IsCancellationRequested);
            Assert.True(cts4.IsCancellationRequested);
            Assert.True(cts5.IsCancellationRequested);

            // Ensure changed tokens are removed
            Assert.Equal(new[] { token2, token3, }, tokens.Keys.OfType <TestPollingChangeToken>().OrderBy(t => t.Id));
        }
        internal PhysicalFileProvider(string root, PhysicalFilesWatcher physicalFilesWatcher)
        {
            if (!Path.IsPathRooted(root))
            {
                throw new ArgumentException("The path must be absolute.", nameof(root));
            }
            var fullRoot = Path.GetFullPath(root);
            // When we do matches in GetFullPath, we want to only match full directory names.
            Root = EnsureTrailingSlash(fullRoot);
            if (!Directory.Exists(Root))
            {
                throw new DirectoryNotFoundException(Root);
            }

            _filesWatcher = physicalFilesWatcher;
        }
Exemple #17
0
        private PhysicalFileProvider(string root, PhysicalFilesWatcher physicalFilesWatcher, ExclusionFilters filters)
        {
            if (!Path.IsPathRooted(root))
            {
                throw new ArgumentException("The path must be absolute.", nameof(root));
            }

            Root = PathUtils.EnsureTrailingSlash(Path.GetFullPath(root));
            if (!Directory.Exists(Root))
            {
                throw new DirectoryNotFoundException(Root);
            }

            _filesWatcher = physicalFilesWatcher;
            _filters      = filters;
        }
Exemple #18
0
        public void CreateFileChangeToken_DoesNotAllowPathsAboveRoot()
        {
            using (var root = new DisposableFileSystem())
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        var token = physicalFilesWatcher.CreateFileChangeToken(Path.GetFullPath(Path.Combine(root.RootPath, "..")));
                        Assert.IsType <NullChangeToken>(token);

                        token = physicalFilesWatcher.CreateFileChangeToken(Path.GetFullPath(Path.Combine(root.RootPath, "../")));
                        Assert.IsType <NullChangeToken>(token);

                        token = physicalFilesWatcher.CreateFileChangeToken("..");
                        Assert.IsType <NullChangeToken>(token);
                    }
        }
        internal PhysicalFileProvider(string root, PhysicalFilesWatcher physicalFilesWatcher)
        {
            if (!Path.IsPathRooted(root))
            {
                throw new ArgumentException("The path must be absolute.", nameof(root));
            }
            var fullRoot = Path.GetFullPath(root);

            // When we do matches in GetFullPath, we want to only match full directory names.
            Root = PathUtils.EnsureTrailingSlash(fullRoot);
            if (!Directory.Exists(Root))
            {
                throw new DirectoryNotFoundException(Root);
            }

            _filesWatcher = physicalFilesWatcher;
        }
Exemple #20
0
        public async Task HandlesOnRenamedEventsThatMatchRootPath()
        {
            using (var root = new DisposableFileSystem())
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        var token  = physicalFilesWatcher.CreateFileChangeToken("**");
                        var called = false;
                        token.RegisterChangeCallback(o => called = true, null);

                        fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, root.RootPath, string.Empty, string.Empty));
                        await Task.Delay(WaitTimeForTokenToFire).ConfigureAwait(false);

                        Assert.False(called, "Callback should not have been triggered");

                        fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, root.RootPath, "old.txt", "new.txt"));
                        await Task.Delay(WaitTimeForTokenToFire).ConfigureAwait(false);

                        Assert.True(called, "Callback should have been triggered");
                    }
        }
Exemple #21
0
        public async Task TokensNotFiredForHiddenAndSystemFiles()
        {
            using (var root = new DisposableFileSystem())
            {
                var hiddenFileName = Guid.NewGuid().ToString();
                var hiddenFilePath = Path.Combine(root.RootPath, hiddenFileName);
                File.Create(hiddenFilePath);
                var fileInfo = new FileInfo(hiddenFilePath);
                File.SetAttributes(hiddenFilePath, fileInfo.Attributes | FileAttributes.Hidden);

                var systemFileName = Guid.NewGuid().ToString();
                var systemFilePath = Path.Combine(root.RootPath, systemFileName);
                File.Create(systemFilePath);
                fileInfo = new FileInfo(systemFilePath);
                File.SetAttributes(systemFilePath, fileInfo.Attributes | FileAttributes.System);

                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var hiddenFiletoken = provider.Watch(Path.GetFileName(hiddenFileName));
                            var systemFiletoken = provider.Watch(Path.GetFileName(systemFileName));

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, hiddenFileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.False(hiddenFiletoken.HasChanged);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, systemFileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.False(systemFiletoken.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #22
0
        public void GetOrAddFilePathChangeToken_AddsPollingChangeTokenWhenPollingIsEnabled()
        {
            using (var root = new DisposableFileSystem())
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: true))
                    {
                        var changeToken = physicalFilesWatcher.GetOrAddFilePathChangeToken("some-path");

                        var compositeChangeToken = Assert.IsType <CompositeChangeToken>(changeToken);
                        Assert.Collection(
                            compositeChangeToken.ChangeTokens,
                            token => Assert.IsType <CancellationChangeToken>(token),
                            token =>
                        {
                            var pollingChangeToken = Assert.IsType <PollingFileChangeToken>(token);
                            Assert.Null(pollingChangeToken.CancellationTokenSource);
                            Assert.False(pollingChangeToken.ActiveChangeCallbacks);
                        });

                        Assert.Empty(physicalFilesWatcher.PollingChangeTokens);
                    }
        }
Exemple #23
0
        public async Task TokenNotFiredForFileNameStartingWithPeriod()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var fileName = "." + Guid.NewGuid().ToString();
                            var token    = provider.Watch(Path.GetFileName(fileName));

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.False(token.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #24
0
        public async Task TokenFiredOnCreation()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: false))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var name  = Guid.NewGuid().ToString();
                            var token = provider.Watch(name);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Created, root.RootPath, name));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #25
0
        public async Task WatcherWithPolling_ReturnsTrueForFileChangedWhenFileSystemWatcherDoesNotRaiseEvents()
        {
            using (var root = new DisposableFileSystem())
            {
                var fileName     = Path.GetRandomFileName();
                var fileLocation = Path.Combine(root.RootPath, fileName);
                PollingFileChangeToken.PollingInterval = TimeSpan.FromMilliseconds(10);

                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: true))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var token = provider.Watch(fileName);
                            File.WriteAllText(fileLocation, "some-content");
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #26
0
        public void GetOrAddWildcardChangeToken_AddsPollingChangeTokenWithCancellationToken_WhenActiveCallbackIsTrue()
        {
            using (var root = new DisposableFileSystem())
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher, pollForChanges: true))
                    {
                        physicalFilesWatcher.UseActivePolling = true;

                        var changeToken = physicalFilesWatcher.GetOrAddWildcardChangeToken("*.cshtml");

                        var compositeChangeToken = Assert.IsType <CompositeChangeToken>(changeToken);
                        Assert.Collection(
                            compositeChangeToken.ChangeTokens,
                            token => Assert.IsType <CancellationChangeToken>(token),
                            token =>
                        {
                            var pollingChangeToken = Assert.IsType <PollingWildCardChangeToken>(token);
                            Assert.NotNull(pollingChangeToken.CancellationTokenSource);
                            Assert.True(pollingChangeToken.ActiveChangeCallbacks);
                        });

                        Assert.NotEmpty(physicalFilesWatcher.PollingChangeTokens);
                    }
        }
        public async Task CorrectTokensFiredForMultipleFiles()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var fileName1 = Guid.NewGuid().ToString();
                            var token1 = provider.Watch(fileName1);
                            var fileName2 = Guid.NewGuid().ToString();
                            var token2 = provider.Watch(fileName2);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName1));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token1.HasChanged);
                            Assert.False(token2.HasChanged);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName2));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token2.HasChanged);
                        }
                    }
                }
            }
        }
        public async Task TokenNotAffectedByExceptions()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var fileName = Guid.NewGuid().ToString();
                            var token = provider.Watch(fileName);

                            token.RegisterChangeCallback(_ =>
                            {
                                throw new Exception();
                            }, null);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
        public async Task TokensFiredOnFileDeleted()
        {
            using (var root = new DisposableFileSystem())
            {
                var fileName = Guid.NewGuid().ToString();
                var fileLocation = Path.Combine(root.RootPath, fileName);

                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var token = provider.Watch(fileName);
                            Assert.NotNull(token);
                            Assert.False(token.HasChanged);
                            Assert.True(token.ActiveChangeCallbacks);

                            fileSystemWatcher.CallOnDeleted(new FileSystemEventArgs(WatcherChangeTypes.Deleted, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
        public async Task TokenFiredForFilesUnderPathEndingWithSlash()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var directoryName = Guid.NewGuid().ToString();
                            var token = provider.Watch(directoryName + Path.DirectorySeparatorChar);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, Path.Combine(root.RootPath, directoryName), Guid.NewGuid().ToString()));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
        public async Task TokenFiredOnDeletion()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var name = Guid.NewGuid().ToString();
                            var token = provider.Watch(name);

                            fileSystemWatcher.CallOnDeleted(new FileSystemEventArgs(WatcherChangeTypes.Deleted, root.RootPath, name));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
        public async Task TokensFiredForRegularExpressionPatterns()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var token1 = provider.Watch("**/*");
                            var pattern1tokenCount = 0;
                            token1.RegisterChangeCallback(_ => { pattern1tokenCount++; }, null);

                            var token2 = provider.Watch("*.cshtml");
                            var pattern2tokenCount = 0;
                            token2.RegisterChangeCallback(_ => { pattern2tokenCount++; }, null);

                            var fileName = Guid.NewGuid().ToString();

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName + ".cshtml"));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.Equal(1, pattern1tokenCount);
                            Assert.Equal(1, pattern2tokenCount);

                            token1 = provider.Watch("**/*");
                            token1.RegisterChangeCallback(_ => { pattern1tokenCount++; }, null);
                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName + ".txt"));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.Equal(2, pattern1tokenCount);
                            Assert.Equal(1, pattern2tokenCount);
                        }
                    }
                }
            }
        }
        public async Task TokenNotFiredForFileNameStartingWithPeriod()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var fileName = "." + Guid.NewGuid().ToString();
                            var token = provider.Watch(Path.GetFileName(fileName));

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.False(token.HasChanged);
                        }
                    }
                }
            }
        }
        public async Task TokensFiredForAllEntriesOnError()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var token1 = provider.Watch(Guid.NewGuid().ToString());
                            var token2 = provider.Watch(Guid.NewGuid().ToString());
                            var token3 = provider.Watch(Guid.NewGuid().ToString());

                            fileSystemWatcher.CallOnError(new ErrorEventArgs(new Exception()));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token1.HasChanged);
                            Assert.True(token2.HasChanged);
                            Assert.True(token3.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #35
0
 internal PhysicalFileProvider(string root, PhysicalFilesWatcher physicalFilesWatcher) : this(root, physicalFilesWatcher, ExclusionFilters.Sensitive)
 {
 }
Exemple #36
0
 public FileVersionHashProvider(string rootPath, IPlatformMemoryCache platformMemoryCache)
 {
     _platformMemoryCache = platformMemoryCache;
     _rootPath            = rootPath.TrimEnd(Path.DirectorySeparatorChar) + Path.DirectorySeparatorChar;
     _fileSystemWatcher   = new PhysicalFilesWatcher(_rootPath, new FileSystemWatcher(_rootPath), false);
 }
        public async Task TokenFiredForRegularExpressionPatternsPointingToSubDirectory()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var subDirectoryName = Guid.NewGuid().ToString();
                            var pattern = string.Format(Path.Combine(subDirectoryName, "**", "*.cshtml"));
                            var token = provider.Watch(pattern);

                            var subSubDirectoryName = Guid.NewGuid().ToString();
                            var fileName = Guid.NewGuid().ToString() + ".cshtml";
                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, Path.Combine(root.RootPath, subDirectoryName, subSubDirectoryName), fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(token.HasChanged);
                        }
                    }
                }
            }
        }
        public async Task TokensFiredForOldAndNewNamesOnRename()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var oldFileName = Guid.NewGuid().ToString();
                            var oldToken = provider.Watch(oldFileName);

                            var newFileName = Guid.NewGuid().ToString();
                            var newToken = provider.Watch(newFileName);

                            fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, root.RootPath, newFileName, oldFileName));
                            await Task.Delay(WaitTimeForTokenToFire);
                            
                            Assert.True(oldToken.HasChanged);
                            Assert.True(newToken.HasChanged);
                        }
                    }
                }
            }
        }
        public async Task FileChangeTokenNotNotifiedAfterExpiry()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var fileName = Guid.NewGuid().ToString();
                            var changeToken = provider.Watch(fileName);
                            var invocationCount = 0;
                            changeToken.RegisterChangeCallback(_ => { invocationCount++; }, null);

                            // Callback expected.
                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            // Callback not expected.
                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, fileName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.Equal(1, invocationCount);
                        }
                    }
                }
            }
        }
        public async Task TokensNotFiredForHiddenAndSystemFiles()
        {
            using (var root = new DisposableFileSystem())
            {
                var hiddenFileName = Guid.NewGuid().ToString();
                var hiddenFilePath = Path.Combine(root.RootPath, hiddenFileName);
                File.Create(hiddenFilePath);
                var fileInfo = new FileInfo(hiddenFilePath);
                File.SetAttributes(hiddenFilePath, fileInfo.Attributes | FileAttributes.Hidden);

                var systemFileName = Guid.NewGuid().ToString();
                var systemFilePath = Path.Combine(root.RootPath, systemFileName);
                File.Create(systemFilePath);
                fileInfo = new FileInfo(systemFilePath);
                File.SetAttributes(systemFilePath, fileInfo.Attributes | FileAttributes.System);

                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var hiddenFiletoken = provider.Watch(Path.GetFileName(hiddenFileName));
                            var systemFiletoken = provider.Watch(Path.GetFileName(systemFileName));

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, hiddenFileName));
                            await Task.Delay(WaitTimeForTokenToFire);
                            Assert.False(hiddenFiletoken.HasChanged);

                            fileSystemWatcher.CallOnChanged(new FileSystemEventArgs(WatcherChangeTypes.Changed, root.RootPath, systemFileName));
                            await Task.Delay(WaitTimeForTokenToFire);
                            Assert.False(systemFiletoken.HasChanged);
                        }
                    }
                }
            }
        }
Exemple #41
0
 public FileVersionHashProvider(string rootPath, IPlatformMemoryCache platformMemoryCache)
 {
     _platformMemoryCache = platformMemoryCache;
     _rootPath            = rootPath.TrimEnd('\\') + '\\';
     _fileSystemWatcher   = new PhysicalFilesWatcher(_rootPath, new FileSystemWatcher(_rootPath), false);
 }
        public async Task TokensFiredForNewDirectoryContentsOnRename()
        {
            using (var root = new DisposableFileSystem())
            {
                using (var fileSystemWatcher = new MockFileSystemWatcher(root.RootPath))
                {
                    using (var physicalFilesWatcher = new PhysicalFilesWatcher(root.RootPath + Path.DirectorySeparatorChar, fileSystemWatcher))
                    {
                        using (var provider = new PhysicalFileProvider(root.RootPath, physicalFilesWatcher))
                        {
                            var oldDirectoryName = Guid.NewGuid().ToString();
                            var oldSubDirectoryName = Guid.NewGuid().ToString();
                            var oldSubDirectoryPath = Path.Combine(oldDirectoryName, oldSubDirectoryName);
                            var oldFileName = Guid.NewGuid().ToString();
                            var oldFilePath = Path.Combine(oldDirectoryName, oldSubDirectoryName, oldFileName);

                            var newDirectoryName = Guid.NewGuid().ToString();
                            var newSubDirectoryName = Guid.NewGuid().ToString();
                            var newSubDirectoryPath = Path.Combine(newDirectoryName, newSubDirectoryName);
                            var newFileName = Guid.NewGuid().ToString();
                            var newFilePath = Path.Combine(newDirectoryName, newSubDirectoryName, newFileName);

                            Directory.CreateDirectory(Path.Combine(root.RootPath, newDirectoryName));
                            Directory.CreateDirectory(Path.Combine(root.RootPath, newDirectoryName, newSubDirectoryName));
                            File.Create(Path.Combine(root.RootPath, newDirectoryName, newSubDirectoryName, newFileName));

                            await Task.Delay(WaitTimeForTokenToFire);

                            var oldDirectoryToken = provider.Watch(oldDirectoryName);
                            var oldSubDirectoryToken = provider.Watch(oldSubDirectoryPath);
                            var oldFileToken = provider.Watch(oldFilePath);

                            var newDirectoryToken = provider.Watch(newDirectoryName);
                            var newSubDirectoryToken = provider.Watch(newSubDirectoryPath);
                            var newFileToken = provider.Watch(newFilePath);

                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.False(oldDirectoryToken.HasChanged);
                            Assert.False(oldSubDirectoryToken.HasChanged);
                            Assert.False(oldFileToken.HasChanged);
                            Assert.False(newDirectoryToken.HasChanged);
                            Assert.False(newSubDirectoryToken.HasChanged);
                            Assert.False(newFileToken.HasChanged);

                            fileSystemWatcher.CallOnRenamed(new RenamedEventArgs(WatcherChangeTypes.Renamed, root.RootPath, newDirectoryName, oldDirectoryName));
                            await Task.Delay(WaitTimeForTokenToFire);

                            Assert.True(oldDirectoryToken.HasChanged);
                            Assert.False(oldSubDirectoryToken.HasChanged);
                            Assert.False(oldFileToken.HasChanged);
                            Assert.True(newDirectoryToken.HasChanged);
                            Assert.True(newSubDirectoryToken.HasChanged);
                            Assert.True(newFileToken.HasChanged);
                        }
                    }
                }
            }
        }