mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2024-12-17 17:20:21 +01:00
19ef1f65db
* abstract Transport class, SocketTransport class * build: import simplexmq * feat: simple io transport unit test for read and write * more efficient buffer extension Co-authored-by: alex <alex@tekartik.com>
49 lines
1.3 KiB
Dart
49 lines
1.3 KiB
Dart
// ignore_for_file: prefer_double_quotes
|
|
|
|
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:simplexmq_io/simplexmq_io.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
const localhost = 'localhost';
|
|
void main() {
|
|
group('transport', () {
|
|
Future<ServerSocket> startServer(
|
|
void Function(Socket client) handleConnection) async {
|
|
var server = await ServerSocket.bind(InternetAddress.anyIPv4, 0);
|
|
server.listen(handleConnection);
|
|
return server;
|
|
}
|
|
|
|
test('simple write', () async {
|
|
var completer = Completer<Uint8List>();
|
|
|
|
var server = await startServer((Socket client) {
|
|
client.listen(
|
|
(Uint8List data) async {
|
|
completer.complete(data);
|
|
},
|
|
);
|
|
});
|
|
var transport = await SocketTransport.connect(localhost, server.port);
|
|
await transport.write(Uint8List.fromList([1, 2, 3]));
|
|
|
|
expect(await completer.future, [1, 2, 3]);
|
|
transport.close();
|
|
await server.close();
|
|
});
|
|
|
|
test('simple read', () async {
|
|
var server = await startServer((Socket client) {
|
|
client.add(Uint8List.fromList([1, 2, 3]));
|
|
});
|
|
var transport = await SocketTransport.connect(localhost, server.port);
|
|
expect(await transport.read(3), [1, 2, 3]);
|
|
transport.close();
|
|
await server.close();
|
|
});
|
|
});
|
|
}
|