platform/packages/sync/test/all_test.dart

103 lines
3 KiB
Dart
Raw Normal View History

2017-08-08 21:04:36 +00:00
import 'dart:isolate';
2021-08-20 04:59:02 +00:00
import 'package:angel3_framework/angel3_framework.dart';
import 'package:angel3_framework/http.dart';
import 'package:angel3_sync/angel3_sync.dart';
import 'package:angel3_test/angel3_test.dart';
import 'package:angel3_websocket/io.dart' as client;
import 'package:angel3_websocket/server.dart';
2021-09-12 02:52:19 +00:00
import 'package:belatuk_pub_sub/isolate.dart' as pub_sub;
import 'package:belatuk_pub_sub/belatuk_pub_sub.dart' as pub_sub;
2017-08-08 21:04:36 +00:00
import 'package:test/test.dart';
2021-06-20 12:37:20 +00:00
void main() {
late Angel app1, app2;
late TestClient app1Client;
late client.WebSockets app2Client;
late pub_sub.Server server;
late ReceivePort app1Port, app2Port;
2017-08-08 21:04:36 +00:00
setUp(() async {
2021-06-20 12:37:20 +00:00
var adapter = pub_sub.IsolateAdapter();
2017-08-08 21:04:36 +00:00
2021-06-20 12:37:20 +00:00
server = pub_sub.Server([
2017-08-08 21:04:36 +00:00
adapter,
])
..registerClient(const pub_sub.ClientInfo('angel_sync1'))
..registerClient(const pub_sub.ClientInfo('angel_sync2'))
..start();
2021-06-20 12:37:20 +00:00
app1 = Angel();
app2 = Angel();
2017-08-08 21:04:36 +00:00
2018-11-15 17:03:03 +00:00
app1.post('/message', (req, res) async {
2017-08-08 21:04:36 +00:00
// Manually broadcast. Even though app1 has no clients, it *should*
// propagate to app2.
2021-06-20 12:37:20 +00:00
var ws = req.container!.make<AngelWebSocket>()!;
2021-02-14 05:22:25 +00:00
// TODO: body is void
//var body = await req.parseBody();
var body = {};
2021-06-20 12:37:20 +00:00
await ws.batchEvent(WebSocketEvent(
2017-08-08 21:04:36 +00:00
eventName: 'message',
2018-11-15 17:03:03 +00:00
data: body['message'],
2017-08-08 21:04:36 +00:00
));
2018-11-15 17:03:03 +00:00
return 'Sent: ${body['message']}';
2017-08-08 21:04:36 +00:00
});
2021-06-20 12:37:20 +00:00
app1Port = ReceivePort();
var ws1 = AngelWebSocket(
2018-11-15 17:03:03 +00:00
app1,
2021-06-20 12:37:20 +00:00
synchronizationChannel: PubSubSynchronizationChannel(
pub_sub.IsolateClient('angel_sync1', adapter.receivePort.sendPort),
2017-08-08 21:04:36 +00:00
),
2018-11-15 17:03:03 +00:00
);
await app1.configure(ws1.configureServer);
app1.get('/ws', ws1.handleRequest);
2017-08-08 21:04:36 +00:00
app1Client = await connectTo(app1);
2021-06-20 12:37:20 +00:00
app2Port = ReceivePort();
var ws2 = AngelWebSocket(
2018-11-15 17:03:03 +00:00
app2,
2021-06-20 12:37:20 +00:00
synchronizationChannel: PubSubSynchronizationChannel(
pub_sub.IsolateClient('angel_sync2', adapter.receivePort.sendPort),
2017-08-08 21:04:36 +00:00
),
2018-11-15 17:03:03 +00:00
);
await app2.configure(ws2.configureServer);
app2.get('/ws', ws2.handleRequest);
2017-08-08 21:04:36 +00:00
2021-06-20 12:37:20 +00:00
var http = AngelHttp(app2);
2018-11-15 17:03:03 +00:00
await http.startServer();
var wsPath =
http.uri.replace(scheme: 'ws', path: '/ws').removeFragment().toString();
print(wsPath);
2021-06-20 12:37:20 +00:00
app2Client = client.WebSockets(wsPath);
2017-08-08 21:04:36 +00:00
await app2Client.connect();
});
tearDown(() {
server.close();
app1Port.close();
app2Port.close();
app1.close();
app2.close();
app1Client.close();
app2Client.close();
});
test('events propagate', () async {
// The point of this test is that neither app1 nor app2
// is aware that the other even exists.
//
// Regardless, a WebSocket event broadcast in app1 will be
// broadcast by app2 as well.
var stream = app2Client.on['message'];
2021-06-20 12:37:20 +00:00
var response = await app1Client
.post(Uri.parse('/message'), body: {'message': 'Hello, world!'});
2017-08-08 21:04:36 +00:00
print('app1 response: ${response.body}');
var msg = await stream.first.timeout(const Duration(seconds: 5));
print('app2 got message: ${msg.data}');
});
}