Browse Source

Merge pull request #39 from jld3103/fix/random-port-already-used

nextcloud: Fix random port already being used in tests
pull/40/head
jld3103 2 years ago committed by GitHub
parent
commit
5bf6f72ac7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 37
      packages/nextcloud/test/helper.dart
  2. 22
      packages/nextcloud/test/notifications_test.dart

37
packages/nextcloud/test/helper.dart

@ -143,21 +143,28 @@ 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) {
'docker', port = randomPort();
[ result = await runExecutableArguments(
'run', 'docker',
'--rm', [
'-d', 'run',
'-p', '--rm',
'$port:80', '-d',
'--add-host', '-p',
'host.docker.internal:host-gateway', '$port:80',
dockerImageName, '--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) { 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