Skip to content

Commit 4958054

Browse files
committed
Better fix for tests.
1 parent d968d39 commit 4958054

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

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

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { MissingReplicationSlotError } from '@module/replication/WalStream.js';
22
import { storage } from '@powersync/service-core';
33
import { METRICS_HELPER, putOp, removeOp } from '@powersync/service-core-tests';
44
import { pgwireRows } from '@powersync/service-jpgwire';
5+
import { JSONBig } from '@powersync/service-jsonbig';
56
import { ReplicationMetric } from '@powersync/service-types';
67
import * as crypto from 'crypto';
7-
import { afterAll, beforeAll, describe, expect, test } from 'vitest';
8+
import { describe, expect, test } from 'vitest';
89
import { describeWithStorage } from './util.js';
910
import { WalStreamTestContext, withMaxWalSize } from './wal_stream_utils.js';
10-
import { JSONBig } from '@powersync/service-jsonbig';
1111

1212
const BASIC_SYNC_RULES = `
1313
bucket_definitions:
@@ -328,18 +328,8 @@ bucket_definitions:
328328
// In the service, this error is handled in WalStreamReplicationJob,
329329
// creating a new replication slot.
330330
await expect(async () => {
331-
try {
332-
await context.replicateSnapshot();
333-
await context.getCheckpoint();
334-
} catch (e) {
335-
// replicateSnapshot can have a ReplicationAbortedError(cause: MissingReplicationSlotError).
336-
// This is specific to tests - real replication will get the MissingReplicationSlotError directly.
337-
if (e?.cause) {
338-
throw e.cause;
339-
} else {
340-
throw e;
341-
}
342-
}
331+
await context.replicateSnapshot();
332+
await context.getCheckpoint();
343333
}).rejects.toThrowError(MissingReplicationSlotError);
344334
}
345335
}
@@ -391,17 +381,7 @@ bucket_definitions:
391381
// The error is handled on a higher level, which triggers
392382
// creating a new replication slot.
393383
await expect(async () => {
394-
try {
395-
await context.replicateSnapshot();
396-
} catch (e) {
397-
// replicateSnapshot can have a ReplicationAbortedError(cause: MissingReplicationSlotError).
398-
// This is specific to tests - real replication will get the MissingReplicationSlotError directly.
399-
if (e?.cause) {
400-
throw e.cause;
401-
} else {
402-
throw e;
403-
}
404-
}
384+
await context.replicateSnapshot();
405385
}).rejects.toThrowError(MissingReplicationSlotError);
406386
}
407387
});

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { METRICS_HELPER, test_utils } from '@powersync/service-core-tests';
1515
import * as pgwire from '@powersync/service-jpgwire';
1616
import { clearTestDb, getClientCheckpoint, TEST_CONNECTION_OPTIONS } from './util.js';
1717
import { CustomTypeRegistry } from '@module/types/registry.js';
18+
import { ReplicationAbortedError } from '@powersync/lib-services-framework';
1819

1920
export class WalStreamTestContext implements AsyncDisposable {
2021
private _walStream?: WalStream;
@@ -142,7 +143,17 @@ export class WalStreamTestContext implements AsyncDisposable {
142143
async replicateSnapshot() {
143144
// Use a settledPromise to avoid unhandled rejections
144145
this.settledReplicationPromise = settledPromise(this.walStream.replicate());
145-
await Promise.race([unsettledPromise(this.settledReplicationPromise), this.walStream.waitForInitialSnapshot()]);
146+
try {
147+
await Promise.race([unsettledPromise(this.settledReplicationPromise), this.walStream.waitForInitialSnapshot()]);
148+
} catch (e) {
149+
if (e instanceof ReplicationAbortedError && e.cause != null) {
150+
// Edge case for tests: replicate() can throw an error, but we'd receive the ReplicationAbortedError from
151+
// waitForInitialSnapshot() first. In that case, prioritize the cause, e.g. MissingReplicationSlotError.
152+
// This is not a concern for production use, since we only use waitForInitialSnapshot() in tests.
153+
throw e.cause;
154+
}
155+
throw e;
156+
}
146157
}
147158

148159
async getCheckpoint(options?: { timeout?: number }) {

0 commit comments

Comments
 (0)