Skip to content

Commit f2ad5a7

Browse files
restore sequence
1 parent 1a1d373 commit f2ad5a7

File tree

1 file changed

+169
-163
lines changed

1 file changed

+169
-163
lines changed

packages/web/tests/stream.test.ts

Lines changed: 169 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -5,177 +5,183 @@ import { ConnectedDatabaseUtils, generateConnectedDatabase } from './utils/gener
55

66
const UPLOAD_TIMEOUT_MS = 3000;
77

8-
describe('Streaming', () => {
9-
/**
10-
* Declares a test to be executed with different generated db functions
11-
*/
12-
const itWithGenerators = (
13-
name: string,
14-
test: (createConnectedDatabase: () => ReturnType<typeof generateConnectedDatabase>) => Promise<void>
15-
) => {
16-
const funcWithWebWorker = generateConnectedDatabase;
17-
const funcWithoutWebWorker = () =>
18-
generateConnectedDatabase({
19-
powerSyncOptions: {
20-
dbFilename: 'test-stream-connection-no-worker.db',
21-
flags: {
22-
useWebWorker: false
8+
describe(
9+
'Streaming',
10+
{
11+
sequential: true
12+
},
13+
() => {
14+
/**
15+
* Declares a test to be executed with different generated db functions
16+
*/
17+
const itWithGenerators = (
18+
name: string,
19+
test: (createConnectedDatabase: () => ReturnType<typeof generateConnectedDatabase>) => Promise<void>
20+
) => {
21+
const funcWithWebWorker = generateConnectedDatabase;
22+
const funcWithoutWebWorker = () =>
23+
generateConnectedDatabase({
24+
powerSyncOptions: {
25+
dbFilename: 'test-stream-connection-no-worker.db',
26+
flags: {
27+
useWebWorker: false
28+
}
2329
}
24-
}
25-
});
30+
});
31+
32+
it(`${name} - with web worker`, () => test(funcWithWebWorker));
33+
it(`${name} - without web worker`, () => test(funcWithoutWebWorker));
34+
};
35+
36+
beforeAll(() => Logger.useDefaults());
37+
38+
let connectionUtilities: ConnectedDatabaseUtils | null = null;
39+
40+
afterEach(async () => {
41+
await connectionUtilities?.dispose();
42+
connectionUtilities = null;
43+
});
44+
45+
itWithGenerators('PowerSync reconnect on closed stream', async (createConnectedDatabase) => {
46+
connectionUtilities = await createConnectedDatabase();
47+
const { powersync, waitForStream, remote } = connectionUtilities;
48+
49+
expect(powersync.connected).toBe(true);
50+
51+
// Close the stream
52+
const newStream = waitForStream();
53+
remote.streamController?.close();
2654

27-
it(`${name} - with web worker`, () => test(funcWithWebWorker));
28-
it(`${name} - without web worker`, () => test(funcWithoutWebWorker));
29-
};
30-
31-
beforeAll(() => Logger.useDefaults());
32-
33-
let connectionUtilities: ConnectedDatabaseUtils | null = null;
34-
35-
afterEach(async () => {
36-
await connectionUtilities?.dispose();
37-
connectionUtilities = null;
38-
});
39-
40-
itWithGenerators('PowerSync reconnect on closed stream', async (createConnectedDatabase) => {
41-
connectionUtilities = await createConnectedDatabase();
42-
const { powersync, waitForStream, remote } = connectionUtilities;
43-
44-
expect(powersync.connected).toBe(true);
45-
46-
// Close the stream
47-
const newStream = waitForStream();
48-
remote.streamController?.close();
49-
50-
// A new stream should be requested
51-
await newStream;
52-
});
53-
54-
itWithGenerators('PowerSync reconnect multiple connect calls', async (createConnectedDatabase) => {
55-
// This initially performs a connect call
56-
connectionUtilities = await createConnectedDatabase();
57-
const { powersync, waitForStream, remote } = connectionUtilities;
58-
expect(powersync.connected).toBe(true);
59-
60-
// Call connect again, a new stream should be requested
61-
const newStream = waitForStream();
62-
powersync.connect(new TestConnector());
63-
64-
// A new stream should be requested
65-
await newStream;
66-
});
67-
68-
itWithGenerators('Should trigger upload connector when connected', async (createConnectedDatabase) => {
69-
connectionUtilities = await createConnectedDatabase();
70-
const { powersync, uploadSpy } = connectionUtilities;
71-
expect(powersync.connected).toBe(true);
72-
73-
// do something which should trigger an upload
74-
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
75-
// It should try and upload
76-
await vi.waitFor(
77-
() => {
78-
// to-have-been-called seems to not work after failing the first check
79-
expect(uploadSpy.mock.calls.length).equals(1);
80-
},
81-
{
82-
timeout: UPLOAD_TIMEOUT_MS,
83-
interval: 500
84-
}
85-
);
86-
});
87-
88-
itWithGenerators('Should retry failed uploads when connected', async (createConnectedDatabase) => {
89-
connectionUtilities = await createConnectedDatabase();
90-
const { powersync, uploadSpy } = connectionUtilities;
91-
92-
expect(powersync.connected).toBe(true);
93-
94-
let uploadCounter = 0;
95-
// This test will throw an exception a few times before uploading
96-
const throwCounter = 2;
97-
uploadSpy.mockImplementation(async (db) => {
98-
if (uploadCounter++ < throwCounter) {
99-
throw new Error('No uploads yet');
100-
}
101-
// Now actually do the upload
102-
const tx = await db.getNextCrudTransaction();
103-
await tx?.complete();
55+
// A new stream should be requested
56+
await newStream;
10457
});
10558

106-
// do something which should trigger an upload
107-
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
108-
109-
// It should try and upload
110-
await vi.waitFor(
111-
() => {
112-
// to-have-been-called seems to not work after failing a check
113-
expect(uploadSpy.mock.calls.length).equals(throwCounter + 1);
114-
},
115-
{
116-
timeout: UPLOAD_TIMEOUT_MS,
117-
interval: 500
118-
}
119-
);
120-
});
121-
122-
itWithGenerators('Should upload after reconnecting', async (createConnectedDatabase) => {
123-
connectionUtilities = await createConnectedDatabase();
124-
const { powersync, connect, uploadSpy } = connectionUtilities;
125-
expect(powersync.connected).toBe(true);
126-
127-
await powersync.disconnect();
128-
129-
// do something (offline) which should trigger an upload
130-
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
131-
132-
await connect();
133-
134-
// It should try and upload
135-
await vi.waitFor(
136-
() => {
137-
// to-have-been-called seems to not work after failing a check
138-
expect(uploadSpy.mock.calls.length).equals(1);
139-
},
140-
{
141-
timeout: UPLOAD_TIMEOUT_MS,
142-
interval: 500
143-
}
144-
);
145-
});
146-
147-
itWithGenerators('Should update status when uploading', async (createConnectedDatabase) => {
148-
connectionUtilities = await createConnectedDatabase();
149-
const { powersync, uploadSpy } = connectionUtilities;
150-
151-
expect(powersync.connected).toBe(true);
152-
153-
let uploadStartedPromise = new Promise<void>((resolve) => {
59+
itWithGenerators('PowerSync reconnect multiple connect calls', async (createConnectedDatabase) => {
60+
// This initially performs a connect call
61+
connectionUtilities = await createConnectedDatabase();
62+
const { powersync, waitForStream, remote } = connectionUtilities;
63+
expect(powersync.connected).toBe(true);
64+
65+
// Call connect again, a new stream should be requested
66+
const newStream = waitForStream();
67+
powersync.connect(new TestConnector());
68+
69+
// A new stream should be requested
70+
await newStream;
71+
});
72+
73+
itWithGenerators('Should trigger upload connector when connected', async (createConnectedDatabase) => {
74+
connectionUtilities = await createConnectedDatabase();
75+
const { powersync, uploadSpy } = connectionUtilities;
76+
expect(powersync.connected).toBe(true);
77+
78+
// do something which should trigger an upload
79+
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
80+
// It should try and upload
81+
await vi.waitFor(
82+
() => {
83+
// to-have-been-called seems to not work after failing the first check
84+
expect(uploadSpy.mock.calls.length).equals(1);
85+
},
86+
{
87+
timeout: UPLOAD_TIMEOUT_MS,
88+
interval: 500
89+
}
90+
);
91+
});
92+
93+
itWithGenerators('Should retry failed uploads when connected', async (createConnectedDatabase) => {
94+
connectionUtilities = await createConnectedDatabase();
95+
const { powersync, uploadSpy } = connectionUtilities;
96+
97+
expect(powersync.connected).toBe(true);
98+
99+
let uploadCounter = 0;
100+
// This test will throw an exception a few times before uploading
101+
const throwCounter = 2;
154102
uploadSpy.mockImplementation(async (db) => {
155-
resolve();
103+
if (uploadCounter++ < throwCounter) {
104+
throw new Error('No uploads yet');
105+
}
156106
// Now actually do the upload
157107
const tx = await db.getNextCrudTransaction();
158108
await tx?.complete();
159109
});
110+
111+
// do something which should trigger an upload
112+
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
113+
114+
// It should try and upload
115+
await vi.waitFor(
116+
() => {
117+
// to-have-been-called seems to not work after failing a check
118+
expect(uploadSpy.mock.calls.length).equals(throwCounter + 1);
119+
},
120+
{
121+
timeout: UPLOAD_TIMEOUT_MS,
122+
interval: 500
123+
}
124+
);
125+
});
126+
127+
itWithGenerators('Should upload after reconnecting', async (createConnectedDatabase) => {
128+
connectionUtilities = await createConnectedDatabase();
129+
const { powersync, connect, uploadSpy } = connectionUtilities;
130+
expect(powersync.connected).toBe(true);
131+
132+
await powersync.disconnect();
133+
134+
// do something (offline) which should trigger an upload
135+
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
136+
137+
await connect();
138+
139+
// It should try and upload
140+
await vi.waitFor(
141+
() => {
142+
// to-have-been-called seems to not work after failing a check
143+
expect(uploadSpy.mock.calls.length).equals(1);
144+
},
145+
{
146+
timeout: UPLOAD_TIMEOUT_MS,
147+
interval: 500
148+
}
149+
);
160150
});
161151

162-
// do something which should trigger an upload
163-
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
164-
165-
await uploadStartedPromise;
166-
167-
expect(powersync.currentStatus.dataFlowStatus.uploading).true;
168-
169-
// Status should update after uploads are completed
170-
await vi.waitFor(
171-
() => {
172-
// to-have-been-called seems to not work after failing a check
173-
expect(powersync.currentStatus.dataFlowStatus.uploading).false;
174-
},
175-
{
176-
timeout: UPLOAD_TIMEOUT_MS,
177-
interval: 500
178-
}
179-
);
180-
});
181-
});
152+
itWithGenerators('Should update status when uploading', async (createConnectedDatabase) => {
153+
connectionUtilities = await createConnectedDatabase();
154+
const { powersync, uploadSpy } = connectionUtilities;
155+
156+
expect(powersync.connected).toBe(true);
157+
158+
let uploadStartedPromise = new Promise<void>((resolve) => {
159+
uploadSpy.mockImplementation(async (db) => {
160+
resolve();
161+
// Now actually do the upload
162+
const tx = await db.getNextCrudTransaction();
163+
await tx?.complete();
164+
});
165+
});
166+
167+
// do something which should trigger an upload
168+
await powersync.execute('INSERT INTO users (id, name) VALUES (uuid(), ?)', ['name']);
169+
170+
await uploadStartedPromise;
171+
172+
expect(powersync.currentStatus.dataFlowStatus.uploading).true;
173+
174+
// Status should update after uploads are completed
175+
await vi.waitFor(
176+
() => {
177+
// to-have-been-called seems to not work after failing a check
178+
expect(powersync.currentStatus.dataFlowStatus.uploading).false;
179+
},
180+
{
181+
timeout: UPLOAD_TIMEOUT_MS,
182+
interval: 500
183+
}
184+
);
185+
});
186+
}
187+
);

0 commit comments

Comments
 (0)