-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mad Sheogorath
committed
Oct 27, 2021
1 parent
4dfeaa9
commit 65a17b2
Showing
9 changed files
with
271 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,5 @@ dependencies: | |
yaml: ^3.1.0 | ||
connectme: ^1.1.1 | ||
packme: ^1.1.5 | ||
dev_dependencies: | ||
test: ^1.17.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"test": [ | ||
{ | ||
"request_param": "double" | ||
}, | ||
{ | ||
"response_param": "double" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
} |