Skip to content

Commit

Permalink
v1.0.0 preparations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mad Sheogorath committed Oct 27, 2021
1 parent 4dfeaa9 commit 65a17b2
Show file tree
Hide file tree
Showing 9 changed files with 271 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,4 +495,4 @@ Future<void> main() async {
It's available for Dart only. Currently there are no plans to implement it for any other language. However if developers will find this package useful then it may be implemented for Node.JS and C++ in the future.

## P.S.
I really hope you enjoy it ;)
I hope you enjoy it ;)
4 changes: 3 additions & 1 deletion lib/core/console.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
part of serveme;

final Stream<List<int>> stdinStream = stdin.asBroadcastStream();

class CommandHandler {
CommandHandler({required this.function, this.validator, this.usage});

Expand All @@ -12,7 +14,7 @@ class Console {
Console(this._server) {
stdin.echoMode = false;
stdin.lineMode = false;
_listener = stdin.listen(_key);
_listener = stdinStream.listen(_key);
}

final ServeMe<ServeMeClient> _server;
Expand Down
6 changes: 5 additions & 1 deletion lib/serveme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ class ServeMe<C extends ServeMeClient> {
return _running;
}

Future<void> stop() async {
await _shutdown(ProcessSignal.sigquit, 100500);
}

Future<void> _shutdown(ProcessSignal event, [int code = 100500]) async {
await log('Server shutdown initiated: $event');
await _events.dispatch(StopEvent(event, code));
Expand All @@ -207,6 +211,6 @@ class ServeMe<C extends ServeMeClient> {
await log('Server stopped');
await console.dispose();
await _logger.dispose();
exit(code);
if (code != 100500) exit(code);
}
}
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ dependencies:
yaml: ^3.1.0
connectme: ^1.1.1
packme: ^1.1.5
dev_dependencies:
test: ^1.17.11
8 changes: 8 additions & 0 deletions test/config_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
port: 31337

debug: true
debug_log: test/debug_test.log
error_log: test/error_test.log

modules:
- test
80 changes: 80 additions & 0 deletions test/generated/test.generated.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import 'package:packme/packme.dart';

class TestResponse extends PackMeMessage {
TestResponse({
required this.responseParam,
});
TestResponse.$empty();

late double responseParam;

@override
int $estimate() {
$reset();
int bytes = 16;
return bytes;
}

@override
void $pack() {
$initPack(216725115);
$packDouble(responseParam);
}

@override
void $unpack() {
$initUnpack();
responseParam = $unpackDouble();
}

@override
String toString() {
return 'TestResponse\x1b[0m(responseParam: ${PackMe.dye(responseParam)})';
}
}

class TestRequest extends PackMeMessage {
TestRequest({
required this.requestParam,
});
TestRequest.$empty();

late double requestParam;

TestResponse $response({
required double responseParam,
}) {
final TestResponse message = TestResponse(responseParam: responseParam);
message.$request = this;
return message;
}

@override
int $estimate() {
$reset();
int bytes = 16;
return bytes;
}

@override
void $pack() {
$initPack(764832169);
$packDouble(requestParam);
}

@override
void $unpack() {
$initUnpack();
requestParam = $unpackDouble();
}

@override
String toString() {
return 'TestRequest\x1b[0m(requestParam: ${PackMe.dye(requestParam)})';
}
}

final Map<int, PackMeMessage Function()> testMessageFactory = <int, PackMeMessage Function()>{
216725115: () => TestResponse.$empty(),
764832169: () => TestRequest.$empty(),
};
15 changes: 15 additions & 0 deletions test/modules/test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:serveme/serveme.dart';

class TestModule extends Module<ServeMeClient> {
@override
Future<void> init() async {
}

@override
void run() {
}

@override
Future<void> dispose() async {
}
}
10 changes: 10 additions & 0 deletions test/packme/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"test": [
{
"request_param": "double"
},
{
"response_param": "double"
}
]
}
147 changes: 147 additions & 0 deletions test/serveme_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:connectme/connectme.dart';
import 'package:serveme/serveme.dart';
import 'package:test/test.dart';
import 'generated/test.generated.dart';
import 'modules/test.dart';

void main() {
late ServeMe<ServeMeClient> server;
late ConnectMeClient client;
late Timer timer;
late TestModule module;

group('Connection tests', () {
test('ServeMe.run() and ConnectMe.connect()', () async {
timer = Timer(const Duration(seconds: 2), () => fail('Operation timed out'));
server = ServeMe<ServeMeClient>(
configFile: 'test/config_test.yaml',
modules: <String, Module<ServeMeClient>>{
'test': module = TestModule(),
},
);
await server.run();
module.events.listen<ConnectEvent<ServeMeClient>>(expectAsync1<Future<void>, dynamic>((dynamic event) async {
expect(event.client, isA<ServeMeClient>());
}));
module.events.listen<DisconnectEvent<ServeMeClient>>(expectAsync1<Future<void>, dynamic>((dynamic event) async {
expect(event.client, isA<ServeMeClient>());
}));
client = await ConnectMe.connect('ws://127.0.0.1:31337',
onConnect: expectAsync0<void>(() {}),
);
await client.close();
await server.stop();
timer.cancel();
});
});

group('ServeMe data exchange tests', () {
setUp(() async {
timer = Timer(const Duration(seconds: 2), () => fail('Operation timed out'));
server = ServeMe<ServeMeClient>(
configFile: 'test/config_test.yaml',
modules: <String, Module<ServeMeClient>>{
'test': module = TestModule(),
},
);
await server.run();
client = await ConnectMe.connect('ws://127.0.0.1:31337');
});

tearDown(() async {
await client.close();
await server.stop();
timer.cancel();
});

test('Client sends String to server', () async {
final Completer<String> completer = Completer<String>();
server.listen<String>((String message, ConnectMeClient client) async {
completer.complete(message);
});
client.send('Test message from client');
expect(await completer.future, 'Test message from client');
});

test('Server broadcasts String to clients', () async {
final Completer<String> completer = Completer<String>();
client.listen<String>((String message) {
completer.complete(message);
});
server.broadcast('Test message from server');
expect(await completer.future, 'Test message from server');
});

test('Client sends Uint8List to server', () async {
final Completer<Uint8List> completer = Completer<Uint8List>();
server.listen<Uint8List>((Uint8List message, ConnectMeClient client) async {
completer.complete(message);
});
client.send(Uint8List.fromList(<int>[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]));
expect(await completer.future, Uint8List.fromList(<int>[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]));
});

test('Server broadcasts Uint8List to clients', () async {
final Completer<Uint8List> completer = Completer<Uint8List>();
client.listen<Uint8List>((Uint8List message) {
completer.complete(message);
});
server.broadcast(Uint8List.fromList(<int>[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]));
expect(await completer.future, Uint8List.fromList(<int>[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]));
});

test('Client sends TestResponse query to server', () async {
server.register(testMessageFactory);
server.listen<TestRequest>((TestRequest request, ConnectMeClient client) async {
client.send(request.$response(responseParam: request.requestParam));
});
client.register(testMessageFactory);
final TestResponse response = await client.query<TestResponse>(TestRequest(requestParam: 3.1415926535));
expect(response.responseParam, 3.1415926535);
});

test('Server sends TestResponse query to client', () async {
client.register(testMessageFactory);
client.listen<TestRequest>((TestRequest request) {
client.send(request.$response(responseParam: request.requestParam));
});
server.register(testMessageFactory);
final TestResponse response = await server.clients.first.query<TestResponse>(TestRequest(requestParam: 3.1415926535));
expect(response.responseParam, 3.1415926535);
});
});

group('ServeMe API tests', () {
setUp(() async {
timer = Timer(const Duration(seconds: 2), () => fail('Operation timed out'));
server = ServeMe<ServeMeClient>(
configFile: 'test/config_test.yaml',
modules: <String, Module<ServeMeClient>>{
'test': module = TestModule(),
},
);
await server.run();
});

tearDown(() async {
await server.stop();
timer.cancel();
});

test('TickEvent dispatch and handle', () async {
module.events.listen<TickEvent>(expectAsync1<Future<void>, dynamic>((dynamic event) async {
expect(event, isA<TickEvent>());
}));
});

test('Scheduler', () async {
final Task task = Task(DateTime.now(), expectAsync1<Future<void>, dynamic>((dynamic time) async {
expect(time, isA<DateTime>());
}));
module.scheduler.schedule(task);
});
});
}

0 comments on commit 65a17b2

Please sign in to comment.