Skip to content

Commit bfc1259

Browse files
committed
Restructure TEST_TABLE for tests.
1 parent 2a7a572 commit bfc1259

40 files changed

+275
-149
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './Logger.js';
22
export { Logger } from 'winston';
3+
export { createLogger, format, transports } from 'winston';

libs/lib-services/src/migrations/AbstractMigrationAgent.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { LockManager } from '../locks/LockManager.js';
2-
import { logger } from '../logger/Logger.js';
2+
import { logger as defaultLogger, Logger } from '../logger/logger-index.js';
33
import * as defs from './migration-definitions.js';
44

55
export type MigrationParams<Generics extends MigrationAgentGenerics = MigrationAgentGenerics> = {
66
count?: number;
77
direction: defs.Direction;
88
migrationContext?: Generics['MIGRATION_CONTEXT'];
9+
logger?: Logger;
910
};
1011

1112
type WriteLogsParams = {
@@ -20,10 +21,12 @@ export type MigrationAgentGenerics = {
2021
export type RunMigrationParams<Generics extends MigrationAgentGenerics = MigrationAgentGenerics> = MigrationParams & {
2122
migrations: defs.Migration<Generics['MIGRATION_CONTEXT']>[];
2223
maxLockWaitMs?: number;
24+
logger?: Logger;
2325
};
2426

2527
type ExecuteParams = RunMigrationParams & {
2628
state?: defs.MigrationState;
29+
logger: Logger;
2730
};
2831

2932
export const DEFAULT_MAX_LOCK_WAIT_MS = 3 * 60 * 1000; // 3 minutes
@@ -46,9 +49,11 @@ export abstract class AbstractMigrationAgent<Generics extends MigrationAgentGene
4649
async run(params: RunMigrationParams) {
4750
await this.init();
4851

52+
const logger = params.logger ?? defaultLogger;
53+
4954
const { direction, migrations, migrationContext } = params;
5055
// Only one process should execute this at a time.
51-
logger.info('Acquiring lock for migrations');
56+
logger.debug('Acquiring lock for migrations');
5257
const lockHandle = await this.locks.acquire({ max_wait_ms: params.maxLockWaitMs ?? DEFAULT_MAX_LOCK_WAIT_MS });
5358

5459
if (!lockHandle) {
@@ -75,22 +80,24 @@ export abstract class AbstractMigrationAgent<Generics extends MigrationAgentGene
7580
direction,
7681
migrations,
7782
state,
78-
migrationContext
83+
migrationContext,
84+
logger
7985
});
8086

8187
await this.writeLogsToStore({
8288
log_stream: logStream,
8389
state
8490
});
8591
} finally {
86-
logger.info('Releasing migration lock');
92+
logger.debug('Releasing migration lock');
8793
await releaseLock();
8894
process.removeListener('beforeExit', releaseLock);
89-
logger.info('Done with migrations');
95+
logger.debug('Done with migrations');
9096
}
9197
}
9298

9399
protected async *execute(params: ExecuteParams): AsyncGenerator<defs.ExecutedMigration> {
100+
const logger = params.logger;
94101
const internalMigrations = await this.loadInternalMigrations();
95102
let migrations = [...internalMigrations, ...params.migrations];
96103

modules/module-mongodb-storage/src/utils/test-utils.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mongo } from '@powersync/lib-service-mongodb';
22
import { PowerSyncMongo } from '../storage/implementation/db.js';
3-
import { TestStorageOptions } from '@powersync/service-core';
3+
import { TestStorageConfig, TestStorageOptions } from '@powersync/service-core';
44
import { MongoReportStorage } from '../storage/MongoReportStorage.js';
55
import { MongoBucketStorage } from '../storage/MongoBucketStorage.js';
66
import { MongoSyncBucketStorageOptions } from '../storage/implementation/MongoSyncBucketStorage.js';
@@ -12,22 +12,25 @@ export type MongoTestStorageOptions = {
1212
};
1313

1414
export function mongoTestStorageFactoryGenerator(factoryOptions: MongoTestStorageOptions) {
15-
return async (options?: TestStorageOptions) => {
16-
const db = connectMongoForTests(factoryOptions.url, factoryOptions.isCI);
15+
return {
16+
factory: async (options?: TestStorageOptions) => {
17+
const db = connectMongoForTests(factoryOptions.url, factoryOptions.isCI);
1718

18-
// None of the tests insert data into this collection, so it was never created
19-
if (!(await db.db.listCollections({ name: db.bucket_parameters.collectionName }).hasNext())) {
20-
await db.db.createCollection('bucket_parameters');
21-
}
19+
// None of the tests insert data into this collection, so it was never created
20+
if (!(await db.db.listCollections({ name: db.bucket_parameters.collectionName }).hasNext())) {
21+
await db.db.createCollection('bucket_parameters');
22+
}
2223

23-
// Full migrations are not currently run for tests, so we manually create this
24-
await db.createCheckpointEventsCollection();
24+
// Full migrations are not currently run for tests, so we manually create this
25+
await db.createCheckpointEventsCollection();
2526

26-
if (!options?.doNotClear) {
27-
await db.clear();
28-
}
27+
if (!options?.doNotClear) {
28+
await db.clear();
29+
}
2930

30-
return new MongoBucketStorage(db, { slot_name_prefix: 'test_' }, factoryOptions.internalOptions);
31+
return new MongoBucketStorage(db, { slot_name_prefix: 'test_' }, factoryOptions.internalOptions);
32+
},
33+
tableIdStrings: false
3134
};
3235
}
3336

modules/module-mongodb-storage/test/src/__snapshots__/storage.test.ts.snap

Lines changed: 0 additions & 25 deletions
This file was deleted.

modules/module-mongodb-storage/test/src/storage_compacting.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { register, TEST_TABLE, test_utils } from '@powersync/service-core-tests';
1+
import { register, test_utils } from '@powersync/service-core-tests';
22
import { describe, expect, test } from 'vitest';
33
import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
44
import { storage, SyncRulesBucketStorage } from '@powersync/service-core';
55

66
describe('Mongo Sync Bucket Storage Compact', () => {
77
register.registerCompactTests(INITIALIZED_MONGO_STORAGE_FACTORY);
88

9+
const TEST_TABLE = test_utils.makeTestTable('test', ['id'], INITIALIZED_MONGO_STORAGE_FACTORY);
10+
911
describe('with blank bucket_state', () => {
1012
// This can happen when migrating from older service versions, that did not populate bucket_state yet.
1113
const populate = async (bucketStorage: SyncRulesBucketStorage) => {
@@ -39,7 +41,7 @@ describe('Mongo Sync Bucket Storage Compact', () => {
3941
};
4042

4143
const setup = async () => {
42-
await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY();
44+
await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
4345
const syncRules = await factory.updateSyncRules({
4446
content: `
4547
bucket_definitions:

modules/module-mongodb-storage/test/src/storage_sync.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { storage } from '@powersync/service-core';
2-
import { register, TEST_TABLE, test_utils } from '@powersync/service-core-tests';
2+
import { register, test_utils } from '@powersync/service-core-tests';
33
import { describe, expect, test } from 'vitest';
44
import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
55

66
describe('sync - mongodb', () => {
77
register.registerSyncTests(INITIALIZED_MONGO_STORAGE_FACTORY);
8+
const TEST_TABLE = test_utils.makeTestTable('test', ['id'], INITIALIZED_MONGO_STORAGE_FACTORY);
89

910
// The split of returned results can vary depending on storage drivers
1011
test('large batch (2)', async () => {
@@ -19,7 +20,7 @@ describe('sync - mongodb', () => {
1920
- SELECT id, description FROM "%"
2021
`
2122
);
22-
await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY();
23+
await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
2324
const bucketStorage = factory.getInstance(sync_rules);
2425

2526
const result = await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ describe('change stream', () => {
2121
describeWithStorage({ timeout: 20_000 }, defineChangeStreamTests);
2222
});
2323

24-
function defineChangeStreamTests(factory: storage.TestStorageFactory) {
24+
function defineChangeStreamTests(config: storage.TestStorageConfig) {
25+
const factory = config.factory;
26+
2527
test('replicating basic values', async () => {
2628
await using context = await ChangeStreamTestContext.open(factory, {
2729
mongoOptions: { postImages: PostImagesOption.READ_ONLY }

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { mongo } from '@powersync/lib-service-mongodb';
2-
import { reduceBucket, TestStorageFactory } from '@powersync/service-core';
2+
import { reduceBucket, TestStorageConfig, TestStorageFactory } from '@powersync/service-core';
33
import { METRICS_HELPER } from '@powersync/service-core-tests';
44
import { JSONBig } from '@powersync/service-jsonbig';
55
import { SqliteJsonValue } from '@powersync/service-sync-rules';
@@ -12,7 +12,9 @@ describe('chunked snapshots', () => {
1212
describeWithStorage({ timeout: 120_000 }, defineBatchTests);
1313
});
1414

15-
function defineBatchTests(factory: TestStorageFactory) {
15+
function defineBatchTests(config: TestStorageConfig) {
16+
const { factory } = config;
17+
1618
// This is not as sensitive to the id type as postgres, but we still test a couple of cases
1719
test('chunked snapshot (int32)', async () => {
1820
await testChunkedSnapshot({

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ChangeStreamInvalidatedError } from '@module/replication/ChangeStream.js';
22
import { MongoManager } from '@module/replication/MongoManager.js';
33
import { normalizeConnectionConfig } from '@module/types/types.js';
4-
import { BucketStorageFactory, TestStorageOptions } from '@powersync/service-core';
4+
import { TestStorageConfig } from '@powersync/service-core';
55
import { describe, expect, test } from 'vitest';
66
import { ChangeStreamTestContext } from './change_stream_utils.js';
77
import { env } from './env.js';
@@ -11,7 +11,9 @@ describe('mongodb resuming replication', () => {
1111
describeWithStorage({}, defineResumeTest);
1212
});
1313

14-
function defineResumeTest(factoryGenerator: (options?: TestStorageOptions) => Promise<BucketStorageFactory>) {
14+
function defineResumeTest(config: TestStorageConfig) {
15+
const factoryGenerator = config.factory;
16+
1517
test('resuming with a different source database', async () => {
1618
await using context = await ChangeStreamTestContext.open(factoryGenerator);
1719
const { db } = context;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import { env } from './env.js';
88
import { describeWithStorage } from './util.js';
99

1010
describe.skipIf(!(env.CI || env.SLOW_TESTS))('batch replication', function () {
11-
describeWithStorage({ timeout: 240_000 }, function (factory) {
11+
describeWithStorage({ timeout: 240_000 }, function (config) {
1212
test('resuming initial replication (1)', async () => {
1313
// Stop early - likely to not include deleted row in first replication attempt.
14-
await testResumingReplication(factory, 2000);
14+
await testResumingReplication(config.factory, 2000);
1515
});
1616
test('resuming initial replication (2)', async () => {
1717
// Stop late - likely to include deleted row in first replication attempt.
18-
await testResumingReplication(factory, 8000);
18+
await testResumingReplication(config.factory, 8000);
1919
});
2020
});
2121
});

0 commit comments

Comments
 (0)