-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathstream.test.ts
More file actions
307 lines (267 loc) · 9.46 KB
/
Copy pathstream.test.ts
File metadata and controls
307 lines (267 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import {
BucketChecksum,
createBaseLogger,
DataStream,
PowerSyncConnectionOptions,
WASQLiteOpenFactory,
WASQLiteVFS
} from '@powersync/web';
import { describe, expect, it, onTestFinished, vi } from 'vitest';
import { TestConnector } from './utils/MockStreamOpenFactory';
import { ConnectedDatabaseUtils, generateConnectedDatabase } from './utils/generateConnectedDatabase';
const UPLOAD_TIMEOUT_MS = 3000;
const logger = createBaseLogger();
logger.useDefaults();
describe('Streaming', { sequential: true }, () => {
describe(
'Streaming - With Web Workers',
{
sequential: true
},
describeStreamingTests(() =>
generateConnectedDatabase({
powerSyncOptions: {
logger
}
})
)
);
describe(
'Streaming - Without Web Workers',
{
sequential: true
},
describeStreamingTests(() =>
generateConnectedDatabase({
powerSyncOptions: {
flags: {
useWebWorker: false
},
logger
}
})
)
);
describe(
'Streaming - With OPFS',
{
sequential: true
},
describeStreamingTests(() =>
generateConnectedDatabase({
powerSyncOptions: {
database: new WASQLiteOpenFactory({
dbFilename: 'streaming-opfs.sqlite',
vfs: WASQLiteVFS.OPFSCoopSyncVFS
}),
logger
}
})
)
);
it('Should handle checkpoints during the upload process', async () => {
const { powersync, remote, uploadSpy } = await generateConnectedDatabase();
expect(powersync.connected).toBe(true);
let resolveUploadPromise: () => void;
let resolveUploadStartedPromise: () => void;
const completeUploadPromise = new Promise<void>((resolve) => {
resolveUploadPromise = resolve;
});
const uploadStartedPromise = new Promise<void>((resolve) => {
resolveUploadStartedPromise = resolve;
});
async function expectUserRows(amount: number) {
const row = await powersync.get<{ r: number }>('SELECT COUNT(*) AS r FROM users');
expect(row.r).toBe(amount);
}
uploadSpy.mockImplementation(async (db) => {
const batch = await db.getCrudBatch();
if (!batch) return;
resolveUploadStartedPromise();
await completeUploadPromise;
await batch?.complete();
});
// trigger an upload
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['from local']);
await expectUserRows(1);
await uploadStartedPromise;
// A connector could have uploaded data (triggering a checkpoint) before finishing
remote.enqueueLine({
checkpoint: {
write_checkpoint: '1',
last_op_id: '2',
buckets: [{ bucket: 'a', priority: 3, checksum: 0 }]
}
});
remote.generateCheckpoint.mockImplementation(() => {
return {
data: {
write_checkpoint: '1'
}
};
});
remote.enqueueLine({
data: {
bucket: 'a',
data: [
{
checksum: 0,
op_id: '1',
op: 'PUT',
object_id: '1',
object_type: 'users',
data: '{"id": "test1", "name": "from local"}'
},
{
checksum: 0,
op_id: '2',
op: 'PUT',
object_id: '2',
object_type: 'users',
data: '{"id": "test1", "name": "additional entry"}'
}
]
}
});
remote.enqueueLine({
checkpoint_complete: {
last_op_id: '2'
}
});
// Give the sync client some time to process these
await new Promise<void>((resolve) => setTimeout(resolve, 500));
// Despite receiving a valid checkpoint with two rows, it should not be visible because we have local data.
await expectUserRows(1);
// Mark the upload as completed. This should trigger a write_checkpoint.json request
resolveUploadPromise!();
await vi.waitFor(() => {
expect(remote.generateCheckpoint.mock.calls.length).equals(1);
});
// Completing the upload should also make the checkpoint visible without it being sent again.
await vi.waitFor(async () => {
await expectUserRows(2);
});
});
});
function describeStreamingTests(createConnectedDatabase: () => Promise<ConnectedDatabaseUtils>) {
return () => {
it('PowerSync reconnect on closed stream', async () => {
const { powersync, waitForStream, remote } = await createConnectedDatabase();
expect(powersync.connected).toBe(true);
// Close the stream
const newStream = waitForStream();
remote.streamController?.close();
// A new stream should be requested
await newStream;
});
it('PowerSync reconnect multiple connect calls', async () => {
// This initially performs a connect call
const { powersync, remote } = await createConnectedDatabase();
expect(powersync.connected).toBe(true);
const spy = vi.spyOn(powersync as any, 'generateSyncStreamImplementation');
// Keep track of all connection streams to check if they are correctly closed later
const generatedStreams: DataStream<any>[] = [];
// This method is used for all mocked connections
const basePostStream = remote.postStream;
const postSpy = vi.spyOn(remote, 'postStream').mockImplementation(async (...options) => {
// Simulate a connection delay
await new Promise((r) => setTimeout(r, 100));
const stream = await basePostStream.call(remote, ...options);
generatedStreams.push(stream);
return stream;
});
// Connect many times. The calls here are not awaited and have no async calls in between.
const connectionAttempts = 10;
for (let i = 1; i <= connectionAttempts; i++) {
powersync.connect(new TestConnector(), { params: { count: i } });
}
await vi.waitFor(
() => {
const call = spy.mock.lastCall![1] as PowerSyncConnectionOptions;
expect(call.params!['count']).eq(connectionAttempts);
},
{ timeout: 2000, interval: 100 }
);
// In this case it should most likely be 1 attempt since all the calls
// are in the same for loop
expect(spy.mock.calls.length).lessThan(connectionAttempts);
// Now with random awaited delays between unawaited calls
for (let i = connectionAttempts; i >= 0; i--) {
await new Promise((r) => setTimeout(r, Math.random() * 10));
powersync.connect(new TestConnector(), { params: { count: i } });
}
await vi.waitFor(
() => {
const call = spy.mock.lastCall![1] as PowerSyncConnectionOptions;
expect(call.params!['count']).eq(0);
},
{ timeout: 8000, interval: 100 }
);
expect(
spy.mock.calls.length,
`Expected generated streams to be less than or equal to ${2 * connectionAttempts}, but got ${spy.mock.calls.length}`
).lessThanOrEqual(2 * connectionAttempts);
// The last request should make a network request with the client params
await vi.waitFor(
() => {
expect(postSpy.mock.lastCall?.[0].data.parameters!['count']).equals(0);
// The async postStream call's invocation is added to the count of calls
// before the generated stream is added (there is a delay)
// expect that the stream has been generated and tracked.
expect(postSpy.mock.calls.length).equals(generatedStreams.length);
},
{ timeout: 1000, interval: 100 }
);
const lastConnectionStream = generatedStreams.pop();
expect(lastConnectionStream).toBeDefined();
expect(lastConnectionStream?.closed).false;
// All streams except the last one (which has been popped off already) should be closed
expect(generatedStreams.every((i) => i.closed)).true;
});
it('Should trigger upload connector when connected', async () => {
const { powersync, uploadSpy } = await createConnectedDatabase();
expect(powersync.connected).toBe(true);
// do something which should trigger an upload
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
// It should try and upload
await vi.waitFor(
() => {
// to-have-been-called seems to not work after failing the first check
expect(uploadSpy.mock.calls.length).equals(1);
},
{
timeout: UPLOAD_TIMEOUT_MS,
interval: 500
}
);
});
it('Should retry failed uploads when connected', async () => {
const { powersync, uploadSpy } = await createConnectedDatabase();
expect(powersync.connected).toBe(true);
let uploadCounter = 0;
// This test will throw an exception a few times before uploading
const throwCounter = 2;
uploadSpy.mockImplementation(async (db) => {
if (uploadCounter++ < throwCounter) {
throw new Error(`No uploads yet`);
}
// Now actually do the upload
const tx = await db.getNextCrudTransaction();
await tx?.complete();
});
// do something which should trigger an upload
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
// It should try and upload
await vi.waitFor(
() => {
// to-have-been-called seems to not work after failing a check
expect(uploadSpy.mock.calls.length).equals(throwCounter + 1);
},
{
timeout: UPLOAD_TIMEOUT_MS,
interval: 500
}
);
});
};
}