Skip to content

Commit

Permalink
Add tests for router
Browse files Browse the repository at this point in the history
  • Loading branch information
muzzammilshahid committed May 9, 2024
1 parent 9c653c2 commit 7f2c93d
Showing 1 changed file with 147 additions and 0 deletions.
147 changes: 147 additions & 0 deletions test/router_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import "dart:collection";
import "dart:typed_data";

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/helpers.dart";
import "package:xconn/src/types.dart";

class MockBaseSession implements IBaseSession {
MockBaseSession(this._id, this._realm, this._authid, this._authrole, this._serializer) {
if (getSubProtocol(_serializer) == jsonSubProtocol) {
_messages = Queue<String>();
} else {
_messages = Queue<Uint8List>();
}
}

final int _id;
final String _realm;
final String _authid;
final String _authrole;
final Serializer _serializer;
late Queue<Object> _messages;

@override
int id() => _id;

@override
String realm() => _realm;

@override
String authid() => _authid;

@override
String authrole() => _authrole;

@override
void send(Object data) {
_messages.addLast(data);
}

@override
Future<Object> receive() async {
return _messages.removeFirst();
}

@override
void sendMessage(msg.Message msg) {
send(_serializer.serialize(msg));
}

@override
Future<msg.Message> receiveMessage() async {
return _serializer.deserialize(await receive());
}

@override
Future<void> close() async {}
}

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

var callee = MockBaseSession(1, "realm1", "john", "anonymous", JSONSerializer());
var caller = MockBaseSession(2, "realm1", "wick", "anonymous", JSONSerializer());

var router = Router()
..addRealm("realm1")
..attachClient(callee)
..attachClient(caller);

late int registrationID;

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

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

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

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

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

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

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

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

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

late int subscriptionID;

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

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

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

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

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

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

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

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

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

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

0 comments on commit 7f2c93d

Please sign in to comment.