Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for router #12

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions lib/src/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class ClientSideLocalBaseSession implements IBaseSession {
String authrole() => _authrole;

@override
serializer() => _serializer;
Serializer serializer() => _serializer;

@override
Future send(Object data) async {
Expand All @@ -238,8 +238,11 @@ class ClientSideLocalBaseSession implements IBaseSession {

@override
Future<Object> receive() async {
await _completer.future;
_completer = Completer();
if (_incomingMessages.isEmpty) {
await _completer.future;
_completer = Completer();
}

return _incomingMessages.removeFirst();
}

Expand All @@ -258,7 +261,9 @@ class ClientSideLocalBaseSession implements IBaseSession {

Future feed(Object data) async {
_incomingMessages.add(data);
_completer.complete();
if (!_completer.isCompleted) {
_completer.complete();
}
}
}

Expand Down
91 changes: 91 additions & 0 deletions test/router_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import "package:test/test.dart";
import "package:wampproto/messages.dart" as msg;
import "package:wampproto/serializers.dart";

import "package:xconn/exports.dart";
import "package:xconn/src/types.dart";

void main() {
const testProcedure = "io.xconn.test_procedure";
const testTopic = "io.xconn.test_topic";

var router = Router()..addRealm("realm1");

var serializer = JSONSerializer();
var clientSideBase = ClientSideLocalBaseSession(1, "realm1", "local", "local", serializer, router);
var serverSideBase = ServerSideLocalBaseSession(1, "realm1", "local", "local", serializer, other: clientSideBase);

router.attachClient(serverSideBase);

late int registrationID;

test("register", () async {
var registerMsg = msg.Register(3, testProcedure);
await router.receiveMessage(clientSideBase, registerMsg);

var registered = await clientSideBase.receiveMessage();
expect(registered, isA<msg.Registered>());

registrationID = (registered as msg.Registered).registrationID;
});

test("call", () async {
var callMsg = msg.Call(4, testProcedure);
await router.receiveMessage(clientSideBase, callMsg);

var invocation = await clientSideBase.receiveMessage();
expect(invocation, isA<msg.Invocation>());

var requestID = (invocation as msg.Invocation).requestID;
var yieldMsg = msg.Yield(requestID);
await router.receiveMessage(clientSideBase, yieldMsg);

var result = await clientSideBase.receiveMessage();
expect(result, isA<msg.Result>());
});

test("unregister", () async {
var unregisterMsg = msg.UnRegister(5, registrationID);
await router.receiveMessage(clientSideBase, unregisterMsg);

var unregistered = await clientSideBase.receiveMessage();
expect(unregistered, isA<msg.UnRegistered>());
});

late int subscriptionID;

test("subscribe", () async {
var subscribeMsg = msg.Subscribe(6, testTopic);
await router.receiveMessage(clientSideBase, subscribeMsg);

var subscribed = await clientSideBase.receiveMessage();
expect(subscribed, isA<msg.Subscribed>());

subscriptionID = (subscribed as msg.Subscribed).subscriptionID;
});

test("publish", () async {
var publish = msg.Publish(7, testTopic);
await router.receiveMessage(clientSideBase, publish);

var event = await clientSideBase.receiveMessage();
expect(event, isA<msg.Event>());

var publishAck = msg.Publish(8, testTopic, options: {"acknowledge": true});
await router.receiveMessage(clientSideBase, publishAck);

var eventAck = await clientSideBase.receiveMessage();
expect(eventAck, isA<msg.Event>());

var published = await clientSideBase.receiveMessage();
expect(published, isA<msg.Published>());
});

test("unsubscribe", () async {
var unsubscribeMsg = msg.UnSubscribe(9, subscriptionID);
await router.receiveMessage(clientSideBase, unsubscribeMsg);

var unsubscribed = await clientSideBase.receiveMessage();
expect(unsubscribed, isA<msg.UnSubscribed>());
});
}