|
32 | 32 | using Microsoft.Extensions.Logging;
|
33 | 33 | using Microsoft.Extensions.Options;
|
34 | 34 | using Microsoft.WebJobs.Script.Tests;
|
| 35 | +using TestFunctions; |
35 | 36 | using Xunit;
|
36 | 37 | using Xunit.Abstractions;
|
37 | 38 | using IApplicationLifetime = Microsoft.AspNetCore.Hosting.IApplicationLifetime;
|
@@ -249,6 +250,164 @@ public async Task Specialization_ResetsSharedLoadContext()
|
249 | 250 | }
|
250 | 251 | }
|
251 | 252 |
|
| 253 | + [Fact] |
| 254 | + public async Task ForNonReadOnlyFileSystem_RestartWorkerForSpecializationAndHotReload() |
| 255 | + { |
| 256 | + _environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, "node"); |
| 257 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, ScriptConstants.FeatureFlagEnableWorkerIndexing); |
| 258 | + |
| 259 | + var builder = CreateStandbyHostBuilder("HttpTriggerNoAuth"); |
| 260 | + |
| 261 | + builder.ConfigureAppConfiguration(config => |
| 262 | + { |
| 263 | + string scriptRootConfigPath = ConfigurationPath.Combine(ConfigurationSectionNames.WebHost, nameof(ScriptApplicationHostOptions.ScriptPath)); |
| 264 | + config.AddInMemoryCollection(new Dictionary<string, string> |
| 265 | + { |
| 266 | + { _scriptRootConfigPath, Path.GetFullPath(@"TestScripts\NodeWithBundles") } |
| 267 | + }); |
| 268 | + }); |
| 269 | + |
| 270 | + using var testServer = new TestServer(builder); |
| 271 | + |
| 272 | + var client = testServer.CreateClient(); |
| 273 | + |
| 274 | + var response = await client.GetAsync("api/warmup"); |
| 275 | + response.EnsureSuccessStatusCode(); |
| 276 | + |
| 277 | + var webChannelManager = testServer.Services.GetService<IWebHostRpcWorkerChannelManager>(); |
| 278 | + var channel = await webChannelManager.GetChannels("node").Single().Value.Task; |
| 279 | + var processId = channel.WorkerProcess.Process.Id; |
| 280 | + |
| 281 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteContainerReady, "1"); |
| 282 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "0"); |
| 283 | + |
| 284 | + response = await client.GetAsync("api/HttpTriggerNoAuth"); |
| 285 | + response.EnsureSuccessStatusCode(); |
| 286 | + var responseContent = await response.Content.ReadAsStringAsync(); |
| 287 | + |
| 288 | + string content = "Node.js HttpTrigger function invoked."; |
| 289 | + responseContent.Contains(content); |
| 290 | + |
| 291 | + channel = await webChannelManager.GetChannels("node").Single().Value.Task; |
| 292 | + var newProcessId = channel.WorkerProcess.Process.Id; |
| 293 | + Assert.NotEqual(processId, newProcessId); |
| 294 | + Assert.Contains(content, responseContent); |
| 295 | + |
| 296 | + var indexJS = Path.GetFullPath(@"TestScripts\NodeWithBundles\HttpTriggerNoAuth\index.js"); |
| 297 | + |
| 298 | + string fileContent = File.ReadAllText(indexJS); |
| 299 | + string newContent = "Updated Node.js HttpTrigger function invoked."; |
| 300 | + string updatedContent = fileContent.Replace(content, newContent); |
| 301 | + File.WriteAllText(indexJS, updatedContent); |
| 302 | + |
| 303 | + var manager = testServer.Host.Services.GetService<IScriptHostManager>(); |
| 304 | + var hostService = manager as WebJobsScriptHostService; |
| 305 | + |
| 306 | + await TestHelpers.Await(() => |
| 307 | + { |
| 308 | + return hostService.State == ScriptHostState.Default; |
| 309 | + }, 5000); |
| 310 | + |
| 311 | + await TestHelpers.Await(() => |
| 312 | + { |
| 313 | + return hostService.State == ScriptHostState.Running; |
| 314 | + }, 5000); |
| 315 | + |
| 316 | + response = await client.GetAsync("api/HttpTriggerNoAuth"); |
| 317 | + response.EnsureSuccessStatusCode(); |
| 318 | + responseContent = await response.Content.ReadAsStringAsync(); |
| 319 | + responseContent.Contains(newContent); |
| 320 | + |
| 321 | + channel = await webChannelManager.GetChannels("node").Single().Value.Task; |
| 322 | + var hotReloadProcessId = channel.WorkerProcess.Process.Id; |
| 323 | + Assert.NotEqual(hotReloadProcessId, newProcessId); |
| 324 | + Assert.Contains(newContent, responseContent); |
| 325 | + } |
| 326 | + |
| 327 | + [Fact] |
| 328 | + public async Task Specialization_RestartsWorkerForNonReadOnlyFileSystem() |
| 329 | + { |
| 330 | + _environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, "node"); |
| 331 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, ScriptConstants.FeatureFlagEnableWorkerIndexing); |
| 332 | + |
| 333 | + var builder = CreateStandbyHostBuilder("HttpTriggerNoAuth"); |
| 334 | + |
| 335 | + builder.ConfigureAppConfiguration(config => |
| 336 | + { |
| 337 | + string scriptRootConfigPath = ConfigurationPath.Combine(ConfigurationSectionNames.WebHost, nameof(ScriptApplicationHostOptions.ScriptPath)); |
| 338 | + config.AddInMemoryCollection(new Dictionary<string, string> |
| 339 | + { |
| 340 | + { _scriptRootConfigPath, Path.GetFullPath(@"TestScripts\NodeWithBundles") } |
| 341 | + }); |
| 342 | + }); |
| 343 | + |
| 344 | + using var testServer = new TestServer(builder); |
| 345 | + |
| 346 | + var client = testServer.CreateClient(); |
| 347 | + |
| 348 | + var response = await client.GetAsync("api/warmup"); |
| 349 | + response.EnsureSuccessStatusCode(); |
| 350 | + |
| 351 | + var placeholderContext = FunctionAssemblyLoadContext.Shared; |
| 352 | + |
| 353 | + var webChannelManager = testServer.Services.GetService<IWebHostRpcWorkerChannelManager>(); |
| 354 | + var channel = await webChannelManager.GetChannels("node").Single().Value.Task; |
| 355 | + var processId = channel.WorkerProcess.Process.Id; |
| 356 | + |
| 357 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteContainerReady, "1"); |
| 358 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "0"); |
| 359 | + |
| 360 | + //await _pauseBeforeHostBuild.WaitAsync(10000); |
| 361 | + response = await client.GetAsync("api/HttpTriggerNoAuth"); |
| 362 | + response.EnsureSuccessStatusCode(); |
| 363 | + |
| 364 | + channel = await webChannelManager.GetChannels("node").Single().Value.Task; |
| 365 | + var newProcessId = channel.WorkerProcess.Process.Id; |
| 366 | + Assert.NotEqual(processId, newProcessId); |
| 367 | + } |
| 368 | + |
| 369 | + |
| 370 | + [Fact] |
| 371 | + public async Task Specialization_UsePlaceholderWorkerforReadOnlyFileSystem() |
| 372 | + { |
| 373 | + _environment.SetEnvironmentVariable(RpcWorkerConstants.FunctionWorkerRuntimeSettingName, "node"); |
| 374 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebJobsFeatureFlags, ScriptConstants.FeatureFlagEnableWorkerIndexing); |
| 375 | + |
| 376 | + var builder = CreateStandbyHostBuilder("HttpTriggerNoAuth"); |
| 377 | + string isFileSystemReadOnly = ConfigurationPath.Combine(ConfigurationSectionNames.WebHost, nameof(ScriptApplicationHostOptions.IsFileSystemReadOnly)); |
| 378 | + |
| 379 | + builder.ConfigureAppConfiguration(config => |
| 380 | + { |
| 381 | + config.AddInMemoryCollection(new Dictionary<string, string> |
| 382 | + { |
| 383 | + { _scriptRootConfigPath, Path.GetFullPath(@"TestScripts\NodeWithBundles") } |
| 384 | + }); |
| 385 | + }); |
| 386 | + |
| 387 | + |
| 388 | + using var testServer = new TestServer(builder); |
| 389 | + |
| 390 | + var client = testServer.CreateClient(); |
| 391 | + |
| 392 | + var response = await client.GetAsync("api/warmup"); |
| 393 | + response.EnsureSuccessStatusCode(); |
| 394 | + |
| 395 | + var webChannelManager = testServer.Services.GetService<IWebHostRpcWorkerChannelManager>(); |
| 396 | + var channel = await webChannelManager.GetChannels("node").Single().Value.Task; |
| 397 | + var processId = channel.WorkerProcess.Process.Id; |
| 398 | + |
| 399 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteRunFromPackage, "1"); |
| 400 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteContainerReady, "1"); |
| 401 | + _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsitePlaceholderMode, "0"); |
| 402 | + |
| 403 | + response = await client.GetAsync("api/HttpTriggerNoAuth"); |
| 404 | + response.EnsureSuccessStatusCode(); |
| 405 | + |
| 406 | + channel = await webChannelManager.GetChannels("node").Single().Value.Task; |
| 407 | + var newProcessId = channel.WorkerProcess.Process.Id; |
| 408 | + Assert.Equal(processId, newProcessId); |
| 409 | + } |
| 410 | + |
252 | 411 | [Fact]
|
253 | 412 | public async Task Specialization_GCMode()
|
254 | 413 | {
|
|
0 commit comments