Browse Source

nextcloud: Fix random port already being used in tests

pull/39/head
jld3103 2 years ago
parent
commit
ca86d52f92
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 13
      packages/nextcloud/test/helper.dart
  2. 22
      packages/nextcloud/test/notifications_test.dart

13
packages/nextcloud/test/helper.dart

@ -143,9 +143,11 @@ class TestHelper {
// ignore: prefer_asserts_with_message // ignore: prefer_asserts_with_message
assert(!useAppPassword || (username != null && password != null)); assert(!useAppPassword || (username != null && password != null));
final port = randomPort(); late ProcessResult result;
late int port;
final result = await runExecutableArguments( while (true) {
port = randomPort();
result = await runExecutableArguments(
'docker', 'docker',
[ [
'run', 'run',
@ -158,6 +160,11 @@ class TestHelper {
dockerImageName, 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) { if (result.exitCode != 0) {
throw Exception('Failed to run docker container: ${result.stderr}'); throw Exception('Failed to run docker container: ${result.stderr}');

22
packages/nextcloud/test/notifications_test.dart

@ -1,4 +1,5 @@
import 'dart:async'; import 'dart:async';
import 'dart:io';
import 'package:nextcloud/nextcloud.dart'; import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud_push_proxy/nextcloud_push_proxy.dart'; import 'package:nextcloud_push_proxy/nextcloud_push_proxy.dart';
@ -135,11 +136,26 @@ Future main() async {
test('Register device and receive notification', () async { test('Register device and receive notification', () async {
const pushToken = '789'; const pushToken = '789';
final port = randomPort();
final keypair = generateKeypair(); final keypair = generateKeypair();
final pushProxy = NextcloudPushProxy(); 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( final subscription = (await client.notifications.registerDeviceAtServer(
pushToken, pushToken,
keypair.publicKey, keypair.publicKey,
@ -155,10 +171,6 @@ Future main() async {
final deviceCompleter = Completer(); final deviceCompleter = Completer();
final notificationCompleter = Completer(); final notificationCompleter = Completer();
await pushProxy.create(
logging: false,
port: port,
);
pushProxy.onNewDevice.listen((final device) async { pushProxy.onNewDevice.listen((final device) async {
expect(device.pushToken, pushToken); expect(device.pushToken, pushToken);
expect(device.deviceIdentifier, isNotEmpty); expect(device.deviceIdentifier, isNotEmpty);

Loading…
Cancel
Save