diff --git a/.changeset/skip-test-connection-replication-metrics.md b/.changeset/skip-test-connection-replication-metrics.md new file mode 100644 index 000000000..4406c5884 --- /dev/null +++ b/.changeset/skip-test-connection-replication-metrics.md @@ -0,0 +1,5 @@ +--- +'@powersync/service-module-postgres': patch +--- + +Skip the PostgreSQL replication-byte metrics recorder in `test-connection` mode so the command can run without `DATA_REPLICATED_BYTES`. diff --git a/modules/module-postgres/src/module/PostgresModule.ts b/modules/module-postgres/src/module/PostgresModule.ts index 3a3bfb2ec..101415230 100644 --- a/modules/module-postgres/src/module/PostgresModule.ts +++ b/modules/module-postgres/src/module/PostgresModule.ts @@ -31,7 +31,7 @@ export class PostgresModule extends replication.ReplicationModule { // Record replicated bytes using global jpgwire metrics. Only registered if this module is replicating - if (context.replicationEngine) { + if (context.replicationEngine && context.serviceMode !== system.ServiceContextMode.TEST_CONNECTION) { jpgwire.setMetricsRecorder({ addBytesRead(bytes) { context.metricsEngine.getCounter(ReplicationMetric.DATA_REPLICATED_BYTES).add(bytes); diff --git a/modules/module-postgres/test/src/metrics_recorder.test.ts b/modules/module-postgres/test/src/metrics_recorder.test.ts new file mode 100644 index 000000000..22f75eccc --- /dev/null +++ b/modules/module-postgres/test/src/metrics_recorder.test.ts @@ -0,0 +1,40 @@ +import { system } from '@powersync/service-core'; +import * as jpgwire from '@powersync/service-jpgwire'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { PostgresModule } from '../../src/module/PostgresModule.js'; + +describe('PostgresModule metrics recorder', () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + it('does not install a jpgwire recorder in TEST_CONNECTION mode', async () => { + const spy = vi.spyOn(jpgwire, 'setMetricsRecorder'); + const mod = new PostgresModule(); + await mod.onInitialized({ + replicationEngine: {}, + serviceMode: system.ServiceContextMode.TEST_CONNECTION, + metricsEngine: { + getCounter() { + throw new Error('DATA_REPLICATED_BYTES should not be read in test-connection'); + } + } + } as unknown as system.ServiceContextContainer); + + expect(spy).not.toHaveBeenCalled(); + }); + + it('installs a jpgwire recorder when replication metrics are available', async () => { + const spy = vi.spyOn(jpgwire, 'setMetricsRecorder'); + const mod = new PostgresModule(); + await mod.onInitialized({ + replicationEngine: {}, + serviceMode: system.ServiceContextMode.UNIFIED, + metricsEngine: { + getCounter: () => ({ add() {} }) + } + } as unknown as system.ServiceContextContainer); + + expect(spy).toHaveBeenCalledTimes(1); + }); +});