Skip to content

Commit 9a792dd

Browse files
committed
isolates - test observers work fine
currently broken: Cannot invoke callback on incorrect isolate.
1 parent 5c6f6d7 commit 9a792dd

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

test/isolates_test.dart

+31-12
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import 'dart:isolate';
44

55
import 'package:objectbox/src/bindings/bindings.dart';
66
import 'package:test/test.dart';
7+
import 'package:objectbox/observable.dart';
78

89
import 'entity.dart';
9-
import 'objectbox.g.dart';
1010
import 'test_env.dart';
1111

1212
// We want to have types explicit - verifying the return types of functions.
@@ -15,8 +15,8 @@ void main() {
1515
/// Set up a simple echo isolate with request-response communication.
1616
/// This isn't really a test, just an example of how isolates can communicate.
1717
test('isolates two-way communication example', () async {
18-
var receivePort = ReceivePort();
19-
await Isolate.spawn(echoIsolate, receivePort.sendPort);
18+
final receivePort = ReceivePort();
19+
final isolate = await Isolate.spawn(echoIsolate, receivePort.sendPort);
2020

2121
Completer sendPortCompleter = Completer<SendPort>();
2222
Completer responseCompleter;
@@ -41,12 +41,15 @@ void main() {
4141
// Send a message to the isolate
4242
expect(await call('hello'), equals('re:hello'));
4343
expect(await call('foo'), equals('re:foo'));
44+
45+
isolate.kill(priority: Isolate.immediate);
46+
receivePort.close();
4447
});
4548

4649
/// Work with a single store accross multiple isolates
4750
test('single store in multiple isolates', () async {
48-
var receivePort = ReceivePort();
49-
await Isolate.spawn(createDataIsolate, receivePort.sendPort);
51+
final receivePort = ReceivePort();
52+
final isolate = await Isolate.spawn(createDataIsolate, receivePort.sendPort);
5053

5154
final sendPortCompleter = Completer<SendPort>();
5255
Completer<dynamic> responseCompleter;
@@ -72,22 +75,38 @@ void main() {
7275
final env = TestEnv('isolates');
7376
expect(await call(env.store.ptr.address), equals('store set'));
7477

75-
expect(env.box.isEmpty(), isTrue);
76-
expect(await call(['put', 'Foo']), equals(1)); // returns inserted id = 1
77-
expect(env.box.get(1).tString, equals('Foo'));
78+
{
79+
// check simple box operations
80+
expect(env.box.isEmpty(), isTrue);
81+
expect(await call(['put', 'Foo']), equals(1)); // returns inserted id = 1
82+
expect(env.box.get(1).tString, equals('Foo'));
83+
}
84+
85+
{
86+
// verify that query streams (using observers) work fine across isolates
87+
final queryStream = env.box.query().build().findStream();
88+
expect(await call(['put', 'Bar']), equals(2));
89+
List<TestEntity> found =
90+
await queryStream.first.timeout(Duration(seconds: 1));
91+
expect(found.length, equals(2));
92+
expect(found.last.tString, equals('Bar'));
93+
}
94+
95+
isolate.kill(priority: Isolate.immediate);
96+
receivePort.close();
7897
});
7998
}
8099

81100
// Echoes back any received message.
82101
void echoIsolate(SendPort sendPort) async {
83102
// Open the ReceivePort to listen for incoming messages
84-
var port = ReceivePort();
103+
final port = ReceivePort();
85104

86105
// Send the port where the main isolate can contact us
87106
sendPort.send(port.sendPort);
88107

89108
// Listen for messages
90-
await for (var data in port) {
109+
await for (final data in port) {
91110
// `data` is the message received.
92111
print('Isolate received: $data');
93112
sendPort.send('re:$data');
@@ -97,14 +116,14 @@ void echoIsolate(SendPort sendPort) async {
97116
// Creates data in the background, in the [Store] received as the first message.
98117
void createDataIsolate(SendPort sendPort) async {
99118
// Open the ReceivePort to listen for incoming messages
100-
var port = ReceivePort();
119+
final port = ReceivePort();
101120

102121
// Send the port where the main isolate can contact us
103122
sendPort.send(port.sendPort);
104123

105124
TestEnv env;
106125
// Listen for messages
107-
await for (var data in port) {
126+
await for (final data in port) {
108127
if (env == null) {
109128
// first message data is Store's C pointer address
110129
env = TestEnv.fromPtr(Pointer<OBX_store>.fromAddress(data));

0 commit comments

Comments
 (0)