Fixed pub_sub tests errors

This commit is contained in:
thomashii@dukefirehawk.com 2021-05-01 08:44:20 +08:00
parent 84d4f3799e
commit e122133764
2 changed files with 34 additions and 32 deletions

View file

@ -3,23 +3,23 @@ import 'package:pub_sub/pub_sub.dart';
import 'package:pub_sub/isolate.dart';
import 'package:test/test.dart';
main() {
void main() {
Server server;
Client client1, client2, client3;
IsolateClient trustedClient;
IsolateAdapter adapter;
setUp(() async {
adapter = new IsolateAdapter();
adapter = IsolateAdapter();
client1 =
new IsolateClient('isolate_test::secret', adapter.receivePort.sendPort);
client2 = new IsolateClient(
'isolate_test::secret2', adapter.receivePort.sendPort);
client3 = new IsolateClient(
'isolate_test::secret3', adapter.receivePort.sendPort);
trustedClient = new IsolateClient(null, adapter.receivePort.sendPort);
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);
server = new Server([adapter])
server = Server([adapter])
..registerClient(const ClientInfo('isolate_test::secret'))
..registerClient(const ClientInfo('isolate_test::secret2'))
..registerClient(const ClientInfo('isolate_test::secret3'))
@ -88,7 +88,7 @@ main() {
group('isolate_server', () {
test('reject unknown client id', () async {
try {
var client = new IsolateClient(
var client = IsolateClient(
'isolate_test::invalid', adapter.receivePort.sendPort);
await client.publish('foo', 'bar');
throw 'Invalid client ID\'s should throw an error, but they do not.';
@ -99,7 +99,7 @@ main() {
test('reject unprivileged publish', () async {
try {
var client = new IsolateClient(
var client = IsolateClient(
'isolate_test::no_publish', adapter.receivePort.sendPort);
await client.publish('foo', 'bar');
throw 'Unprivileged publishes should throw an error, but they do not.';
@ -110,7 +110,7 @@ main() {
test('reject unprivileged subscribe', () async {
try {
var client = new IsolateClient(
var client = IsolateClient(
'isolate_test::no_subscribe', adapter.receivePort.sendPort);
await client.subscribe('foo');
throw 'Unprivileged subscribes should throw an error, but they do not.';

View file

@ -6,7 +6,7 @@ import 'package:pub_sub/json_rpc_2.dart';
import 'package:stream_channel/stream_channel.dart';
import 'package:test/test.dart';
main() {
void main() {
ServerSocket serverSocket;
Server server;
Client client1, client2, client3;
@ -16,7 +16,7 @@ main() {
setUp(() async {
serverSocket = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
adapter = new JsonRpc2Adapter(
adapter = JsonRpc2Adapter(
serverSocket.map<StreamChannel<String>>(streamSocket),
isTrusted: true);
@ -29,15 +29,12 @@ main() {
var socket4 =
await Socket.connect(InternetAddress.loopbackIPv4, serverSocket.port);
client1 =
new JsonRpc2Client('json_rpc_2_test::secret', streamSocket(socket1));
client2 =
new JsonRpc2Client('json_rpc_2_test::secret2', streamSocket(socket2));
client3 =
new JsonRpc2Client('json_rpc_2_test::secret3', streamSocket(socket3));
trustedClient = new JsonRpc2Client(null, streamSocket(socket4));
client1 = JsonRpc2Client('json_rpc_2_test::secret', streamSocket(socket1));
client2 = JsonRpc2Client('json_rpc_2_test::secret2', streamSocket(socket2));
client3 = JsonRpc2Client('json_rpc_2_test::secret3', streamSocket(socket3));
trustedClient = JsonRpc2Client(null, streamSocket(socket4));
server = new Server([adapter])
server = Server([adapter])
..registerClient(const ClientInfo('json_rpc_2_test::secret'))
..registerClient(const ClientInfo('json_rpc_2_test::secret2'))
..registerClient(const ClientInfo('json_rpc_2_test::secret3'))
@ -54,8 +51,12 @@ main() {
});
tearDown(() {
Future.wait(
[server.close(), client1.close(), client2.close(), client3.close()]);
Future.wait([
server?.close(),
client1?.close(),
client2?.close(),
client3?.close()
]);
});
group('trusted', () {
@ -104,7 +105,7 @@ main() {
var sock = await Socket.connect(
InternetAddress.loopbackIPv4, serverSocket.port);
var client =
new JsonRpc2Client('json_rpc_2_test::invalid', streamSocket(sock));
JsonRpc2Client('json_rpc_2_test::invalid', streamSocket(sock));
await client.publish('foo', 'bar');
throw 'Invalid client ID\'s should throw an error, but they do not.';
} on PubSubException catch (e) {
@ -116,8 +117,8 @@ main() {
try {
var sock = await Socket.connect(
InternetAddress.loopbackIPv4, serverSocket.port);
var client = new JsonRpc2Client(
'json_rpc_2_test::no_publish', streamSocket(sock));
var client =
JsonRpc2Client('json_rpc_2_test::no_publish', streamSocket(sock));
await client.publish('foo', 'bar');
throw 'Unprivileged publishes should throw an error, but they do not.';
} on PubSubException catch (e) {
@ -129,8 +130,8 @@ main() {
try {
var sock = await Socket.connect(
InternetAddress.loopbackIPv4, serverSocket.port);
var client = new JsonRpc2Client(
'json_rpc_2_test::no_subscribe', streamSocket(sock));
var client =
JsonRpc2Client('json_rpc_2_test::no_subscribe', streamSocket(sock));
await client.subscribe('foo');
throw 'Unprivileged subscribes should throw an error, but they do not.';
} on PubSubException catch (e) {
@ -141,8 +142,9 @@ main() {
}
StreamChannel<String> streamSocket(Socket socket) {
var channel = new _SocketStreamChannel(socket);
return channel.transform(new StreamChannelTransformer.fromCodec(utf8));
var channel = _SocketStreamChannel(socket);
var transfomer = StreamChannelTransformer.fromCodec(utf8);
return channel.cast<List<int>>().transform(transfomer);
}
class _SocketStreamChannel extends StreamChannelMixin<List<int>> {
@ -152,7 +154,7 @@ class _SocketStreamChannel extends StreamChannelMixin<List<int>> {
_SocketStreamChannel(this.socket);
@override
StreamSink<List<int>> get sink => _sink ??= new _SocketSink(socket);
StreamSink<List<int>> get sink => _sink ??= _SocketSink(socket);
@override
Stream<List<int>> get stream => socket;