Skip to content

Commit 07466dc

Browse files
committed
Avoid running stream tests twice
1 parent 4c24289 commit 07466dc

File tree

4 files changed

+104
-96
lines changed

4 files changed

+104
-96
lines changed

packages/common/src/client/sync/bucket/SqliteBucketStorage.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,18 @@ export class SqliteBucketStorage extends BaseObserver<BucketStorageListener> imp
145145
return { ready: false, checkpointValid: false, checkpointFailures: r.checkpointFailures };
146146
}
147147

148-
const bucketNames = checkpoint.buckets.map((b) => b.bucket);
148+
const buckets = checkpoint.buckets;
149+
if (priority !== undefined) {
150+
buckets.filter((b) => b.priority <= priority);
151+
}
152+
const bucketNames = buckets.map((b) => b.bucket);
149153
await this.writeTransaction(async (tx) => {
150154
await tx.execute(`UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))`, [
151155
checkpoint.last_op_id,
152156
JSON.stringify(bucketNames)
153157
]);
154158

155-
if (checkpoint.write_checkpoint) {
159+
if (priority == null && checkpoint.write_checkpoint) {
156160
await tx.execute("UPDATE ps_buckets SET last_op = ? WHERE name = '$local'", [checkpoint.write_checkpoint]);
157161
}
158162
});

packages/web/tests/stream.test.ts

Lines changed: 3 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,11 @@
1-
import { BucketChecksum, BucketDescription, Schema, Table, column } from '@powersync/common';
2-
import { WebPowerSyncOpenFactoryOptions } from '@powersync/web';
1+
import { BucketChecksum } from '@powersync/common';
32
import Logger from 'js-logger';
4-
import { v4 as uuid } from 'uuid';
53
import { beforeAll, describe, expect, it, onTestFinished, vi } from 'vitest';
6-
import { MockRemote, MockStreamOpenFactory, TestConnector } from './utils/MockStreamOpenFactory';
7-
8-
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
9-
10-
export type ConnectedDatabaseUtils = UnwrapPromise<ReturnType<typeof generateConnectedDatabase>>;
11-
export type GenerateConnectedDatabaseOptions = {
12-
powerSyncOptions: Partial<WebPowerSyncOpenFactoryOptions>;
13-
};
4+
import { TestConnector } from './utils/MockStreamOpenFactory';
5+
import { generateConnectedDatabase } from './utils/generateConnectedDatabase';
146

157
const UPLOAD_TIMEOUT_MS = 3000;
168

17-
export const DEFAULT_CONNECTED_POWERSYNC_OPTIONS = {
18-
powerSyncOptions: {
19-
dbFilename: 'test-stream-connection.db',
20-
flags: {
21-
enableMultiTabs: false,
22-
useWebWorker: true
23-
},
24-
// Makes tests faster
25-
crudUploadThrottleMs: 0,
26-
schema: new Schema({
27-
users: new Table({ name: column.text })
28-
})
29-
}
30-
};
31-
32-
export async function generateConnectedDatabase(
33-
options: GenerateConnectedDatabaseOptions = DEFAULT_CONNECTED_POWERSYNC_OPTIONS
34-
) {
35-
const { powerSyncOptions } = options;
36-
const { powerSyncOptions: defaultPowerSyncOptions } = DEFAULT_CONNECTED_POWERSYNC_OPTIONS;
37-
/**
38-
* Very basic implementation of a listener pattern.
39-
* Required since we cannot extend multiple classes.
40-
*/
41-
const callbacks: Map<string, () => void> = new Map();
42-
const connector = new TestConnector();
43-
const uploadSpy = vi.spyOn(connector, 'uploadData');
44-
const remote = new MockRemote(connector, () => callbacks.forEach((c) => c()));
45-
46-
const factory = new MockStreamOpenFactory(
47-
{
48-
...defaultPowerSyncOptions,
49-
...powerSyncOptions,
50-
flags: {
51-
...(defaultPowerSyncOptions.flags ?? {}),
52-
...(powerSyncOptions.flags ?? {})
53-
}
54-
},
55-
remote
56-
);
57-
const powersync = factory.getInstance();
58-
59-
const waitForStream = () =>
60-
new Promise<void>((resolve) => {
61-
const id = uuid();
62-
callbacks.set(id, () => {
63-
resolve();
64-
callbacks.delete(id);
65-
});
66-
});
67-
68-
const connect = async () => {
69-
const streamOpened = waitForStream();
70-
71-
const connectedPromise = powersync.connect(connector);
72-
73-
await streamOpened;
74-
75-
remote.enqueueLine({token_expires_in: 3426});
76-
77-
// Wait for connected to be true
78-
await connectedPromise;
79-
};
80-
81-
await connect();
82-
83-
onTestFinished(async () => {
84-
await powersync.disconnectAndClear();
85-
await powersync.close();
86-
});
87-
88-
return {
89-
connector,
90-
connect,
91-
factory,
92-
powersync,
93-
remote,
94-
uploadSpy,
95-
waitForStream
96-
};
97-
}
98-
999
describe('Streaming', () => {
10010
/**
10111
* Declares a test to be executed with different generated db functions

packages/web/tests/uploads.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Logger from 'js-logger';
22
import p from 'p-defer';
33
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
4-
import { ConnectedDatabaseUtils, generateConnectedDatabase } from './stream.test';
4+
import { ConnectedDatabaseUtils, generateConnectedDatabase } from './utils/generateConnectedDatabase';
55

66
// Don't want to actually export the warning string from the package
77
const PARTIAL_WARNING = 'Potentially previously uploaded CRUD entries are still present';
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { BucketChecksum, BucketDescription, Schema, Table, column } from '@powersync/common';
2+
import { WebPowerSyncOpenFactoryOptions } from '@powersync/web';
3+
import { v4 as uuid } from 'uuid';
4+
import { onTestFinished, vi } from 'vitest';
5+
import { MockRemote, MockStreamOpenFactory, TestConnector } from './MockStreamOpenFactory';
6+
7+
type UnwrapPromise<T> = T extends Promise<infer U> ? U : T;
8+
9+
export type ConnectedDatabaseUtils = UnwrapPromise<ReturnType<typeof generateConnectedDatabase>>;
10+
export type GenerateConnectedDatabaseOptions = {
11+
powerSyncOptions: Partial<WebPowerSyncOpenFactoryOptions>;
12+
};
13+
14+
export const DEFAULT_CONNECTED_POWERSYNC_OPTIONS = {
15+
powerSyncOptions: {
16+
dbFilename: 'test-stream-connection.db',
17+
flags: {
18+
enableMultiTabs: false,
19+
useWebWorker: true
20+
},
21+
// Makes tests faster
22+
crudUploadThrottleMs: 0,
23+
schema: new Schema({
24+
users: new Table({ name: column.text })
25+
})
26+
}
27+
};
28+
29+
export async function generateConnectedDatabase(
30+
options: GenerateConnectedDatabaseOptions = DEFAULT_CONNECTED_POWERSYNC_OPTIONS
31+
) {
32+
const { powerSyncOptions } = options;
33+
const { powerSyncOptions: defaultPowerSyncOptions } = DEFAULT_CONNECTED_POWERSYNC_OPTIONS;
34+
/**
35+
* Very basic implementation of a listener pattern.
36+
* Required since we cannot extend multiple classes.
37+
*/
38+
const callbacks: Map<string, () => void> = new Map();
39+
const connector = new TestConnector();
40+
const uploadSpy = vi.spyOn(connector, 'uploadData');
41+
const remote = new MockRemote(connector, () => callbacks.forEach((c) => c()));
42+
43+
const factory = new MockStreamOpenFactory(
44+
{
45+
...defaultPowerSyncOptions,
46+
...powerSyncOptions,
47+
flags: {
48+
...(defaultPowerSyncOptions.flags ?? {}),
49+
...(powerSyncOptions.flags ?? {})
50+
}
51+
},
52+
remote
53+
);
54+
const powersync = factory.getInstance();
55+
56+
const waitForStream = () =>
57+
new Promise<void>((resolve) => {
58+
const id = uuid();
59+
callbacks.set(id, () => {
60+
resolve();
61+
callbacks.delete(id);
62+
});
63+
});
64+
65+
const connect = async () => {
66+
const streamOpened = waitForStream();
67+
68+
const connectedPromise = powersync.connect(connector);
69+
70+
await streamOpened;
71+
72+
remote.enqueueLine({token_expires_in: 3426});
73+
74+
// Wait for connected to be true
75+
await connectedPromise;
76+
};
77+
78+
await connect();
79+
80+
onTestFinished(async () => {
81+
await powersync.disconnectAndClear();
82+
await powersync.close();
83+
});
84+
85+
return {
86+
connector,
87+
connect,
88+
factory,
89+
powersync,
90+
remote,
91+
uploadSpy,
92+
waitForStream
93+
};
94+
}

0 commit comments

Comments
 (0)