1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
+ using System ;
4
5
using System . IO ;
5
6
using System . Threading ;
6
7
using System . Threading . Tasks ;
@@ -56,6 +57,53 @@ public void InitializesGetDirectorySnapshot()
56
57
}
57
58
}
58
59
60
+ [ Fact ]
61
+ public static async Task HostRestarts_OnWatchFilesChange ( )
62
+ {
63
+ using ( var directory = new TempDirectory ( ) )
64
+ {
65
+ // Setup
66
+ string tempDir = directory . Path ;
67
+ Directory . CreateDirectory ( Path . Combine ( tempDir , "Host" ) ) ;
68
+ File . Create ( Path . Combine ( tempDir , "my_watched_file.txt" ) ) ;
69
+ File . Create ( Path . Combine ( tempDir , "my_ignored_file.txt" ) ) ;
70
+
71
+ var jobHostOptions = new ScriptJobHostOptions
72
+ {
73
+ RootLogPath = tempDir ,
74
+ RootScriptPath = tempDir ,
75
+ FileWatchingEnabled = true ,
76
+ WatchFiles = { "my_watched_file.txt" }
77
+ } ;
78
+
79
+ var loggerFactory = new LoggerFactory ( ) ;
80
+ var mockApplicationLifetime = new Mock < IApplicationLifetime > ( ) ;
81
+ var mockScriptHostManager = new Mock < IScriptHostManager > ( ) ;
82
+ var mockEventManager = new ScriptEventManager ( ) ;
83
+ var environment = new TestEnvironment ( ) ;
84
+
85
+ // Act
86
+ FileMonitoringService fileMonitoringService = new FileMonitoringService ( new OptionsWrapper < ScriptJobHostOptions > ( jobHostOptions ) ,
87
+ loggerFactory , mockEventManager , mockApplicationLifetime . Object , mockScriptHostManager . Object , environment ) ;
88
+ await fileMonitoringService . StartAsync ( new CancellationToken ( canceled : false ) ) ;
89
+
90
+ var ignoredFileEventArgs = new FileSystemEventArgs ( WatcherChangeTypes . Created , tempDir , "my_ignored_file.txt" ) ;
91
+ FileEvent ignoredFileEvent = new FileEvent ( "ScriptFiles" , ignoredFileEventArgs ) ;
92
+
93
+ var watchedFileEventArgs = new FileSystemEventArgs ( WatcherChangeTypes . Created , tempDir , "my_watched_file.txt" ) ;
94
+ FileEvent watchedFileEvent = new FileEvent ( "ScriptFiles" , watchedFileEventArgs ) ;
95
+
96
+ // Test
97
+ mockEventManager . Publish ( ignoredFileEvent ) ;
98
+ await Task . Delay ( TimeSpan . FromSeconds ( 3 ) ) ;
99
+ mockScriptHostManager . Verify ( m => m . RestartHostAsync ( default ) , Times . Never ) ;
100
+
101
+ mockEventManager . Publish ( watchedFileEvent ) ;
102
+ await Task . Delay ( TimeSpan . FromSeconds ( 3 ) ) ;
103
+ mockScriptHostManager . Verify ( m => m . RestartHostAsync ( default ) ) ;
104
+ }
105
+ }
106
+
59
107
[ Theory ]
60
108
[ InlineData ( "app_offline.htm" , 150 , true , false ) ]
61
109
[ InlineData ( "app_offline.htm" , 10 , true , false ) ]
@@ -75,7 +123,8 @@ public static async Task TestAppOfflineDebounceTime(string fileName, int delayIn
75
123
{
76
124
RootLogPath = tempDir ,
77
125
RootScriptPath = tempDir ,
78
- FileWatchingEnabled = true
126
+ FileWatchingEnabled = true ,
127
+ WatchFiles = { "host.json" }
79
128
} ;
80
129
var loggerFactory = new LoggerFactory ( ) ;
81
130
var mockApplicationLifetime = new Mock < IApplicationLifetime > ( ) ;
0 commit comments