1
- import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest' ;
2
1
import { AbstractPowerSyncDatabase , SqliteBucketStorage , SyncStatus } from '@powersync/common' ;
3
2
import {
4
3
PowerSyncDatabase ,
5
4
SharedWebStreamingSyncImplementation ,
6
5
WebRemote ,
7
6
WebStreamingSyncImplementationOptions
8
7
} from '@powersync/web' ;
9
- import { testSchema } from './utils/testDb' ;
10
- import { TestConnector } from './utils/MockStreamOpenFactory' ;
11
8
import { Mutex } from 'async-mutex' ;
12
9
import Logger from 'js-logger' ;
10
+ import { afterEach , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest' ;
11
+ import { TestConnector } from './utils/MockStreamOpenFactory' ;
12
+ import { testSchema } from './utils/testDb' ;
13
13
14
14
describe ( 'Multiple Instances' , ( ) => {
15
15
const dbFilename = 'test-multiple-instances.db' ;
@@ -23,6 +23,8 @@ describe('Multiple Instances', () => {
23
23
schema : testSchema
24
24
} ) ;
25
25
26
+ beforeAll ( ( ) => Logger . useDefaults ( ) ) ;
27
+
26
28
beforeEach ( ( ) => {
27
29
db = openDatabase ( ) ;
28
30
} ) ;
@@ -184,34 +186,44 @@ describe('Multiple Instances', () => {
184
186
} ) ;
185
187
186
188
// Create the first streaming client
187
- const syncOptions1 : WebStreamingSyncImplementationOptions = {
189
+ const stream1 = new SharedWebStreamingSyncImplementation ( {
188
190
adapter : new SqliteBucketStorage ( db . database , new Mutex ( ) ) ,
189
191
remote : new WebRemote ( connector1 ) ,
190
192
uploadCrud : async ( ) => {
191
193
triggerUpload1 ( ) ;
192
194
connector1 . uploadData ( db ) ;
193
195
} ,
194
- identifier
195
- } ;
196
- const stream1 = new SharedWebStreamingSyncImplementation ( syncOptions1 ) ;
196
+ identifier,
197
+ retryDelayMs : 100 ,
198
+ flags : {
199
+ broadcastLogs : true
200
+ }
201
+ } ) ;
197
202
198
203
// Generate the second streaming sync implementation
199
204
const connector2 = new TestConnector ( ) ;
200
- const spy2 = vi . spyOn ( connector2 , 'uploadData' ) ;
205
+ // The second connector will be called first to upload, we don't want it to actually upload
206
+ // This will cause the sync uploads to be delayed as the CRUD queue did not change
207
+ const spy2 = vi . spyOn ( connector2 , 'uploadData' ) . mockImplementation ( async ( ) => { } ) ;
208
+
201
209
let triggerUpload2 : ( ) => void ;
202
210
const upload2TriggeredPromise = new Promise < void > ( ( resolve ) => {
203
211
triggerUpload2 = resolve ;
204
212
} ) ;
205
- const syncOptions2 : WebStreamingSyncImplementationOptions = {
213
+
214
+ const stream2 = new SharedWebStreamingSyncImplementation ( {
206
215
adapter : new SqliteBucketStorage ( db . database , new Mutex ( ) ) ,
207
216
remote : new WebRemote ( connector1 ) ,
208
217
uploadCrud : async ( ) => {
209
218
triggerUpload2 ( ) ;
210
219
connector2 . uploadData ( db ) ;
211
220
} ,
212
- identifier
213
- } ;
214
- const stream2 = new SharedWebStreamingSyncImplementation ( syncOptions2 ) ;
221
+ identifier,
222
+ retryDelayMs : 100 ,
223
+ flags : {
224
+ broadcastLogs : true
225
+ }
226
+ } ) ;
215
227
216
228
// Waits for the stream to be marked as connected
217
229
const stream2UpdatedPromise = new Promise < void > ( ( resolve , reject ) => {
@@ -230,6 +242,8 @@ describe('Multiple Instances', () => {
230
242
231
243
// The status in the second stream client should be updated
232
244
await stream2UpdatedPromise ;
245
+
246
+ console . log ( 'stream 2 status updated' ) ;
233
247
expect ( stream2 . isConnected ) . true ;
234
248
235
249
// Create something with CRUD in it.
@@ -243,6 +257,8 @@ describe('Multiple Instances', () => {
243
257
// The second connector should be called to upload
244
258
await upload2TriggeredPromise ;
245
259
260
+ console . log ( '2 upload was triggered' ) ;
261
+
246
262
// It should call the latest connected client
247
263
expect ( spy2 ) . toHaveBeenCalledOnce ( ) ;
248
264
0 commit comments