Skip to content

Commit f433195

Browse files
committed
Fix tests.
1 parent d279db1 commit f433195

File tree

9 files changed

+78
-104
lines changed

9 files changed

+78
-104
lines changed

modules/module-postgres/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"build": "tsc -b",
1414
"build:tests": "tsc -b test/tsconfig.json",
1515
"clean": "rm -rf ./lib && tsc -b --clean",
16-
"test": "vitest --no-threads"
16+
"test": "vitest"
1717
},
1818
"exports": {
1919
".": {

modules/module-postgres/src/utils/populate_test_data.ts

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import * as crypto from 'crypto';
2-
import { Worker, isMainThread, parentPort, workerData } from 'node:worker_threads';
1+
import { Worker } from 'node:worker_threads';
32

43
import * as pgwire from '@powersync/service-jpgwire';
54

@@ -12,49 +11,10 @@ export interface PopulateDataOptions {
1211
size: number;
1312
}
1413

15-
if (isMainThread || parentPort == null) {
16-
// Not a worker - ignore
17-
} else {
18-
try {
19-
const options = workerData as PopulateDataOptions;
20-
21-
const result = await populateDataInner(options);
22-
parentPort.postMessage(result);
23-
process.exit(0);
24-
} catch (e) {
25-
// This is a bug, not a connection issue
26-
console.error(e);
27-
// Only closes the Worker thread
28-
process.exit(2);
29-
}
30-
}
31-
32-
async function populateDataInner(options: PopulateDataOptions) {
33-
// Dedicated connection so we can release the memory easily
34-
const initialDb = await pgwire.connectPgWire(options.connection, { type: 'standard' });
35-
const largeDescription = crypto.randomBytes(options.size / 2).toString('hex');
36-
let operation_count = 0;
37-
for (let i = 0; i < options.num_transactions; i++) {
38-
const prefix = `test${i}K`;
39-
40-
await initialDb.query({
41-
statement: `INSERT INTO test_data(id, description, other) SELECT $1 || i, $2, 'foo' FROM generate_series(1, $3) i`,
42-
params: [
43-
{ type: 'varchar', value: prefix },
44-
{ type: 'varchar', value: largeDescription },
45-
{ type: 'int4', value: options.per_transaction }
46-
]
47-
});
48-
operation_count += options.per_transaction;
49-
}
50-
await initialDb.end();
51-
return operation_count;
52-
}
53-
5414
export async function populateData(options: PopulateDataOptions) {
5515
const WORKER_TIMEOUT = 30_000;
5616

57-
const worker = new Worker(new URL('./populate_test_data.js', import.meta.url), {
17+
const worker = new Worker(new URL('./populate_test_data_worker.js', import.meta.url), {
5818
workerData: options
5919
});
6020
const timeout = setTimeout(() => {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as crypto from 'crypto';
2+
import { isMainThread, parentPort, workerData } from 'node:worker_threads';
3+
4+
import * as pgwire from '@powersync/service-jpgwire';
5+
import type { PopulateDataOptions } from './populate_test_data.js';
6+
7+
// This util is actually for tests only, but we need it compiled to JS for the service to work, so it's placed in the service.
8+
9+
if (isMainThread || parentPort == null) {
10+
// Must not be imported - only expected to run in a worker
11+
throw new Error('Do not import this file');
12+
} else {
13+
try {
14+
const options = workerData as PopulateDataOptions;
15+
if (options == null) {
16+
throw new Error('loaded worker without options');
17+
}
18+
19+
const result = await populateDataInner(options);
20+
parentPort.postMessage(result);
21+
process.exit(0);
22+
} catch (e) {
23+
// This is a bug, not a connection issue
24+
console.error(e);
25+
// Only closes the Worker thread
26+
process.exit(2);
27+
}
28+
}
29+
30+
async function populateDataInner(options: PopulateDataOptions) {
31+
// Dedicated connection so we can release the memory easily
32+
const initialDb = await pgwire.connectPgWire(options.connection, { type: 'standard' });
33+
const largeDescription = crypto.randomBytes(options.size / 2).toString('hex');
34+
let operation_count = 0;
35+
for (let i = 0; i < options.num_transactions; i++) {
36+
const prefix = `test${i}K`;
37+
38+
await initialDb.query({
39+
statement: `INSERT INTO test_data(id, description, other) SELECT $1 || i, $2, 'foo' FROM generate_series(1, $3) i`,
40+
params: [
41+
{ type: 'varchar', value: prefix },
42+
{ type: 'varchar', value: largeDescription },
43+
{ type: 'int4', value: options.per_transaction }
44+
]
45+
});
46+
operation_count += options.per_transaction;
47+
}
48+
await initialDb.end();
49+
return operation_count;
50+
}

modules/module-postgres/vitest.config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import tsconfigPaths from 'vite-tsconfig-paths';
44
export default defineConfig({
55
plugins: [tsconfigPaths()],
66
test: {
7-
setupFiles: './test/src/setup.ts'
7+
setupFiles: './test/src/setup.ts',
8+
poolOptions: {
9+
threads: {
10+
singleThread: true
11+
}
12+
},
13+
pool: 'threads'
814
}
915
});

packages/service-core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"scripts": {
1313
"build": "tsc -b",
1414
"build:tests": "tsc -b test/tsconfig.json",
15-
"test": "vitest --no-threads",
15+
"test": "vitest",
1616
"clean": "rm -rf ./lib && tsc -b --clean"
1717
},
1818
"dependencies": {

packages/service-core/test/src/__snapshots__/sync.test.ts.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ exports[`sync - mongodb > compacting data - invalidate checkpoint 2`] = `
5656
"data": [
5757
{
5858
"checksum": 1859363232n,
59-
"data": "{\\"id\\":\\"t1\\",\\"description\\":\\"Test 1b\\"}",
59+
"data": "{"id":"t1","description":"Test 1b"}",
6060
"object_id": "t1",
6161
"object_type": "test",
6262
"op": "PUT",
@@ -65,7 +65,7 @@ exports[`sync - mongodb > compacting data - invalidate checkpoint 2`] = `
6565
},
6666
{
6767
"checksum": 3028503153n,
68-
"data": "{\\"id\\":\\"t2\\",\\"description\\":\\"Test 2b\\"}",
68+
"data": "{"id":"t2","description":"Test 2b"}",
6969
"object_id": "t2",
7070
"object_type": "test",
7171
"op": "PUT",
@@ -146,7 +146,7 @@ exports[`sync - mongodb > sync global data 1`] = `
146146
"data": [
147147
{
148148
"checksum": 920318466n,
149-
"data": "{\\"id\\":\\"t1\\",\\"description\\":\\"Test 1\\"}",
149+
"data": "{"id":"t1","description":"Test 1"}",
150150
"object_id": "t1",
151151
"object_type": "test",
152152
"op": "PUT",
@@ -155,7 +155,7 @@ exports[`sync - mongodb > sync global data 1`] = `
155155
},
156156
{
157157
"checksum": 3280762209n,
158-
"data": "{\\"id\\":\\"t2\\",\\"description\\":\\"Test 2\\"}",
158+
"data": "{"id":"t2","description":"Test 2"}",
159159
"object_id": "t2",
160160
"object_type": "test",
161161
"op": "PUT",
@@ -199,7 +199,7 @@ exports[`sync - mongodb > sync legacy non-raw data 1`] = `
199199
"checksum": 3442149460n,
200200
"data": {
201201
"description": "Test
202-
\\"string\\"",
202+
"string"",
203203
"id": "t1",
204204
"large_num": 12345678901234567890n,
205205
},
@@ -268,7 +268,7 @@ exports[`sync - mongodb > sync updates to global data 2`] = `
268268
"data": [
269269
{
270270
"checksum": 920318466n,
271-
"data": "{\\"id\\":\\"t1\\",\\"description\\":\\"Test 1\\"}",
271+
"data": "{"id":"t1","description":"Test 1"}",
272272
"object_id": "t1",
273273
"object_type": "test",
274274
"op": "PUT",
@@ -311,7 +311,7 @@ exports[`sync - mongodb > sync updates to global data 3`] = `
311311
"data": [
312312
{
313313
"checksum": 3280762209n,
314-
"data": "{\\"id\\":\\"t2\\",\\"description\\":\\"Test 2\\"}",
314+
"data": "{"id":"t2","description":"Test 2"}",
315315
"object_id": "t2",
316316
"object_type": "test",
317317
"op": "PUT",

packages/service-core/vitest.config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import tsconfigPaths from 'vite-tsconfig-paths';
44
export default defineConfig({
55
plugins: [tsconfigPaths()],
66
test: {
7-
setupFiles: './test/src/setup.ts'
7+
setupFiles: './test/src/setup.ts',
8+
poolOptions: {
9+
threads: {
10+
singleThread: true
11+
}
12+
},
13+
pool: 'threads'
814
}
915
});

pnpm-lock.yaml

Lines changed: 2 additions & 50 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"yaml": "^2.5.0"
2121
},
2222
"devDependencies": {
23-
"@types/node": "18.11.11",
23+
"@types/node": "^22.5.5",
2424
"typescript": "^5.6.2"
2525
}
2626
}

0 commit comments

Comments
 (0)