@@ -4,9 +4,9 @@ import 'dart:isolate';
4
4
5
5
import 'package:objectbox/src/bindings/bindings.dart' ;
6
6
import 'package:test/test.dart' ;
7
+ import 'package:objectbox/observable.dart' ;
7
8
8
9
import 'entity.dart' ;
9
- import 'objectbox.g.dart' ;
10
10
import 'test_env.dart' ;
11
11
12
12
// We want to have types explicit - verifying the return types of functions.
@@ -15,8 +15,8 @@ void main() {
15
15
/// Set up a simple echo isolate with request-response communication.
16
16
/// This isn't really a test, just an example of how isolates can communicate.
17
17
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);
20
20
21
21
Completer sendPortCompleter = Completer <SendPort >();
22
22
Completer responseCompleter;
@@ -41,12 +41,15 @@ void main() {
41
41
// Send a message to the isolate
42
42
expect (await call ('hello' ), equals ('re:hello' ));
43
43
expect (await call ('foo' ), equals ('re:foo' ));
44
+
45
+ isolate.kill (priority: Isolate .immediate);
46
+ receivePort.close ();
44
47
});
45
48
46
49
/// Work with a single store accross multiple isolates
47
50
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);
50
53
51
54
final sendPortCompleter = Completer <SendPort >();
52
55
Completer <dynamic > responseCompleter;
@@ -72,22 +75,38 @@ void main() {
72
75
final env = TestEnv ('isolates' );
73
76
expect (await call (env.store.ptr.address), equals ('store set' ));
74
77
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 ();
78
97
});
79
98
}
80
99
81
100
// Echoes back any received message.
82
101
void echoIsolate (SendPort sendPort) async {
83
102
// Open the ReceivePort to listen for incoming messages
84
- var port = ReceivePort ();
103
+ final port = ReceivePort ();
85
104
86
105
// Send the port where the main isolate can contact us
87
106
sendPort.send (port.sendPort);
88
107
89
108
// Listen for messages
90
- await for (var data in port) {
109
+ await for (final data in port) {
91
110
// `data` is the message received.
92
111
print ('Isolate received: $data ' );
93
112
sendPort.send ('re:$data ' );
@@ -97,14 +116,14 @@ void echoIsolate(SendPort sendPort) async {
97
116
// Creates data in the background, in the [Store] received as the first message.
98
117
void createDataIsolate (SendPort sendPort) async {
99
118
// Open the ReceivePort to listen for incoming messages
100
- var port = ReceivePort ();
119
+ final port = ReceivePort ();
101
120
102
121
// Send the port where the main isolate can contact us
103
122
sendPort.send (port.sendPort);
104
123
105
124
TestEnv env;
106
125
// Listen for messages
107
- await for (var data in port) {
126
+ await for (final data in port) {
108
127
if (env == null ) {
109
128
// first message data is Store's C pointer address
110
129
env = TestEnv .fromPtr (Pointer <OBX_store >.fromAddress (data));
0 commit comments