Skip to content

Commit

Permalink
test(auth): Fix flaky test (#3678)
Browse files Browse the repository at this point in the history
Fixes flaky HostedUI test by updating expectations and not relying on the timings of the event loop.
  • Loading branch information
dnys1 authored Sep 6, 2023
1 parent 55f4618 commit 14db3b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ class HostedUiPlatformImpl extends HostedUiPlatform {
/// of the ports may be blocked.
@visibleForTesting
Future<LocalServer> localConnect(Iterable<Uri> uris) async {
final localServer = _localServer;
if (localServer != null) {
if (_localServer case final localServer?) {
return localServer;
}

Expand All @@ -182,7 +181,7 @@ class HostedUiPlatformImpl extends HostedUiPlatform {
'All ports were blocked: [${uris.map((uri) => uri.port).join(',')}]',
);
}
return LocalServer(server, selectedUri);
return _localServer = LocalServer(server, selectedUri);
}

Future<void> _respond(
Expand Down Expand Up @@ -212,20 +211,16 @@ class HostedUiPlatformImpl extends HostedUiPlatform {
_noSuitableRedirect(signIn: true);
}

_localServer = await localConnect(signInUris);
final localServer = await localConnect(signInUris);
try {
final signInUrl = (await getSignInUri(
provider: provider,
redirectUri: _localServer!.uri,
redirectUri: localServer.uri,
))
.toString();
await launchUrl(signInUrl);

final server = _localServer?.server;
if (server == null) {
return;
}
await for (final request in server) {
await for (final request in localServer.server) {
final method = request.method;
if (method != 'GET') {
await _respond(
Expand Down Expand Up @@ -288,17 +283,12 @@ class HostedUiPlatformImpl extends HostedUiPlatform {
_noSuitableRedirect(signIn: false);
}

_localServer = await localConnect(signOutUris);
final localServer = await localConnect(signOutUris);
try {
final signOutUri =
getSignOutUri(redirectUri: _localServer!.uri).toString();
final signOutUri = getSignOutUri(redirectUri: localServer.uri).toString();
await launchUrl(signOutUri);

final server = _localServer?.server;
if (server == null) {
return;
}
await for (final request in server) {
await for (final request in localServer.server) {
final method = request.method;
if (method != 'GET') {
await _respond(
Expand Down Expand Up @@ -331,8 +321,9 @@ class HostedUiPlatformImpl extends HostedUiPlatform {
/// Closes the open server, if any.
@override
Future<void> close() async {
await _localServer?.server.close();
final localServer = _localServer;
_localServer = null;
await localServer?.server.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,16 @@ import 'package:test/test.dart';
class MockHostedUiPlatform extends HostedUiPlatformImpl {
MockHostedUiPlatform(super.dependencyManager);

LocalServer? _localServer;
final launchedServer = Completer<void>();

@override
Future<void> launchUrl(String url) async {}

@override
Future<LocalServer> localConnect(Iterable<Uri> uris) async {
return _localServer = await super.localConnect(uris);
}

@override
Future<void> close() async {
_localServer = null;
await super.close();
final localServer = await super.localConnect(uris);
launchedServer.complete();
return localServer;
}
}

Expand Down Expand Up @@ -153,32 +149,41 @@ void main() {
expect(hostedUiPlatform.signInRedirectUri, redirect);
expect(hostedUiPlatform.signOutRedirectUri, redirect);

unawaited(
expect(
hostedUiPlatform.signIn(
options: const CognitoSignInWithWebUIPluginOptions(),
),
completes,
);

await expectLater(client.get(redirect), completes);
expect(
hostedUiPlatform._localServer,
isNotNull,
reason: "Server won't close until a valid redirect is performed",
await hostedUiPlatform.launchedServer.future;

await expectLater(
client.get(redirect),
completion(
isA<http.Response>()
.having((res) => res.statusCode, 'statusCode', isNot(200)),
),
reason: 'Local server should be running and able to accept '
'requests. Should return 4xx for anything but a success/error '
'redirect.',
);

final successRedirect = redirect.replace(
queryParameters: {'state': 'state', 'code': 'code'},
);
await expectLater(
client.get(
redirect.replace(
queryParameters: {'state': 'state', 'code': 'code'},
),
client.get(successRedirect),
completion(
isA<http.Response>()
.having((res) => res.statusCode, 'statusCode', 200),
),
completes,
reason: "Server won't close until a valid redirect is performed",
);
await Future<void>.delayed(Duration.zero);
expect(
hostedUiPlatform._localServer,
isNull,
reason: 'A valid redirect includes state + code/error',
await expectLater(
client.get(redirect),
throwsA(isA<http.ClientException>()),
reason: 'Server should be closed after successful redirect',
);
});
});
Expand Down

0 comments on commit 14db3b4

Please sign in to comment.