-
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
1 parent
9c653c2
commit 7f2c93d
Showing
1 changed file
with
147 additions
and
0 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
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>()); | ||
}); | ||
} |