From ca86d52f923de5474b55c2c939963a8002bd16ef Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 25 Jul 2022 12:59:30 +0200 Subject: [PATCH] nextcloud: Fix random port already being used in tests --- packages/nextcloud/test/helper.dart | 37 +++++++++++-------- .../nextcloud/test/notifications_test.dart | 22 ++++++++--- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/packages/nextcloud/test/helper.dart b/packages/nextcloud/test/helper.dart index 72a27f93..e5cdeb1c 100644 --- a/packages/nextcloud/test/helper.dart +++ b/packages/nextcloud/test/helper.dart @@ -143,21 +143,28 @@ class TestHelper { // ignore: prefer_asserts_with_message assert(!useAppPassword || (username != null && password != null)); - final port = randomPort(); - - final result = await runExecutableArguments( - 'docker', - [ - 'run', - '--rm', - '-d', - '-p', - '$port:80', - '--add-host', - 'host.docker.internal:host-gateway', - dockerImageName, - ], - ); + late ProcessResult result; + late int port; + while (true) { + port = randomPort(); + result = await runExecutableArguments( + 'docker', + [ + 'run', + '--rm', + '-d', + '-p', + '$port:80', + '--add-host', + 'host.docker.internal:host-gateway', + dockerImageName, + ], + ); + // 125 means the docker run command itself has failed which indicated the port is already used + if (result.exitCode != 125) { + break; + } + } if (result.exitCode != 0) { throw Exception('Failed to run docker container: ${result.stderr}'); diff --git a/packages/nextcloud/test/notifications_test.dart b/packages/nextcloud/test/notifications_test.dart index 5021ce18..c4c8818b 100644 --- a/packages/nextcloud/test/notifications_test.dart +++ b/packages/nextcloud/test/notifications_test.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:io'; import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud_push_proxy/nextcloud_push_proxy.dart'; @@ -135,11 +136,26 @@ Future main() async { test('Register device and receive notification', () async { const pushToken = '789'; - final port = randomPort(); final keypair = generateKeypair(); final pushProxy = NextcloudPushProxy(); + late int port; + while (true) { + port = randomPort(); + try { + await pushProxy.create( + logging: false, + port: port, + ); + break; + } on SocketException catch (e) { + if (e.osError?.errorCode != 98) { + rethrow; + } + } + } + final subscription = (await client.notifications.registerDeviceAtServer( pushToken, keypair.publicKey, @@ -155,10 +171,6 @@ Future main() async { final deviceCompleter = Completer(); final notificationCompleter = Completer(); - await pushProxy.create( - logging: false, - port: port, - ); pushProxy.onNewDevice.listen((final device) async { expect(device.pushToken, pushToken); expect(device.deviceIdentifier, isNotEmpty);