Ejemplo n.º 1
0
        static void Constructor_MissingFiles_Handler()
        {
            HostFileChangeMonitor monitor;
            PlatformID            pid = Environment.OSVersion.Platform;
            bool   runningOnWindows   = ((int)pid != 128 && pid != PlatformID.Unix && pid != PlatformID.MacOSX);
            string missingFile        = Path.Combine("missing", "file", "path");

            if (runningOnWindows)
            {
                missingFile = "c:\\" + missingFile;
            }
            else
            {
                missingFile = "/" + missingFile;
            }

            var paths = new List <string> {
                missingFile
            };

            // Actually thrown by FileSystemWatcher constructor - note that the exception message suggests the file's
            // parent directory is being watched, not the file itself:
            //
            // MonoTests.System.Runtime.Caching.HostFileChangeMonitorTest.Constructor_MissingFiles:
            // System.ArgumentException : The directory name c:\missing\file is invalid.
            // at System.IO.FileSystemWatcher..ctor(String path, String filter)
            // at System.IO.FileSystemWatcher..ctor(String path)
            // at System.Runtime.Caching.FileChangeNotificationSystem.System.Runtime.Caching.Hosting.IFileChangeNotificationSystem.StartMonitoring(String filePath, OnChangedCallback onChangedCallback, Object& state, DateTimeOffset& lastWriteTime, Int64& fileSize)
            // at System.Runtime.Caching.HostFileChangeMonitor.InitDisposableMembers()
            // at System.Runtime.Caching.HostFileChangeMonitor..ctor(IList`1 filePaths)
            // at MonoTests.System.Runtime.Caching.HostFileChangeMonitorTest.Constructor_MissingFiles() in c:\users\grendel\documents\visual studio 2010\Projects\System.Runtime.Caching.Test\System.Runtime.Caching.Test\System.Runtime.Caching\HostFileChangeMonitorTest.cs:line 68
            Assert.Throws <ArgumentException> (() => {
                monitor = new HostFileChangeMonitor(paths);
            }, "#A1");

            if (runningOnWindows)
            {
                missingFile = "c:\\file.txt";
            }
            else
            {
                missingFile = "/file.txt";
            }

            paths.Clear();
            paths.Add(missingFile);
            monitor = new HostFileChangeMonitor(paths);
            Assert.AreEqual(1, monitor.FilePaths.Count, "#A2-1");
            Assert.AreEqual(missingFile, monitor.FilePaths [0], "#A2-2");
            Assert.AreEqual(missingFile + "701CE1722770000FFFFFFFFFFFFFFFF", monitor.UniqueId, "#A2-4");
            monitor.Dispose();

            paths.Add(missingFile);
            monitor = new HostFileChangeMonitor(paths);
            Assert.AreEqual(2, monitor.FilePaths.Count, "#A3-1");
            Assert.AreEqual(missingFile, monitor.FilePaths [0], "#A3-2");
            Assert.AreEqual(missingFile, monitor.FilePaths [1], "#A3-3");
            Assert.AreEqual(missingFile + "701CE1722770000FFFFFFFFFFFFFFFF", monitor.UniqueId, "#A3-4");
            monitor.Dispose();
        }
Ejemplo n.º 2
0
        public void Constructor_Duplicates()
        {
            HostFileChangeMonitor monitor;
            PlatformID            pid = Environment.OSVersion.Platform;
            bool   runningOnWindows   = ((int)pid != 128 && pid != PlatformID.Unix && pid != PlatformID.MacOSX);
            string missingFile        = Path.Combine("missing", "file", "path");

            if (runningOnWindows)
            {
                missingFile = "c:\\file.txt";
            }
            else
            {
                missingFile = "/file.txt";
            }

            var paths = new List <string> {
                missingFile,
                missingFile
            };

            // Just checks if it doesn't throw any exception for dupes
            monitor = new HostFileChangeMonitor(paths);
            monitor.Dispose();
        }
        public void Constructor_Duplicates()
        {
            HostFileChangeMonitor monitor;
            string missingFile = Path.GetFullPath(Guid.NewGuid().ToString("N"));

            var paths = new List <string> {
                missingFile,
                missingFile
            };

            // Just checks if it doesn't throw any exception for dupes
            monitor = new HostFileChangeMonitor(paths);
            monitor.Dispose();
        }
        public void UniqueId()
        {
            Tuple <string, string, string, IList <string> > setup = null;

            try
            {
                setup = SetupMonitoring();
                FileInfo fi;
                var      monitor = new HostFileChangeMonitor(setup.Item4);
                var      sb      = new StringBuilder();

                fi = new FileInfo(setup.Item2);
                sb.AppendFormat("{0}{1:X}{2:X}",
                                setup.Item2,
                                fi.LastWriteTimeUtc.Ticks,
                                fi.Length);

                fi = new FileInfo(setup.Item3);
                sb.AppendFormat("{0}{1:X}{2:X}",
                                setup.Item3,
                                fi.LastWriteTimeUtc.Ticks,
                                fi.Length);

                Assert.Equal(sb.ToString(), monitor.UniqueId);

                var list = new List <string>(setup.Item4);
                list.Add(setup.Item1);

                monitor = new HostFileChangeMonitor(list);
                var di = new DirectoryInfo(setup.Item1);
                sb.AppendFormat("{0}{1:X}{2:X}",
                                setup.Item1,
                                di.LastWriteTimeUtc.Ticks,
                                -1L);
                Assert.Equal(sb.ToString(), monitor.UniqueId);

                list.Add(setup.Item1);
                monitor = new HostFileChangeMonitor(list);
                Assert.Equal(sb.ToString(), monitor.UniqueId);
                monitor.Dispose();
            }
            finally
            {
                CleanupMonitoring(setup);
            }
        }
        public static void Constructor_MissingFiles_Handler()
        {
            HostFileChangeMonitor monitor;
            string missingFile = Path.GetFullPath(Path.Combine(Guid.NewGuid().ToString("N"), "file", "path"));

            var paths = new List <string> {
                missingFile
            };

            // Actually thrown by FileSystemWatcher constructor - note that the exception message suggests the file's
            // parent directory is being watched, not the file itself:
            //
            // MonoTests.System.Runtime.Caching.HostFileChangeMonitorTest.Constructor_MissingFiles:
            // System.ArgumentException : The directory name c:\missing\file is invalid.
            // at System.IO.FileSystemWatcher..ctor(String path, String filter)
            // at System.IO.FileSystemWatcher..ctor(String path)
            // at System.Runtime.Caching.FileChangeNotificationSystem.System.Runtime.Caching.Hosting.IFileChangeNotificationSystem.StartMonitoring(String filePath, OnChangedCallback onChangedCallback, Object& state, DateTimeOffset& lastWriteTime, Int64& fileSize)
            // at System.Runtime.Caching.HostFileChangeMonitor.InitDisposableMembers()
            // at System.Runtime.Caching.HostFileChangeMonitor..ctor(IList`1 filePaths)
            // at MonoTests.System.Runtime.Caching.HostFileChangeMonitorTest.Constructor_MissingFiles() in c:\users\grendel\documents\visual studio 2010\Projects\System.Runtime.Caching.Test\System.Runtime.Caching.Test\System.Runtime.Caching\HostFileChangeMonitorTest.cs:line 68
            Assert.Throws <ArgumentException>(() =>
            {
                new HostFileChangeMonitor(paths);
            });

            missingFile = Path.GetFullPath(Guid.NewGuid().ToString("N"));

            paths.Clear();
            paths.Add(missingFile);
            monitor = new HostFileChangeMonitor(paths);
            Assert.Equal(1, monitor.FilePaths.Count);
            Assert.Equal(missingFile, monitor.FilePaths[0]);
            //??
            Assert.Equal(missingFile + "701CE1722770000FFFFFFFFFFFFFFFF", monitor.UniqueId);
            monitor.Dispose();

            paths.Add(missingFile);
            monitor = new HostFileChangeMonitor(paths);
            Assert.Equal(2, monitor.FilePaths.Count);
            Assert.Equal(missingFile, monitor.FilePaths[0]);
            Assert.Equal(missingFile, monitor.FilePaths[1]);
            //??
            Assert.Equal(missingFile + "701CE1722770000FFFFFFFFFFFFFFFF", monitor.UniqueId);
            monitor.Dispose();
        }
Ejemplo n.º 6
0
        private bool RegisterMoniter(string key, params string[] dependentFiles)
        {
            if (this._isDisposed)
            {
                return(false);
            }
            string moniterKey = key + "|" + string.Join("|", dependentFiles);

            if (!this._monitors.ContainsKey(moniterKey))
            {
                lock (this._monitors)
                {
                    if (!this._monitors.ContainsKey(moniterKey))
                    {
                        HostFileChangeMonitor moniter = new HostFileChangeMonitor(dependentFiles);
                        moniter.NotifyOnChanged(delegate(object state)
                        {
                            try
                            {
                                this.Remove(key);
                                moniter.Dispose();
                            }
                            catch
                            {
                            }
                            finally
                            {
                                this._monitors.Remove(moniterKey);
                                this.RegisterMoniter(key, dependentFiles);
                            }
                        });
                        this._monitors.Add(moniterKey, moniter);
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 7
0
        void DisposeOurself()
        {
            // guarantee that we execute only once if an exception
            // is thrown from this function by nulling fields before
            // we access them
            object l_depFileInfos = _depFileInfos;
            object l_entries      = _entries;

            _objNotify    = null;
            _depFileInfos = null;
            _entries      = null;

            // stop monitoring files
            if (l_depFileInfos != null)
            {
                FileChangesMonitor fmon = HttpRuntime.FileChangesMonitor;

                DepFileInfo oneDepFileInfo = l_depFileInfos as DepFileInfo;
                if (oneDepFileInfo != null)
                {
                    fmon.StopMonitoringPath(oneDepFileInfo._filename, this);
                }
                else
                {
                    DepFileInfo[] depFileInfos = (DepFileInfo[])l_depFileInfos;
                    foreach (DepFileInfo depFileInfo in depFileInfos)
                    {
                        // ensure that we handle partially contructed
                        // objects by checking filename for null
                        string filename = depFileInfo._filename;
                        if (filename != null)
                        {
                            fmon.StopMonitoringPath(filename, this);
                        }
                    }
                }
            }

            // stop monitoring cache items
            if (l_entries != null)
            {
                DepCacheInfo oneEntry = l_entries as DepCacheInfo;
                if (oneEntry != null)
                {
                    oneEntry._cacheStore.RemoveDependent(oneEntry._key, this);
                }
                else
                {
                    DepCacheInfo[] entries = (DepCacheInfo[])l_entries;
                    foreach (DepCacheInfo entry in entries)
                    {
                        // ensure that we handle partially contructed
                        // objects by checking entry for null
                        if (entry != null)
                        {
                            entry._cacheStore.RemoveDependent(entry._key, this);
                        }
                    }
                }
            }

#if USE_MEMORY_CACHE
            if (_fileChangeMonitor != null)
            {
                _fileChangeMonitor.Dispose();
            }
            if (_entryChangeMonitor != null)
            {
                _entryChangeMonitor.Dispose();
            }
#endif
        }