From 810578ea540146185ef491e8b2dc5722ccf6499c Mon Sep 17 00:00:00 2001 From: gkc Date: Thu, 12 Dec 2024 09:58:14 +0000 Subject: [PATCH] test: removed unused `at_end2end_test/test/commons.dart` chore: removed a TODO --- tests/at_end2end_test/test/commons.dart | 119 ------------------ .../at_end2end_test/test/e2e_test_utils.dart | 19 ++- .../test/update_verb_test.dart | 3 +- 3 files changed, 15 insertions(+), 126 deletions(-) delete mode 100644 tests/at_end2end_test/test/commons.dart diff --git a/tests/at_end2end_test/test/commons.dart b/tests/at_end2end_test/test/commons.dart deleted file mode 100644 index 9905d9a23..000000000 --- a/tests/at_end2end_test/test/commons.dart +++ /dev/null @@ -1,119 +0,0 @@ -import 'dart:collection'; -import 'dart:convert'; -import 'dart:io'; - -import 'package:test/test.dart'; - -import 'pkam_utils.dart'; - -var _queue = Queue(); -var maxRetryCount = 10; -var retryCount = 1; - -///Socket Connection -Future socket_connection(host, port) async { - var context = SecurityContext(); - print(Directory.current); - context.setTrustedCertificates('lib/secondary/base/certs/cacert.pem'); - context.usePrivateKey('lib/secondary/base/certs/privkey.pem'); - context.useCertificateChain('lib/secondary/base/certs/fullchain.pem'); - return await SecureSocket.connect(host, port, context: context); -} - -void clear() { - _queue.clear(); - print('queue cleared'); -} - -///Secure Socket Connection -Future secure_socket_connection(var host, var port) async { - var socket; - while (retryCount < maxRetryCount) { - try { - socket = await SecureSocket.connect(host, port); - if (socket != null) { - break; - } - } on Exception { - print('retrying "$host:$port" for connection.. $retryCount'); - await Future.delayed(Duration(seconds: 5)); - retryCount++; - } - } - return socket; -} - -/// Socket Listener -void socket_listener(Socket socket) { - socket.listen(_messageHandler); -} - -/// Socket write -Future socket_writer(Socket socket, String msg) async { - print('command sent: $msg'); - msg = msg + '\n'; - socket.write(msg); -} - -///The prepare function takes a socket and atsign as input params and runs a from verb and pkam verb on the atsign param. -Future prepare(Socket socket, String atsign) async { - // FROM VERB - await socket_writer(socket, 'from:$atsign'); - var response = await read(); - print('From verb response $response'); - response = response.replaceAll('data:', ''); - var pkam_digest = generatePKAMDigest(atsign, response); - // var cram = getDigest(atsign, response); - - // PKAM VERB - await socket_writer(socket, 'pkam:$pkam_digest'); - response = await read(); - print('pkam verb response $response'); - expect(response, 'data:success\n'); - - //CRAM VERB - // await socket_writer(socket, 'cram:$cram'); - // response = await read(); - // print('cram verb response $response'); - // expect(response, 'data:success\n'); -} - -void _messageHandler(data) { - if (data.length == 1 && data.first == 64) { - return; - } - //ignore prompt(@ or @@) after '\n'. byte code for \n is 10 - if (data.last == 64 && data.contains(10)) { - data = data.sublist(0, data.lastIndexOf(10) + 1); - _queue.add(utf8.decode(data)); - } else if (data.length > 1 && data.first == 64 && data.last == 64) { - // pol responses do not end with '\n'. Add \n for buffer completion - _queue.add(utf8.decode(data)); - } else { - _queue.add(utf8.decode(data)); - } -} - -Future read({int maxWaitMilliSeconds = 5000}) async { - var result; - //wait maxWaitMilliSeconds seconds for response from remote socket - var loopCount = (maxWaitMilliSeconds / 50).round(); - for (var i = 0; i < loopCount; i++) { - await Future.delayed(Duration(milliseconds: 1000)); - var queueLength = _queue.length; - if (queueLength > 0) { - result = _queue.removeFirst(); - // result from another secondary is either data or a @@ denoting complete - // of the handshake - if (result.startsWith('data:') || - (result.startsWith('error:')) || - (result.startsWith('@') && result.endsWith('@'))) { - return result; - } else { - //log any other response and ignore - result = ''; - } - } - } - return result; -} diff --git a/tests/at_end2end_test/test/e2e_test_utils.dart b/tests/at_end2end_test/test/e2e_test_utils.dart index 9862a13c6..e5bce6041 100644 --- a/tests/at_end2end_test/test/e2e_test_utils.dart +++ b/tests/at_end2end_test/test/e2e_test_utils.dart @@ -28,10 +28,17 @@ Future getSocketHandler(atSign) async { if (asc == null) { throw _NoSuchAtSignException('$atSign not configured'); } - // TODO switch on _AtSignConfig.connectionType and create a - // TODO SimpleOutboundSocketConnection or SimpleOutboundWebsocketConnection - // TODO as required - var handler = SimpleOutboundSocketHandler._(asc.host, asc.port, atSign); + + // ignore: prefer_typing_uninitialized_variables + var handler; + switch(asc.connectionType!) { + case _ConnectionTypeEnum.socket: + handler = SimpleOutboundSocketHandler._(asc.host, asc.port, atSign); + break; + case _ConnectionTypeEnum.websocket: + // TODO e.g. handler = SimpleOutboundWebsocketConnection._(asc.host, asc.port, atSign); + throw UnimplementedError('e2e_test_utils cannot yet create a websocket connection'); + } await handler.connect(); handler.startListening(); @@ -212,11 +219,11 @@ class _AtSignConfig { String atSign; String host; int port; - _ConnectionTypeEnum connectionType; + _ConnectionTypeEnum? connectionType; /// Creates and adds to [atSignConfigMap] or throws [_AtSignAlreadyAddedException] if we've already got it. _AtSignConfig(this.atSign, this.host, this.port, this.connectionType) { - this.connectionType ??= _ConnectionTypeEnum.socket; + connectionType ??= _ConnectionTypeEnum.socket; if (atSignConfigMap.containsKey(atSign)) { throw _AtSignAlreadyAddedException("AtSignConfig for $atSign has already been created"); } diff --git a/tests/at_end2end_test/test/update_verb_test.dart b/tests/at_end2end_test/test/update_verb_test.dart index 45ad8898d..a4ae2b448 100644 --- a/tests/at_end2end_test/test/update_verb_test.dart +++ b/tests/at_end2end_test/test/update_verb_test.dart @@ -3,7 +3,6 @@ import 'dart:math'; import 'package:test/test.dart'; -import 'commons.dart'; import 'e2e_test_utils.dart' as e2e; void main() { @@ -77,6 +76,8 @@ void main() { expect(response, contains('data:$value')); //LOOKUP VERB in the other secondary + var maxRetryCount = 10; + var retryCount = 1; while (true) { await sh2.writeCommand('llookup:cached:$atSign_2:youtube_id$atSign_1'); response = await sh2.read();