2
2
// Licensed under the MIT License. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Collections . Generic ;
5
6
using System . Linq ;
6
7
using System . Threading ;
7
8
using System . Threading . Tasks ;
@@ -33,6 +34,7 @@ public class WebJobsScriptHostServiceTests
33
34
private ILoggerFactory _loggerFactory ;
34
35
private Mock < IScriptWebHostEnvironment > _mockScriptWebHostEnvironment ;
35
36
private Mock < IEnvironment > _mockEnvironment ;
37
+ private IConfiguration _mockConfig ;
36
38
private OptionsWrapper < HostHealthMonitorOptions > _healthMonitorOptions ;
37
39
private HostPerformanceManager _hostPerformanceManager ;
38
40
private Mock < IHost > _host ;
@@ -55,6 +57,7 @@ public WebJobsScriptHostServiceTests()
55
57
_healthMonitorOptions = new OptionsWrapper < HostHealthMonitorOptions > ( new HostHealthMonitorOptions ( ) ) ;
56
58
var serviceProviderMock = new Mock < IServiceProvider > ( MockBehavior . Strict ) ;
57
59
_hostPerformanceManager = new HostPerformanceManager ( _mockEnvironment . Object , _healthMonitorOptions , serviceProviderMock . Object ) ;
60
+ _mockConfig = new Mock < IConfiguration > ( ) . Object ;
58
61
}
59
62
60
63
private Mock < IHost > CreateMockHost ( SemaphoreSlim disposedSemaphore = null )
@@ -104,7 +107,9 @@ public async Task HostInitialization_OnInitializationException_MaintainsErrorInf
104
107
105
108
_hostService = new WebJobsScriptHostService (
106
109
_monitor , hostBuilder . Object , NullLoggerFactory . Instance ,
107
- _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object , _hostPerformanceManager , _healthMonitorOptions , metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ) ;
110
+ _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object ,
111
+ _hostPerformanceManager , _healthMonitorOptions , metricsLogger ,
112
+ new Mock < IApplicationLifetime > ( ) . Object , _mockConfig ) ;
108
113
109
114
await _hostService . StartAsync ( CancellationToken . None ) ;
110
115
Assert . True ( AreRequiredMetricsGenerated ( metricsLogger ) ) ;
@@ -129,7 +134,10 @@ public async Task HostRestart_Specialization_Succeeds()
129
134
130
135
_hostService = new WebJobsScriptHostService (
131
136
_monitor , hostBuilder . Object , _loggerFactory ,
132
- _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object , _hostPerformanceManager , _healthMonitorOptions , metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ) ;
137
+ _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object ,
138
+ _hostPerformanceManager , _healthMonitorOptions ,
139
+ metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ,
140
+ _mockConfig ) ;
133
141
134
142
await _hostService . StartAsync ( CancellationToken . None ) ;
135
143
Assert . True ( AreRequiredMetricsGenerated ( metricsLogger ) ) ;
@@ -181,7 +189,10 @@ public async Task HostRestart_DuringInitializationWithError_Recovers()
181
189
182
190
_hostService = new WebJobsScriptHostService (
183
191
_monitor , hostBuilder . Object , _loggerFactory ,
184
- _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object , _hostPerformanceManager , _healthMonitorOptions , metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ) ;
192
+ _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object ,
193
+ _hostPerformanceManager , _healthMonitorOptions ,
194
+ metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ,
195
+ _mockConfig ) ;
185
196
186
197
TestLoggerProvider hostALogger = hostA . Object . GetTestLoggerProvider ( ) ;
187
198
TestLoggerProvider hostBLogger = hostB . Object . GetTestLoggerProvider ( ) ;
@@ -254,7 +265,10 @@ public async Task HostRestart_DuringInitialization_Cancels()
254
265
255
266
_hostService = new WebJobsScriptHostService (
256
267
_monitor , hostBuilder . Object , _loggerFactory ,
257
- _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object , _hostPerformanceManager , _healthMonitorOptions , metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ) ;
268
+ _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object ,
269
+ _hostPerformanceManager , _healthMonitorOptions ,
270
+ metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ,
271
+ _mockConfig ) ;
258
272
259
273
TestLoggerProvider hostALogger = hostA . Object . GetTestLoggerProvider ( ) ;
260
274
@@ -321,7 +335,9 @@ public async Task DisposedHost_ServicesNotExposed()
321
335
322
336
_hostService = new WebJobsScriptHostService (
323
337
_monitor , hostBuilder . Object , NullLoggerFactory . Instance ,
324
- _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object , _hostPerformanceManager , _healthMonitorOptions , metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ) ;
338
+ _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object ,
339
+ _hostPerformanceManager , _healthMonitorOptions , metricsLogger ,
340
+ new Mock < IApplicationLifetime > ( ) . Object , _mockConfig ) ;
325
341
326
342
Task startTask = _hostService . StartAsync ( CancellationToken . None ) ;
327
343
@@ -351,7 +367,9 @@ public async Task HostRestart_BeforeStart_WaitsForStartToContinue()
351
367
352
368
_hostService = new WebJobsScriptHostService (
353
369
_monitor , hostBuilder . Object , _loggerFactory ,
354
- _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object , _hostPerformanceManager , _healthMonitorOptions , metricsLogger , new Mock < IApplicationLifetime > ( ) . Object ) ;
370
+ _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object ,
371
+ _hostPerformanceManager , _healthMonitorOptions , metricsLogger ,
372
+ new Mock < IApplicationLifetime > ( ) . Object , _mockConfig ) ;
355
373
356
374
// Simulate a call to specialize coming from the PlaceholderSpecializationMiddleware. This
357
375
// can happen before we ever start the service, which could create invalid state.
@@ -379,6 +397,38 @@ public async Task HostRestart_BeforeStart_WaitsForStartToContinue()
379
397
_host . Verify ( p => p . StartAsync ( It . IsAny < CancellationToken > ( ) ) , Times . Once ) ;
380
398
}
381
399
400
+ [ Theory ]
401
+ [ InlineData ( "1" , false ) ]
402
+ [ InlineData ( "0" , false ) ]
403
+ [ InlineData ( "true" , true ) ]
404
+ [ InlineData ( "True" , true ) ]
405
+ [ InlineData ( "false" , false ) ]
406
+ public void ShouldEnforceSequentialRestart_WithCorrectConfig ( string value , bool expectedResult )
407
+ {
408
+ var metricsLogger = new TestMetricsLogger ( ) ;
409
+ _host . Setup ( h => h . StartAsync ( It . IsAny < CancellationToken > ( ) ) )
410
+ . Returns ( Task . CompletedTask ) ;
411
+
412
+ var hostBuilder = new Mock < IScriptHostBuilder > ( ) ;
413
+ hostBuilder . Setup ( b => b . BuildHost ( It . IsAny < bool > ( ) , It . IsAny < bool > ( ) ) )
414
+ . Returns ( _host . Object ) ;
415
+
416
+ IConfiguration config = new ConfigurationBuilder ( )
417
+ . AddInMemoryCollection ( new Dictionary < string , string >
418
+ {
419
+ { "AzureFunctionsJobHost:SequentialRestart" , value } ,
420
+ } )
421
+ . Build ( ) ;
422
+
423
+ _hostService = new WebJobsScriptHostService (
424
+ _monitor , hostBuilder . Object , NullLoggerFactory . Instance ,
425
+ _mockScriptWebHostEnvironment . Object , _mockEnvironment . Object ,
426
+ _hostPerformanceManager , _healthMonitorOptions , metricsLogger ,
427
+ new Mock < IApplicationLifetime > ( ) . Object , config ) ;
428
+
429
+ Assert . Equal ( expectedResult , _hostService . ShouldEnforceSequentialRestart ( ) ) ;
430
+ }
431
+
382
432
public void RestartHost ( )
383
433
{
384
434
_hostService . RestartHostAsync ( CancellationToken . None ) . Wait ( ) ;
0 commit comments