Skip to content

Commit 379b3e8

Browse files
committed
attempting a test fix
1 parent 687d03c commit 379b3e8

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/DrainModeResumeEndToEndTests.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
2-
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
3-
using Microsoft.WebJobs.Script.Tests;
4-
using Newtonsoft.Json;
5-
using Newtonsoft.Json.Linq;
61
using System;
72
using System.IO;
83
using System.Net;
9-
using System.Threading;
104
using System.Threading.Tasks;
5+
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
6+
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
7+
using Microsoft.WebJobs.Script.Tests;
8+
using Newtonsoft.Json;
119
using Xunit;
1210

1311
namespace Microsoft.Azure.WebJobs.Script.Tests.EndToEnd
@@ -27,7 +25,7 @@ public async Task DrainModeEnabled_RunningHost_StartsNewHost_ReturnsOk()
2725
Assert.Equal(drainStatus.State, DrainModeState.Disabled);
2826

2927
// Capture pre-drain instance ID
30-
var originalInstanceId = this.HostInstanceId;
28+
var originalInstanceId = await GetActiveHostInstanceIdAsync();
3129

3230
// Validate ability to call HttpTrigger without issues
3331
response = await SamplesTestHelpers.InvokeHttpTrigger(this, "HttpTrigger");
@@ -62,7 +60,7 @@ public async Task DrainModeEnabled_RunningHost_StartsNewHost_ReturnsOk()
6260
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
6361

6462
// Validate the instance ID has changed
65-
Assert.NotEqual(originalInstanceId, this.HostInstanceId);
63+
Assert.NotEqual(originalInstanceId, await GetActiveHostInstanceIdAsync());
6664
}
6765

6866
[Fact]

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/EndToEndTestFixture.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ protected EndToEndTestFixture(
8383

8484
public TestEventGenerator EventGenerator { get; private set; } = new TestEventGenerator();
8585

86-
public string HostInstanceId => Host.JobHostServices.GetService<IOptions<ScriptJobHostOptions>>().Value.InstanceId;
86+
public async Task<string> GetActiveHostInstanceIdAsync()
87+
{
88+
// During restarts the ActiveHost can be null. Wait to see if it recovers.
89+
await TestHelpers.Await(() => Host.JobHostServices is not null,
90+
userMessageCallback: () => $"Timed out waiting for JobHostServices to be available. Logs:{Environment.NewLine}{Host.GetLog()}.");
91+
92+
return Host.JobHostServices.GetService<IOptions<ScriptJobHostOptions>>().Value.InstanceId;
93+
}
8794

8895
public string MasterKey { get; private set; }
8996

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/FunctionsControllerEndToEndTests.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
2-
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
3-
using Microsoft.WebJobs.Script.Tests;
41
using System;
52
using System.IO;
63
using System.Net;
74
using System.Threading.Tasks;
5+
using Microsoft.Azure.WebJobs.Script.WebHost.Models;
6+
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
7+
using Microsoft.WebJobs.Script.Tests;
88
using Xunit;
99

1010
namespace Microsoft.Azure.WebJobs.Script.Tests.EndToEnd
@@ -17,7 +17,7 @@ public class FunctionsControllerEndToEndTests : FunctionsControllerTestFixture
1717
public async Task FunctionsController_GetAllFunctions_ReturnsOk()
1818
{
1919
// Capture original instance ID
20-
var originalInstanceId = this.HostInstanceId;
20+
var originalInstanceId = await GetActiveHostInstanceIdAsync();
2121

2222
// Validate ability to call HttpTrigger without issues
2323
var response = await SamplesTestHelpers.InvokeHttpTrigger(this, "HttpTrigger");
@@ -32,14 +32,14 @@ public async Task FunctionsController_GetAllFunctions_ReturnsOk()
3232
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
3333

3434
// Validate the instance ID is still the same
35-
Assert.Equal(originalInstanceId, this.HostInstanceId);
35+
Assert.Equal(originalInstanceId, await GetActiveHostInstanceIdAsync());
3636
}
3737

3838
[Fact]
3939
public async Task FunctionsController_GetSpecificFunction_ReturnsOk()
4040
{
4141
// Capture original instance ID
42-
var originalInstanceId = this.HostInstanceId;
42+
var originalInstanceId = await GetActiveHostInstanceIdAsync();
4343

4444
// Validate ability to call HttpTrigger without issues
4545
var response = await SamplesTestHelpers.InvokeHttpTrigger(this, "HttpTrigger");
@@ -55,14 +55,14 @@ public async Task FunctionsController_GetSpecificFunction_ReturnsOk()
5555
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
5656

5757
// Validate the instance ID is still the same
58-
Assert.Equal(originalInstanceId, this.HostInstanceId);
58+
Assert.Equal(originalInstanceId, await GetActiveHostInstanceIdAsync());
5959
}
6060

6161
[Fact]
6262
public async Task FunctionsController_GetSpecificFunctionStatus_ReturnsOk()
6363
{
6464
// Capture original instance ID
65-
var originalInstanceId = this.HostInstanceId;
65+
var originalInstanceId = await GetActiveHostInstanceIdAsync();
6666

6767
// Validate ability to call HttpTrigger without issues
6868
var response = await SamplesTestHelpers.InvokeHttpTrigger(this, "HttpTrigger");
@@ -77,14 +77,14 @@ public async Task FunctionsController_GetSpecificFunctionStatus_ReturnsOk()
7777
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
7878

7979
// Validate the instance ID is still the same
80-
Assert.Equal(originalInstanceId, this.HostInstanceId);
80+
Assert.Equal(originalInstanceId, await GetActiveHostInstanceIdAsync());
8181
}
8282

8383
[Fact]
8484
public async Task FunctionsController_CreateUpdate_NoFileChange_ReturnsCreated_NoRestart()
8585
{
8686
// Capture original instance ID
87-
var originalInstanceId = this.HostInstanceId;
87+
var originalInstanceId = await GetActiveHostInstanceIdAsync();
8888

8989
// Validate ability to call HttpTrigger without issues
9090
var response = await SamplesTestHelpers.InvokeHttpTrigger(this, "HttpTrigger");
@@ -99,14 +99,14 @@ public async Task FunctionsController_CreateUpdate_NoFileChange_ReturnsCreated_N
9999
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
100100

101101
// Validate the instance ID is still the same
102-
Assert.Equal(originalInstanceId, this.HostInstanceId);
102+
Assert.Equal(originalInstanceId, await GetActiveHostInstanceIdAsync());
103103
}
104104

105105
[Fact]
106106
public async Task FunctionsController_CreateUpdate_FileChange_ReturnsCreated_RestartsJobHost()
107107
{
108108
// Capture pre-restart instance ID
109-
var originalInstanceId = this.HostInstanceId;
109+
var originalInstanceId = await GetActiveHostInstanceIdAsync();
110110

111111
// Validate ability to call HttpTrigger without issues
112112
var response = await SamplesTestHelpers.InvokeHttpTrigger(this, "HttpTrigger");
@@ -125,7 +125,7 @@ public async Task FunctionsController_CreateUpdate_FileChange_ReturnsCreated_Res
125125
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
126126

127127
// Validate the instance ID has changed
128-
Assert.NotEqual(originalInstanceId, this.HostInstanceId);
128+
Assert.NotEqual(originalInstanceId, await GetActiveHostInstanceIdAsync());
129129

130130
// Reset config
131131
response = await SamplesTestHelpers.InvokeEndpointPut(this, "admin/functions/HttpTrigger", TestData());

test/WebJobs.Script.Tests.Integration/WebHostEndToEnd/SamplesEndToEndTests_Node_MultipleProcesses.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ public async Task NodeProcess_Different_AfterHostRestart()
4848
await Task.Delay(TimeSpan.FromMinutes(1));
4949

5050
IEnumerable<int> nodeProcessesAfter = Process.GetProcessesByName("node").Select(p => p.Id);
51-
51+
5252
// Verify node process is different after host restart
5353
var result = nodeProcessesAfter.Where(pId1 => !nodeProcessesBeforeHostRestart.Any(pId2 => pId2 == pId1) && !_fixture.NodeProcessesBeforeTestStarted.Any(pId3 => pId3 == pId1));
5454
Assert.Equal(3, result.Count());
5555
}
56-
56+
5757
private async Task<IEnumerable<int>> WaitAndGetAllNodeProcesses(int expectedProcessCount)
5858
{
5959
IEnumerable<int> nodeProcessesBeforeHostRestart = Process.GetProcessesByName("node").Select(p => p.Id);
60-
while(nodeProcessesBeforeHostRestart.Count() < expectedProcessCount)
60+
while (nodeProcessesBeforeHostRestart.Count() < expectedProcessCount)
6161
{
6262
await Task.Delay(TimeSpan.FromSeconds(5));
6363
nodeProcessesBeforeHostRestart = Process.GetProcessesByName("node").Select(p => p.Id);
@@ -78,20 +78,20 @@ public async Task NodeProcessCount_RemainsSame_AfterMultipleTimeouts()
7878
{
7979
throw new Exception("Failed to start all 3 node processes");
8080
}
81-
var oldHostInstanceId = _fixture.HostInstanceId;
81+
var oldHostInstanceId = await _fixture.GetActiveHostInstanceIdAsync();
8282
IEnumerable<int> nodeProcessesBeforeHostRestart = await getNodeTask;
8383
string functionKey = await _fixture.Host.GetFunctionSecretAsync("httptrigger-timeout");
8484
string uri = $"api/httptrigger-timeout?code={functionKey}&name=Yogi";
8585

8686
// Send multiple requests that would timeout
87-
for (int i=0; i<5; ++i)
87+
for (int i = 0; i < 5; ++i)
8888
{
8989
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
9090
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain"));
9191
timeoutTasks.Add(_fixture.Host.HttpClient.SendAsync(request));
9292
}
9393
var results = await Task.WhenAll(timeoutTasks);
94-
foreach(var timeoutResult in results)
94+
foreach (var timeoutResult in results)
9595
{
9696
Assert.Equal(HttpStatusCode.InternalServerError, timeoutResult.StatusCode); // Confirm response code after timeout (10 seconds)
9797
}
@@ -101,12 +101,12 @@ public async Task NodeProcessCount_RemainsSame_AfterMultipleTimeouts()
101101
// Confirm count remains the same
102102
Assert.Equal(nodeProcessesBeforeHostRestart.Count(), nodeProcessesAfter.Count());
103103

104-
104+
105105
// Confirm all processes are different
106106
Assert.Equal(3, nodeProcessesAfter.Except(nodeProcessesBeforeHostRestart).Count());
107107

108108
// Confirm host instance ids are the same
109-
Assert.Equal(oldHostInstanceId, _fixture.HostInstanceId);
109+
Assert.Equal(oldHostInstanceId, await _fixture.GetActiveHostInstanceIdAsync());
110110
}
111111

112112
public class MultiplepleProcessesTestFixture : EndToEndTestFixture

0 commit comments

Comments
 (0)