3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
- using System . Text ;
6
+ using System . IO ;
7
+ using System . Threading ;
8
+ using System . Threading . Tasks ;
9
+ using Microsoft . Azure . WebJobs . Script . Eventing ;
7
10
using Microsoft . Azure . WebJobs . Script . WebHost ;
11
+ using Microsoft . Extensions . Logging ;
12
+ using Microsoft . Extensions . Options ;
13
+ using Moq ;
14
+ using WebJobs . Script . Tests ;
8
15
using Xunit ;
9
16
10
17
namespace Microsoft . Azure . WebJobs . Script . Tests
@@ -19,5 +26,66 @@ public static void GetRelativeDirectory_ReturnsExpectedDirectoryName(string path
19
26
{
20
27
Assert . Equal ( expected , FileMonitoringService . GetRelativeDirectory ( path , @"C:\Functions\Scripts" ) ) ;
21
28
}
29
+
30
+ [ Theory ]
31
+ [ InlineData ( "app_offline.htm" , 150 , true , false ) ]
32
+ [ InlineData ( "app_offline.htm" , 10 , true , false ) ]
33
+ [ InlineData ( "host.json" , 0 , false , false ) ]
34
+ [ InlineData ( "host.json" , 200 , false , false ) ]
35
+ [ InlineData ( "host.json" , 1000 , false , true ) ]
36
+ public static async Task TestAppOfflineDebounceTime ( string fileName , int delayInMs , bool expectShutdown , bool expectRestart )
37
+ {
38
+ using ( var directory = new TempDirectory ( ) )
39
+ {
40
+ // Setup
41
+ string tempDir = directory . Path ;
42
+ Directory . CreateDirectory ( Path . Combine ( tempDir , "Host" ) ) ;
43
+ File . Create ( Path . Combine ( tempDir , fileName ) ) ;
44
+
45
+ var jobHostOptions = new ScriptJobHostOptions
46
+ {
47
+ RootLogPath = tempDir ,
48
+ RootScriptPath = tempDir ,
49
+ FileWatchingEnabled = true
50
+ } ;
51
+ var loggerFactory = new LoggerFactory ( ) ;
52
+ var mockWebHostEnvironment = new Mock < IScriptJobHostEnvironment > ( MockBehavior . Loose ) ;
53
+ var mockEventManager = new ScriptEventManager ( ) ;
54
+
55
+ // Act
56
+ FileMonitoringService fileMonitoringService = new FileMonitoringService ( new OptionsWrapper < ScriptJobHostOptions > ( jobHostOptions ) ,
57
+ loggerFactory , mockEventManager , mockWebHostEnvironment . Object ) ;
58
+ await fileMonitoringService . StartAsync ( new CancellationToken ( canceled : false ) ) ;
59
+
60
+ var offlineEventArgs = new FileSystemEventArgs ( WatcherChangeTypes . Created , tempDir , fileName ) ;
61
+ FileEvent offlinefileEvent = new FileEvent ( "ScriptFiles" , offlineEventArgs ) ;
62
+
63
+ var randomFileEventArgs = new FileSystemEventArgs ( WatcherChangeTypes . Created , tempDir , "random.txt" ) ;
64
+ FileEvent randomFileEvent = new FileEvent ( "ScriptFiles" , randomFileEventArgs ) ;
65
+
66
+ mockEventManager . Publish ( offlinefileEvent ) ;
67
+ await Task . Delay ( delayInMs ) ;
68
+ mockEventManager . Publish ( randomFileEvent ) ;
69
+
70
+ // Test
71
+ if ( expectShutdown )
72
+ {
73
+ mockWebHostEnvironment . Verify ( m => m . Shutdown ( ) ) ;
74
+ }
75
+ else
76
+ {
77
+ mockWebHostEnvironment . Verify ( m => m . Shutdown ( ) , Times . Never ) ;
78
+ }
79
+
80
+ if ( expectRestart )
81
+ {
82
+ mockWebHostEnvironment . Verify ( m => m . RestartHost ( ) ) ;
83
+ }
84
+ else
85
+ {
86
+ mockWebHostEnvironment . Verify ( m => m . RestartHost ( ) , Times . Never ) ;
87
+ }
88
+ }
89
+ }
22
90
}
23
91
}
0 commit comments