Skip to content

Commit 4ab4112

Browse files
committed
Updated tests
Added a MetricsHelper to aid with test setup for metrics
1 parent 437bd7a commit 4ab4112

File tree

16 files changed

+168
-67
lines changed

16 files changed

+168
-67
lines changed
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { container } from '@powersync/lib-services-framework';
2-
import { test_utils } from '@powersync/service-core-tests';
32
import { beforeAll, beforeEach } from 'vitest';
3+
import { METRICS_HELPER } from '@powersync/service-core-tests';
44

55
beforeAll(async () => {
66
// Executes for every test file
77
container.registerDefaults();
8-
await test_utils.initMetrics();
98
});
109

1110
beforeEach(async () => {
12-
await test_utils.resetMetrics();
11+
METRICS_HELPER.resetMetrics();
1312
});

modules/module-mongodb/test/src/change_stream_utils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import { mongo } from '@powersync/lib-service-mongodb';
2-
import { ActiveCheckpoint, BucketStorageFactory, OpId, SyncRulesBucketStorage } from '@powersync/service-core';
3-
import { test_utils } from '@powersync/service-core-tests';
2+
import {
3+
ActiveCheckpoint,
4+
BucketStorageFactory,
5+
createCoreReplicationMetrics,
6+
initializeCoreReplicationMetrics,
7+
OpId,
8+
SyncRulesBucketStorage
9+
} from '@powersync/service-core';
10+
import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
411

512
import { ChangeStream, ChangeStreamOptions } from '@module/replication/ChangeStream.js';
613
import { MongoManager } from '@module/replication/MongoManager.js';
@@ -32,7 +39,10 @@ export class ChangeStreamTestContext {
3239
constructor(
3340
public factory: BucketStorageFactory,
3441
public connectionManager: MongoManager
35-
) {}
42+
) {
43+
createCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
44+
initializeCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
45+
}
3646

3747
async dispose() {
3848
this.abortController.abort();
@@ -72,6 +82,7 @@ export class ChangeStreamTestContext {
7282
}
7383
const options: ChangeStreamOptions = {
7484
storage: this.storage,
85+
metrics: METRICS_HELPER.metricsEngine,
7586
connections: this.connectionManager,
7687
abort_signal: this.abortController.signal
7788
};

modules/module-mongodb/test/src/env.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export const env = utils.collectEnvironmentVariables({
77
CI: utils.type.boolean.default('false'),
88
SLOW_TESTS: utils.type.boolean.default('false'),
99
TEST_MONGO_STORAGE: utils.type.boolean.default('true'),
10-
TEST_POSTGRES_STORAGE: utils.type.boolean.default('true')
10+
TEST_POSTGRES_STORAGE: utils.type.boolean.default('false')
1111
});
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import { container } from '@powersync/lib-services-framework';
2-
import { test_utils } from '@powersync/service-core-tests';
2+
import { METRICS_HELPER } from '@powersync/service-core-tests';
33
import { beforeEach } from 'node:test';
44
import { beforeAll } from 'vitest';
55

66
beforeAll(async () => {
77
// Executes for every test file
88
container.registerDefaults();
9-
10-
await test_utils.initMetrics();
119
});
1210

1311
beforeEach(async () => {
14-
await test_utils.resetMetrics();
12+
METRICS_HELPER.resetMetrics();
1513
});

modules/module-mysql/test/src/BinLogStream.test.ts

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Metrics, storage } from '@powersync/service-core';
2-
import { putOp, removeOp } from '@powersync/service-core-tests';
1+
import { ReplicationMetricType, storage } from '@powersync/service-core';
2+
import { METRICS_HELPER, putOp, removeOp } from '@powersync/service-core-tests';
33
import { v4 as uuid } from 'uuid';
44
import { describe, expect, test } from 'vitest';
55
import { BinlogStreamTestContext } from './BinlogStreamUtils.js';
@@ -35,9 +35,10 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
3535

3636
await context.replicateSnapshot();
3737

38-
const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
38+
const startRowCount =
39+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
3940
const startTxCount =
40-
(await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
41+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.TRANSACTIONS_REPLICATED_TOTAL)) ?? 0;
4142

4243
context.startStreaming();
4344
const testId = uuid();
@@ -47,9 +48,9 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
4748
const data = await context.getBucketData('global[]');
4849

4950
expect(data).toMatchObject([putOp('test_data', { id: testId, description: 'test1', num: 1152921504606846976n })]);
50-
const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
51+
const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
5152
const endTxCount =
52-
(await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
53+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.TRANSACTIONS_REPLICATED_TOTAL)) ?? 0;
5354
expect(endRowCount - startRowCount).toEqual(1);
5455
expect(endTxCount - startTxCount).toEqual(1);
5556
});
@@ -68,9 +69,10 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
6869

6970
await context.replicateSnapshot();
7071

71-
const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
72+
const startRowCount =
73+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
7274
const startTxCount =
73-
(await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
75+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.TRANSACTIONS_REPLICATED_TOTAL)) ?? 0;
7476

7577
context.startStreaming();
7678

@@ -80,9 +82,9 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
8082
const data = await context.getBucketData('global[]');
8183

8284
expect(data).toMatchObject([putOp('test_DATA', { id: testId, description: 'test1' })]);
83-
const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
85+
const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
8486
const endTxCount =
85-
(await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
87+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.TRANSACTIONS_REPLICATED_TOTAL)) ?? 0;
8688
expect(endRowCount - startRowCount).toEqual(1);
8789
expect(endTxCount - startTxCount).toEqual(1);
8890
});
@@ -172,10 +174,15 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
172174
const testId = uuid();
173175
await connectionManager.query(`INSERT INTO test_data(id, description) VALUES('${testId}','test1')`);
174176

177+
const startRowCount =
178+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
179+
175180
await context.replicateSnapshot();
176181

182+
const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
177183
const data = await context.getBucketData('global[]');
178184
expect(data).toMatchObject([putOp('test_data', { id: testId, description: 'test1' })]);
185+
expect(endRowCount - startRowCount).toEqual(1);
179186
});
180187

181188
test('snapshot with date values', async () => {
@@ -227,9 +234,10 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
227234

228235
await context.replicateSnapshot();
229236

230-
const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
237+
const startRowCount =
238+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
231239
const startTxCount =
232-
(await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
240+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.TRANSACTIONS_REPLICATED_TOTAL)) ?? 0;
233241

234242
context.startStreaming();
235243

@@ -256,9 +264,9 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
256264
timestamp: '2023-03-06T15:47:00.000Z'
257265
})
258266
]);
259-
const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
267+
const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
260268
const endTxCount =
261-
(await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
269+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.TRANSACTIONS_REPLICATED_TOTAL)) ?? 0;
262270
expect(endRowCount - startRowCount).toEqual(2);
263271
expect(endTxCount - startTxCount).toEqual(2);
264272
});
@@ -272,19 +280,20 @@ function defineBinlogStreamTests(factory: storage.TestStorageFactory) {
272280

273281
await context.replicateSnapshot();
274282

275-
const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
283+
const startRowCount =
284+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
276285
const startTxCount =
277-
(await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
286+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.TRANSACTIONS_REPLICATED_TOTAL)) ?? 0;
278287

279288
context.startStreaming();
280289

281290
await connectionManager.query(`INSERT INTO test_donotsync(id, description) VALUES('${uuid()}','test1')`);
282291
const data = await context.getBucketData('global[]');
283292

284293
expect(data).toMatchObject([]);
285-
const endRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
294+
const endRowCount = (await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
286295
const endTxCount =
287-
(await Metrics.getInstance().getMetricValueForTests('powersync_transactions_replicated_total')) ?? 0;
296+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.TRANSACTIONS_REPLICATED_TOTAL)) ?? 0;
288297

289298
// There was a transaction, but we should not replicate any actual data
290299
expect(endRowCount - startRowCount).toEqual(0);

modules/module-mysql/test/src/BinlogStreamUtils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { logger } from '@powersync/lib-services-framework';
55
import {
66
ActiveCheckpoint,
77
BucketStorageFactory,
8+
createCoreReplicationMetrics,
9+
initializeCoreReplicationMetrics,
810
OpId,
911
OplogEntry,
1012
storage,
1113
SyncRulesBucketStorage
1214
} from '@powersync/service-core';
13-
import { test_utils } from '@powersync/service-core-tests';
15+
import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
1416
import mysqlPromise from 'mysql2/promise';
1517
import { clearTestDb, TEST_CONNECTION_OPTIONS } from './util.js';
1618

@@ -43,7 +45,10 @@ export class BinlogStreamTestContext {
4345
constructor(
4446
public factory: BucketStorageFactory,
4547
public connectionManager: MySQLConnectionManager
46-
) {}
48+
) {
49+
createCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
50+
initializeCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
51+
}
4752

4853
async dispose() {
4954
this.abortController.abort();
@@ -96,6 +101,7 @@ export class BinlogStreamTestContext {
96101
}
97102
const options: BinLogStreamOptions = {
98103
storage: this.storage,
104+
metrics: METRICS_HELPER.metricsEngine,
99105
connections: this.connectionManager,
100106
abortSignal: this.abortController.signal
101107
};
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { container } from '@powersync/lib-services-framework';
2-
import { test_utils } from '@powersync/service-core-tests';
2+
import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
33
import { beforeAll, beforeEach } from 'vitest';
44

55
beforeAll(async () => {
66
// Executes for every test file
77
container.registerDefaults();
8-
await test_utils.initMetrics();
98
});
109

1110
beforeEach(async () => {
12-
await test_utils.resetMetrics();
11+
METRICS_HELPER.resetMetrics();
1312
});

modules/module-postgres/test/src/large_batch.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Metrics, storage } from '@powersync/service-core';
1+
import { ReplicationMetricType, storage } from '@powersync/service-core';
22
import * as timers from 'timers/promises';
33
import { describe, expect, test } from 'vitest';
44
import { populateData } from '../../dist/utils/populate_test_data.js';
@@ -9,6 +9,7 @@ import {
99
TEST_CONNECTION_OPTIONS
1010
} from './util.js';
1111
import { WalStreamTestContext } from './wal_stream_utils.js';
12+
import { METRICS_HELPER } from '@powersync/service-core-tests';
1213

1314
describe.skipIf(!env.TEST_MONGO_STORAGE)('batch replication tests - mongodb', { timeout: 120_000 }, function () {
1415
// These are slow but consistent tests.
@@ -301,12 +302,13 @@ function defineBatchTests(factory: storage.TestStorageFactory) {
301302

302303
let done = false;
303304

304-
const startRowCount = (await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0;
305+
const startRowCount =
306+
(await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0;
305307
try {
306308
(async () => {
307309
while (!done) {
308310
const count =
309-
((await Metrics.getInstance().getMetricValueForTests('powersync_rows_replicated_total')) ?? 0) -
311+
((await METRICS_HELPER.getMetricValueForTests(ReplicationMetricType.ROWS_REPLICATED_TOTAL)) ?? 0) -
310312
startRowCount;
311313

312314
if (count >= stopAfter) {
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { container } from '@powersync/lib-services-framework';
2-
import { test_utils } from '@powersync/service-core-tests';
2+
import { METRICS_HELPER } from '@powersync/service-core-tests';
33
import { beforeAll, beforeEach } from 'vitest';
44

55
beforeAll(async () => {
66
// Executes for every test file
77
container.registerDefaults();
8-
await test_utils.initMetrics();
98
});
109

1110
beforeEach(async () => {
12-
await test_utils.resetMetrics();
11+
METRICS_HELPER.resetMetrics();
1312
});

modules/module-postgres/test/src/slow_tests.test.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as bson from 'bson';
2-
import { afterEach, describe, expect, test } from 'vitest';
2+
import { afterEach, beforeAll, describe, expect, test } from 'vitest';
33
import { WalStream, WalStreamOptions } from '../../src/replication/WalStream.js';
44
import { env } from './env.js';
55
import {
@@ -15,8 +15,8 @@ import * as pgwire from '@powersync/service-jpgwire';
1515
import { SqliteRow } from '@powersync/service-sync-rules';
1616

1717
import { PgManager } from '@module/replication/PgManager.js';
18-
import { storage } from '@powersync/service-core';
19-
import { test_utils } from '@powersync/service-core-tests';
18+
import { createCoreReplicationMetrics, initializeCoreReplicationMetrics, storage } from '@powersync/service-core';
19+
import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
2020
import * as mongo_storage from '@powersync/service-module-mongodb-storage';
2121
import * as postgres_storage from '@powersync/service-module-postgres-storage';
2222
import * as timers from 'node:timers/promises';
@@ -49,6 +49,11 @@ function defineSlowTests(factory: storage.TestStorageFactory) {
4949
let abortController: AbortController | undefined;
5050
let streamPromise: Promise<void> | undefined;
5151

52+
beforeAll(async () => {
53+
createCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
54+
initializeCoreReplicationMetrics(METRICS_HELPER.metricsEngine);
55+
});
56+
5257
afterEach(async () => {
5358
// This cleans up, similar to WalStreamTestContext.dispose().
5459
// These tests are a little more complex than what is supported by WalStreamTestContext.
@@ -106,7 +111,8 @@ bucket_definitions:
106111
const options: WalStreamOptions = {
107112
abort_signal: abortController.signal,
108113
connections,
109-
storage: storage
114+
storage: storage,
115+
metrics: METRICS_HELPER.metricsEngine
110116
};
111117
walStream = new WalStream(options);
112118

@@ -358,7 +364,8 @@ bucket_definitions:
358364
const options: WalStreamOptions = {
359365
abort_signal: abortController.signal,
360366
connections,
361-
storage: storage
367+
storage: storage,
368+
metrics: METRICS_HELPER.metricsEngine
362369
};
363370
walStream = new WalStream(options);
364371

0 commit comments

Comments
 (0)