Exemple #1
0
        public async Task DisposedSource_DoesNotPublishEvents()
        {
            using (var directory = new TempDirectory())
                using (var eventManager = new ScriptEventManager())
                {
                    var events = new List <FileEvent>();
                    eventManager.OfType <FileEvent>().Subscribe(e => events.Add(e));
                    using (var eventSource = new FileWatcherEventSource(eventManager, "TestSource", directory.Path))
                    {
                        File.WriteAllText(Path.Combine(directory.Path, "test.txt"), "Test");
                        await TestHelpers.Await(() => events.Count == 2 /* We expect created and changed*/, timeout : 5000, pollingInterval : 500);
                    }

                    using (var fileStream = File.OpenWrite(Path.Combine(directory.Path, "test.txt")))
                        using (var writer = new StreamWriter(fileStream))
                        {
                            await writer.WriteLineAsync("Test 123");
                        }

                    // A small wait to ensure events would be propagated
                    await Task.Delay(2000);

                    Assert.Equal(2, events.Count);
                }
        }
Exemple #2
0
        /// <summary>
        /// Initialize file and directory change monitoring.
        /// </summary>
        private void InitializeFileWatchers()
        {
            _debugModeFileWatcher = new AutoRecoveringFileSystemWatcher(_hostLogPath, ScriptConstants.DebugSentinelFileName,
                                                                        includeSubdirectories: false, changeTypes: WatcherChangeTypes.Created | WatcherChangeTypes.Changed);
            _debugModeFileWatcher.Changed += OnDebugModeFileChanged;
            _logger.LogDebug("Debug file watch initialized.");

            _diagnosticModeFileWatcher = new AutoRecoveringFileSystemWatcher(_hostLogPath, ScriptConstants.DiagnosticSentinelFileName,
                                                                             includeSubdirectories: false, changeTypes: WatcherChangeTypes.Created | WatcherChangeTypes.Changed);
            _diagnosticModeFileWatcher.Changed += OnDiagnosticModeFileChanged;
            _logger.LogDebug("Diagnostic file watch initialized.");

            if (_scriptOptions.FileWatchingEnabled)
            {
                _fileEventSource = new FileWatcherEventSource(_eventManager, EventSources.ScriptFiles, _scriptOptions.RootScriptPath);

                _eventSubscriptions.Add(_eventManager.OfType <FileEvent>()
                                        .Where(f => string.Equals(f.Source, EventSources.ScriptFiles, StringComparison.Ordinal))
                                        .Subscribe(e => OnFileChanged(e.FileChangeArguments)));

                _logger.LogDebug("File event source initialized.");
            }

            _eventSubscriptions.Add(_eventManager.OfType <HostRestartEvent>()
                                    .Subscribe((msg) => ScheduleRestartAsync(false)
                                               .ContinueWith(t => _logger.LogCritical(t.Exception.Message),
                                                             TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously)));
        }
Exemple #3
0
        /// <summary>
        /// Initialize file and directory change monitoring.
        /// </summary>
        private void InitializeFileWatchers()
        {
            if (_scriptOptions.FileWatchingEnabled)
            {
                _fileEventSource = new FileWatcherEventSource(_eventManager, EventSources.ScriptFiles, _scriptOptions.RootScriptPath);

                _eventSubscriptions.Add(_eventManager.OfType <FileEvent>()
                                        .Where(f => string.Equals(f.Source, EventSources.ScriptFiles, StringComparison.Ordinal))
                                        .Subscribe(e => OnFileChanged(e.FileChangeArguments)));

                _logger.LogDebug("File event source initialized.");
            }

            _eventSubscriptions.Add(_eventManager.OfType <HostRestartEvent>()
                                    .Subscribe((msg) => ScheduleRestartAsync(false)
                                               .ContinueWith(t => _logger.LogCritical(t.Exception.Message),
                                                             TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously)));

            // Delay starting up for logging and debug file watchers to avoid long start up times
            Utility.ExecuteAfterColdStartDelay(_environment, InitializeSecondaryFileWatchers);
        }
Exemple #4
0
        public async Task Constructor_CreatesExpectedFileWatcher()
        {
            using (var directory = new TempDirectory())
                using (var eventManager = new ScriptEventManager())
                    using (var eventSource = new FileWatcherEventSource(eventManager, "TestSource", directory.Path))
                    {
                        var events = new List <FileEvent>();
                        eventManager.OfType <FileEvent>().Subscribe(e => events.Add(e));

                        File.WriteAllText(Path.Combine(directory.Path, "test.txt"), "Test");

                        await TestHelpers.Await(() => events.Count == 2 /* We expect created and changed*/, timeout : 5000, pollingInterval : 500);

                        Assert.True(events.All(e => string.Equals(e.Source, "TestSource", StringComparison.Ordinal)), "Event source did not match the expected source");
                        Assert.True(events.All(e => string.Equals(e.Name, "FileEvent", StringComparison.Ordinal)));
                        Assert.Equal("test.txt", events.First().FileChangeArguments.Name);
                        Assert.Equal(Path.Combine(directory.Path, "test.txt"), events[0].FileChangeArguments.FullPath);
                        Assert.Equal(WatcherChangeTypes.Created, events[0].FileChangeArguments.ChangeType);
                        Assert.Equal(WatcherChangeTypes.Changed, events[1].FileChangeArguments.ChangeType);
                    }
        }