|
1 | 1 | import 'dart:ffi' as ffi;
|
2 | 2 | import 'dart:io';
|
| 3 | +import 'dart:isolate'; |
3 | 4 |
|
| 5 | +import 'package:async/async.dart'; |
4 | 6 | import 'package:objectbox/internal.dart';
|
5 | 7 | import 'package:objectbox/src/native/bindings/bindings.dart';
|
6 | 8 | import 'package:objectbox/src/native/bindings/helpers.dart';
|
@@ -60,25 +62,47 @@ void main() {
|
60 | 62 | env.closeAndDelete();
|
61 | 63 | });
|
62 | 64 |
|
63 |
| - test('store attach', () { |
| 65 | + test('store attach fails if same isolate', () { |
| 66 | + final env = TestEnv('basics'); |
| 67 | + expect( |
| 68 | + () => Store.attach(getObjectBoxModel(), env.dir.path), |
| 69 | + throwsA(predicate((UnsupportedError e) => |
| 70 | + e.message!.contains('Cannot create multiple Store instances')))); |
| 71 | + env.closeAndDelete(); |
| 72 | + }); |
| 73 | + |
| 74 | + test('store attach remains open if main store closed', () async { |
64 | 75 | final env = TestEnv('basics');
|
65 | 76 | final store1 = env.store;
|
66 |
| - final store2 = Store.attach(getObjectBoxModel(), env.dir.path); |
67 |
| - expect(store1, isNot(store2)); |
68 |
| - expect(InternalStoreAccess.ptr(store1), |
69 |
| - isNot(InternalStoreAccess.ptr(store2))); |
| 77 | + final receivePort = ReceivePort(); |
| 78 | + final received = StreamQueue<dynamic>(receivePort); |
| 79 | + await Isolate.spawn(storeAttachIsolate, |
| 80 | + StoreAttachIsolateInit(receivePort.sendPort, env.dir.path)); |
| 81 | + final commandPort = await received.next as SendPort; |
| 82 | + |
| 83 | + // Check native instance pointer is different. |
| 84 | + final store2Address = await received.next as int; |
| 85 | + expect(InternalStoreAccess.ptr(store1).address, isNot(store2Address)); |
70 | 86 |
|
71 | 87 | final id = store1.box<TestEntity>().put(TestEntity(tString: 'foo'));
|
72 | 88 | expect(id, 1);
|
73 | 89 | // Close original store to test store remains open until all refs closed.
|
74 | 90 | store1.close();
|
75 | 91 | expect(true, Store.isOpen('testdata-basics'));
|
76 |
| - final read = store2.box<TestEntity>().get(id); |
77 |
| - expect(read, isNotNull); |
78 |
| - expect(read!.tString, 'foo'); |
79 |
| - store2.close(); |
| 92 | + |
| 93 | + // Read data with attached store. |
| 94 | + commandPort.send(id); |
| 95 | + final readtString = await received.next as String?; |
| 96 | + expect(readtString, isNotNull); |
| 97 | + expect(readtString, 'foo'); |
| 98 | + |
| 99 | + // Close attached store, should close store completely. |
| 100 | + commandPort.send(null); |
| 101 | + await received.next; |
80 | 102 | expect(false, Store.isOpen('testdata-basics'));
|
81 |
| - env.closeAndDelete(); |
| 103 | + |
| 104 | + // Dispose StreamQueue. |
| 105 | + await received.cancel(); |
82 | 106 | });
|
83 | 107 |
|
84 | 108 | test('store is open', () {
|
@@ -166,3 +190,31 @@ void main() {
|
166 | 190 | Directory('basics').deleteSync(recursive: true);
|
167 | 191 | });
|
168 | 192 | }
|
| 193 | + |
| 194 | +class StoreAttachIsolateInit { |
| 195 | + SendPort sendPort; |
| 196 | + String path; |
| 197 | + |
| 198 | + StoreAttachIsolateInit(this.sendPort, this.path); |
| 199 | +} |
| 200 | + |
| 201 | +void storeAttachIsolate(StoreAttachIsolateInit init) async { |
| 202 | + final store2 = Store.attach(getObjectBoxModel(), init.path); |
| 203 | + |
| 204 | + final commandPort = ReceivePort(); |
| 205 | + init.sendPort.send(commandPort.sendPort); |
| 206 | + init.sendPort.send(InternalStoreAccess.ptr(store2).address); |
| 207 | + |
| 208 | + await for (final message in commandPort) { |
| 209 | + if (message is int) { |
| 210 | + final read = store2.box<TestEntity>().get(message); |
| 211 | + init.sendPort.send(read?.tString); |
| 212 | + } else if (message == null) { |
| 213 | + store2.close(); |
| 214 | + init.sendPort.send(null); |
| 215 | + break; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + print('Store attach isolate finished'); |
| 220 | +} |
0 commit comments