Avoid test race condition between two completing requests (#52887)
authorMiha Zupan <mihazupan.zupan1@gmail.com>
Tue, 18 May 2021 19:19:09 +0000 (21:19 +0200)
committerGitHub <noreply@github.com>
Tue, 18 May 2021 19:19:09 +0000 (21:19 +0200)
src/libraries/System.Net.Http/tests/FunctionalTests/TelemetryTest.cs

index 6ac5e43..dc4f2ef 100644 (file)
@@ -585,6 +585,7 @@ namespace System.Net.Http.Functional.Tests
                 {
                     var firstRequestReceived = new SemaphoreSlim(0, 1);
                     var secondRequestSent = new SemaphoreSlim(0, 1);
+                    var firstRequestFinished = new SemaphoreSlim(0, 1);
 
                     await GetFactoryForVersion(version).CreateClientAndServerAsync(
                         async uri =>
@@ -605,7 +606,13 @@ namespace System.Net.Http.Functional.Tests
                             Task secondRequest = client.GetStringAsync(uri);
                             secondRequestSent.Release();
 
-                            await new[] { firstRequest, secondRequest }.WhenAllOrAnyFailed();
+                            // We are asserting that ActivityIds between Start/Stop pairs match below
+                            // We wait for the first request to finish to ensure that RequestStop events
+                            // are logged in the same order as RequestStarts
+                            await firstRequest;
+                            firstRequestFinished.Release();
+
+                            await secondRequest;
                         },
                         async server =>
                         {
@@ -634,6 +641,7 @@ namespace System.Net.Http.Functional.Tests
                                 await connection.SendResponseAsync();
 
                                 // Second request
+                                Assert.True(await firstRequestFinished.WaitAsync(TimeSpan.FromSeconds(10)));
                                 await connection.ReadRequestDataAsync(readBody: false);
                                 await connection.SendResponseAsync();
                             };