Skip to content

Commit 5bcd84f

Browse files
committed
Fixing flaky tests
1 parent df4030e commit 5bcd84f

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

test/WebJobs.Script.Tests.Integration/SamplesEndToEndTests.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public SamplesEndToEndTests(TestFixture fixture)
5050
[Fact]
5151
public async Task EventHubTrigger()
5252
{
53-
TestHelpers.ClearFunctionLogs("EventHubTrigger");
53+
_fixture.TraceWriter.ClearTraces();
5454

5555
// write 3 events
5656
List<EventData> events = new List<EventData>();
@@ -87,7 +87,7 @@ await TestHelpers.Await(() =>
8787
{
8888
// wait until all of the 3 of the unique IDs sent
8989
// above have been processed
90-
logs = string.Join("\r\n", TestHelpers.GetFunctionLogsAsync("EventHubTrigger", throwOnNoLogs: false).Result);
90+
logs = string.Join("\r\n", _fixture.GetFunctionLogs("EventHubTrigger"));
9191
return ids.All(p => logs.Contains(p));
9292
});
9393

@@ -350,7 +350,7 @@ public async Task HttpTrigger_DuplicateQueryParams_Succeeds()
350350
[Fact]
351351
public async Task HttpTrigger_CustomRoute_Get_ReturnsExpectedResponse()
352352
{
353-
TestHelpers.ClearFunctionLogs("HttpTrigger-CustomRoute-Get");
353+
_fixture.TraceWriter.ClearTraces();
354354

355355
var id = "4e2796ae-b865-4071-8a20-2a15cbaf856c";
356356
string functionKey = "82fprgh77jlbhcma3yr1zen8uv9yb0i7dwze3np2";
@@ -402,8 +402,8 @@ public async Task HttpTrigger_CustomRoute_Get_ReturnsExpectedResponse()
402402
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
403403

404404
// verify route parameters were part of binding data
405-
var logs = await TestHelpers.GetFunctionLogsAsync("HttpTrigger-CustomRoute-Get");
406-
var log = logs.Single(p => p.Contains($"category: electronics id: {id}"));
405+
var logs = _fixture.GetFunctionLogs("HttpTrigger-CustomRoute-Get");
406+
var log = logs.SingleOrDefault(p => p.Contains($"category: electronics id: {id}"));
407407
Assert.NotNull(log);
408408
}
409409

@@ -477,7 +477,7 @@ await TestHelpers.Await(() =>
477477
[Fact]
478478
public async Task HttpTrigger_CSharp_CustomRoute_ReturnsExpectedResponse()
479479
{
480-
TestHelpers.ClearFunctionLogs("HttpTrigger-CSharp-CustomRoute");
480+
_fixture.TraceWriter.ClearTraces();
481481

482482
string functionKey = "68qkqlughacc6f9n6t4ubk0jq7r5er7pta13yh20";
483483
string uri = $"api/csharp/products/electronics/123?code={functionKey}";
@@ -523,7 +523,7 @@ public async Task HttpTrigger_CSharp_CustomRoute_ReturnsExpectedResponse()
523523
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
524524

525525
// verify route parameters were part of binding data
526-
var logs = await TestHelpers.GetFunctionLogsAsync("HttpTrigger-CSharp-CustomRoute");
526+
var logs = _fixture.GetFunctionLogs("HttpTrigger-CSharp-CustomRoute");
527527
Assert.True(logs.Any(p => p.Contains("Parameters: category=electronics id=123")));
528528
Assert.True(logs.Any(p => p.Contains("ProductInfo: Category=electronics Id=123")));
529529
}
@@ -786,7 +786,7 @@ await TestHelpers.Await(() =>
786786
[Fact]
787787
public async Task QueueTriggerPython_Succeeds()
788788
{
789-
TestHelpers.ClearFunctionLogs("QueueTrigger-Python");
789+
_fixture.TraceWriter.ClearTraces();
790790

791791
// write the input message
792792
CloudQueue inputQueue = _fixture.QueueClient.GetQueueReference("samples-python");
@@ -809,12 +809,11 @@ public async Task QueueTriggerPython_Succeeds()
809809
Assert.Equal(id, (string)jsonObject["id"]);
810810

811811
// verify the function output
812-
var logs = await TestHelpers.GetFunctionLogsAsync("QueueTrigger-Python");
812+
var logs = _fixture.GetFunctionLogs("QueueTrigger-Python").ToList();
813813

814-
// strip off the timestamps and [Info] from the beginning of each line
815-
logs = logs.Select(l => l.Split(new[] { ' ' }, 3)[2]).ToList();
816814
int idx = logs.IndexOf("Read 5 Table entities");
817-
for (int i = idx + 1; i < 5; i++)
815+
logs = logs.Skip(idx + 1).Take(5).ToList();
816+
for (int i = 0; i < 5; i++)
818817
{
819818
string json = logs[i];
820819
JObject entity = null;
@@ -1220,6 +1219,15 @@ public TestFixture()
12201219

12211220
public HttpServer HttpServer { get; set; }
12221221

1222+
public TestTraceWriter TraceWriter => _traceWriter;
1223+
1224+
public IEnumerable<string> GetFunctionLogs(string functionName)
1225+
{
1226+
return _traceWriter.GetTraces()
1227+
.Where(p => p.Properties.ContainsKey(ScriptConstants.LoggerFunctionNameKey) && string.Compare((string)p.Properties[ScriptConstants.LoggerFunctionNameKey], functionName) == 0)
1228+
.Select(p => p.Message);
1229+
}
1230+
12231231
public void Dispose()
12241232
{
12251233
HttpServer?.Dispose();

0 commit comments

Comments
 (0)