From 4e00733aece25ecddaa03335f328944d064bd26a Mon Sep 17 00:00:00 2001 From: Alexander Markov Date: Fri, 21 Mar 2025 10:24:00 -0700 Subject: [PATCH] [tests] Fix flaky standalone/io/http_auth_bearer_test This test uses multiple asyncExpectThrows which are built on top of asyncStart/asyncEnd. Multiple top-level asyncStart/asyncEnd is not allowed, which caused the folllowing flaky error: Exception: Fatal: asyncStart() was called even though we are done with testing.. This is most likely a bug in your test. This change adds a top-level asyncStart/asyncEnd to enclose individual test cases. Also, awaits are added to make sure all test cases are completed before issuing the final asyncEnd. TEST=standalone/io/http_auth_bearer_test Change-Id: Ie0165c7a845928848451cfe37cf26e1292d208c3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/417221 Reviewed-by: Alexander Aprelev Commit-Queue: Alexander Markov --- .../standalone/io/http_auth_bearer_test.dart | 54 +++++++++---------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/tests/standalone/io/http_auth_bearer_test.dart b/tests/standalone/io/http_auth_bearer_test.dart index 379bb7c4458..151bdcabe3d 100644 --- a/tests/standalone/io/http_auth_bearer_test.dart +++ b/tests/standalone/io/http_auth_bearer_test.dart @@ -61,9 +61,7 @@ class Server { return this; } - void shutdown() { - server.close(); - } + Future shutdown() => server.close(); String get host => server.address.address; @@ -83,7 +81,7 @@ void testCreateInvalidBearerTokens() { Expect.throws(() => HttpClientBearerCredentials(" ")); } -void testBearerWithoutCredentials() async { +Future testBearerWithoutCredentials() async { final server = await Server().start(); final client = HttpClient(); @@ -100,11 +98,11 @@ void testBearerWithoutCredentials() async { ], ]); - server.shutdown(); + await server.shutdown(); client.close(); } -void testBearerWithCredentials() async { +Future testBearerWithCredentials() async { final server = await Server().start(); final client = HttpClient(); @@ -130,11 +128,11 @@ void testBearerWithCredentials() async { ], ]); - server.shutdown(); + await server.shutdown(); client.close(); } -void testBearerWithAuthenticateCallback() async { +Future testBearerWithAuthenticateCallback() async { final server = await Server().start(); final client = HttpClient(); @@ -165,11 +163,11 @@ void testBearerWithAuthenticateCallback() async { // assert that all authenticate callbacks have actually been called Expect.setEquals({for (int i = 0; i < 5; i++) "test$i"}, callbacks); - server.shutdown(); + await server.shutdown(); client.close(); } -void testMalformedAuthenticateHeaderWithoutCredentials() async { +Future testMalformedAuthenticateHeaderWithoutCredentials() async { final server = await Server().start(); final client = HttpClient(); final uri = Uri.parse( @@ -178,13 +176,13 @@ void testMalformedAuthenticateHeaderWithoutCredentials() async { // the request should resolve normally if no authentication is configured final request = await client.getUrl(uri); - final response = await request.close(); + await request.close(); - server.shutdown(); + await server.shutdown(); client.close(); } -void testMalformedAuthenticateHeaderWithCredentials() async { +Future testMalformedAuthenticateHeaderWithCredentials() async { final server = await Server().start(); final client = HttpClient(); final uri = Uri.parse( @@ -197,15 +195,15 @@ void testMalformedAuthenticateHeaderWithCredentials() async { await asyncExpectThrows( Future(() async { final request = await client.getUrl(uri); - final response = await request.close(); + await request.close(); }), ); - server.shutdown(); + await server.shutdown(); client.close(); } -void testMalformedAuthenticateHeaderWithAuthenticateCallback() async { +Future testMalformedAuthenticateHeaderWithAuthenticateCallback() async { final server = await Server().start(); final client = HttpClient(); final uri = Uri.parse( @@ -217,15 +215,15 @@ void testMalformedAuthenticateHeaderWithAuthenticateCallback() async { await asyncExpectThrows( Future(() async { final request = await client.getUrl(uri); - final response = await request.close(); + await request.close(); }), ); - server.shutdown(); + await server.shutdown(); client.close(); } -void testLocalServerBearer() async { +Future testLocalServerBearer() async { final client = HttpClient(); client.authenticate = (url, scheme, realm) async { @@ -248,16 +246,18 @@ void testLocalServerBearer() async { client.close(); } -main() { +Future main() async { + asyncStart(); testCreateValidBearerTokens(); testCreateInvalidBearerTokens(); - testBearerWithoutCredentials(); - testBearerWithCredentials(); - testBearerWithAuthenticateCallback(); - testMalformedAuthenticateHeaderWithoutCredentials(); - testMalformedAuthenticateHeaderWithCredentials(); - testMalformedAuthenticateHeaderWithAuthenticateCallback(); + await testBearerWithoutCredentials(); + await testBearerWithCredentials(); + await testBearerWithAuthenticateCallback(); + await testMalformedAuthenticateHeaderWithoutCredentials(); + await testMalformedAuthenticateHeaderWithCredentials(); + await testMalformedAuthenticateHeaderWithAuthenticateCallback(); // These tests are not normally run. They can be used for locally // testing with another web server (e.g. Apache). - // testLocalServerBearer(); + // await testLocalServerBearer(); + asyncEnd(); }