2021-03-08 12:56:39 +00:00
|
|
|
import 'dart:async';
|
2021-05-15 11:20:33 +00:00
|
|
|
import 'package:angel3_pub_sub/angel3_pub_sub.dart';
|
|
|
|
import 'package:angel3_pub_sub/isolate.dart';
|
2021-03-08 12:56:39 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
2021-05-01 01:54:59 +00:00
|
|
|
void main() {
|
|
|
|
late Server server;
|
|
|
|
late Client client1, client2, client3;
|
|
|
|
late IsolateClient trustedClient;
|
|
|
|
late IsolateAdapter adapter;
|
2021-03-08 12:56:39 +00:00
|
|
|
|
|
|
|
setUp(() async {
|
2021-05-01 01:54:59 +00:00
|
|
|
adapter = IsolateAdapter();
|
2021-03-08 12:56:39 +00:00
|
|
|
client1 =
|
2021-05-01 01:54:59 +00:00
|
|
|
IsolateClient('isolate_test::secret', adapter.receivePort.sendPort);
|
|
|
|
client2 =
|
|
|
|
IsolateClient('isolate_test::secret2', adapter.receivePort.sendPort);
|
|
|
|
client3 =
|
|
|
|
IsolateClient('isolate_test::secret3', adapter.receivePort.sendPort);
|
|
|
|
trustedClient = IsolateClient(null, adapter.receivePort.sendPort);
|
2021-03-08 12:56:39 +00:00
|
|
|
|
2021-05-01 01:54:59 +00:00
|
|
|
server = Server([adapter])
|
2021-03-08 12:56:39 +00:00
|
|
|
..registerClient(const ClientInfo('isolate_test::secret'))
|
|
|
|
..registerClient(const ClientInfo('isolate_test::secret2'))
|
|
|
|
..registerClient(const ClientInfo('isolate_test::secret3'))
|
|
|
|
..registerClient(
|
|
|
|
const ClientInfo('isolate_test::no_publish', canPublish: false))
|
|
|
|
..registerClient(
|
|
|
|
const ClientInfo('isolate_test::no_subscribe', canSubscribe: false))
|
|
|
|
..start();
|
|
|
|
|
|
|
|
var sub = await client3.subscribe('foo');
|
|
|
|
sub.listen((data) {
|
|
|
|
print('Client3 caught foo: $data');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
tearDown(() {
|
|
|
|
Future.wait([
|
|
|
|
server.close(),
|
|
|
|
client1.close(),
|
|
|
|
client2.close(),
|
|
|
|
client3.close(),
|
|
|
|
trustedClient.close()
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
group('trusted', () {
|
|
|
|
test('can publish', () async {
|
|
|
|
await trustedClient.publish('hey', 'bye');
|
|
|
|
expect(trustedClient.clientId, isNotNull);
|
|
|
|
});
|
|
|
|
test('can sub/unsub', () async {
|
2021-05-01 01:54:59 +00:00
|
|
|
String? clientId;
|
2021-03-08 12:56:39 +00:00
|
|
|
await trustedClient.publish('heyaaa', 'byeaa');
|
|
|
|
expect(clientId = trustedClient.clientId, isNotNull);
|
|
|
|
|
|
|
|
var sub = await trustedClient.subscribe('yeppp');
|
|
|
|
expect(trustedClient.clientId, clientId);
|
|
|
|
|
|
|
|
await sub.unsubscribe();
|
|
|
|
expect(trustedClient.clientId, clientId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('subscribers receive published events', () async {
|
|
|
|
var sub = await client2.subscribe('foo');
|
|
|
|
await client1.publish('foo', 'bar');
|
|
|
|
expect(await sub.first, 'bar');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('subscribers are not sent their own events', () async {
|
|
|
|
var sub = await client1.subscribe('foo');
|
|
|
|
await client1.publish('foo',
|
|
|
|
'<this should never be sent to client1, because client1 sent it.>');
|
|
|
|
await sub.unsubscribe();
|
|
|
|
expect(await sub.isEmpty, isTrue);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('can unsubscribe', () async {
|
|
|
|
var sub = await client2.subscribe('foo');
|
|
|
|
await client1.publish('foo', 'bar');
|
|
|
|
await sub.unsubscribe();
|
|
|
|
await client1.publish('foo', '<client2 will not catch this!>');
|
|
|
|
expect(await sub.length, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
group('isolate_server', () {
|
|
|
|
test('reject unknown client id', () async {
|
|
|
|
try {
|
2021-05-01 01:54:59 +00:00
|
|
|
var client = IsolateClient(
|
2021-03-08 12:56:39 +00:00
|
|
|
'isolate_test::invalid', adapter.receivePort.sendPort);
|
|
|
|
await client.publish('foo', 'bar');
|
|
|
|
throw 'Invalid client ID\'s should throw an error, but they do not.';
|
|
|
|
} on PubSubException catch (e) {
|
|
|
|
print('Expected exception was thrown: ${e.message}');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('reject unprivileged publish', () async {
|
|
|
|
try {
|
2021-05-01 01:54:59 +00:00
|
|
|
var client = IsolateClient(
|
2021-03-08 12:56:39 +00:00
|
|
|
'isolate_test::no_publish', adapter.receivePort.sendPort);
|
|
|
|
await client.publish('foo', 'bar');
|
|
|
|
throw 'Unprivileged publishes should throw an error, but they do not.';
|
|
|
|
} on PubSubException catch (e) {
|
|
|
|
print('Expected exception was thrown: ${e.message}');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('reject unprivileged subscribe', () async {
|
|
|
|
try {
|
2021-05-01 01:54:59 +00:00
|
|
|
var client = IsolateClient(
|
2021-03-08 12:56:39 +00:00
|
|
|
'isolate_test::no_subscribe', adapter.receivePort.sendPort);
|
|
|
|
await client.subscribe('foo');
|
|
|
|
throw 'Unprivileged subscribes should throw an error, but they do not.';
|
|
|
|
} on PubSubException catch (e) {
|
|
|
|
print('Expected exception was thrown: ${e.message}');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|