Skip to content

Commit

Permalink
Adjust test timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
DallasHoff committed Dec 2, 2024
1 parent 319baa7 commit e10a373
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 119 deletions.
68 changes: 36 additions & 32 deletions test/delete-database-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,39 +45,43 @@ describe.each([
await destroy();
});

it('should or should not notify other instances of a delete', async () => {
let onConnectReason1: ConnectReason | null = null;
let onConnectReason2: ConnectReason | null = null;

const db1 = new SQLocal({
databasePath: path,
onConnect: (reason) => (onConnectReason1 = reason),
});
const db2 = new SQLocal({
databasePath: path,
onConnect: (reason) => (onConnectReason2 = reason),
});

await vi.waitUntil(() => onConnectReason1 === 'initial');
onConnectReason1 = null;
await vi.waitUntil(() => onConnectReason2 === 'initial');
onConnectReason2 = null;

await db1.deleteDatabaseFile();

if (type !== 'memory') {
await vi.waitUntil(() => onConnectReason2 === 'delete');
expect(onConnectReason2).toBe('delete');
} else {
expect(onConnectReason2).toBe(null);
it(
'should or should not notify other instances of a delete',
{ timeout: type === 'opfs' ? 2000 : undefined },
async () => {
let onConnectReason1: ConnectReason | null = null;
let onConnectReason2: ConnectReason | null = null;

const db1 = new SQLocal({
databasePath: path,
onConnect: (reason) => (onConnectReason1 = reason),
});
const db2 = new SQLocal({
databasePath: path,
onConnect: (reason) => (onConnectReason2 = reason),
});

await vi.waitUntil(() => onConnectReason1 === 'initial');
onConnectReason1 = null;
await vi.waitUntil(() => onConnectReason2 === 'initial');
onConnectReason2 = null;

await db1.deleteDatabaseFile();

if (type !== 'memory') {
await vi.waitUntil(() => onConnectReason2 === 'delete');
expect(onConnectReason2).toBe('delete');
} else {
expect(onConnectReason2).toBe(null);
}

expect(onConnectReason1).toBe('delete');

await db2.deleteDatabaseFile();
await db2.destroy();
await db1.destroy();
}

expect(onConnectReason1).toBe('delete');

await db2.deleteDatabaseFile();
await db2.destroy();
await db1.destroy();
});
);

it('should restore user functions', async () => {
const db = new SQLocal(path);
Expand Down
176 changes: 92 additions & 84 deletions test/overwrite-database-file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,63 +83,67 @@ describe.each([
await db2.destroy();
});

it('should or should not notify other instances of an overwrite', async () => {
const eventValues = new Set<string>();
const db1 = new SQLocal({
databasePath: path,
onConnect: (reason) => eventValues.add(`connect1(${reason})`),
});
const db2 = new SQLocal({
databasePath: path,
onConnect: (reason) => eventValues.add(`connect2(${reason})`),
});

await db2.sql`CREATE TABLE nums (num INTEGER NOT NULL)`;
await db2.sql`INSERT INTO nums (num) VALUES (123)`;

await vi.waitUntil(() => {
return (
eventValues.has('connect1(initial)') &&
eventValues.has('connect2(initial)')
);
});
eventValues.clear();

if (type !== 'memory') {
const nums1 = await db1.sql`SELECT * FROM nums`;
expect(nums1).toEqual([{ num: 123 }]);
}

const dbFile = await db2.getDatabaseFile();
await db2.sql`INSERT INTO nums (num) VALUES (456)`;
await db1.overwriteDatabaseFile(dbFile, async () => {
await db1.sql`INSERT INTO nums (num) VALUES (789)`;
eventValues.add('unlock1');
});

if (type !== 'memory') {
await vi.waitUntil(() => eventValues.size === 3);
expect(eventValues.has('unlock1')).toBe(true);
expect(eventValues.has('connect1(overwrite)')).toBe(true);
expect(eventValues.has('connect2(overwrite)')).toBe(true);
} else {
await vi.waitUntil(() => eventValues.size === 1);
expect(eventValues.has('unlock1')).toBe(true);
it(
'should or should not notify other instances of an overwrite',
{ timeout: type === 'opfs' ? 2000 : undefined },
async () => {
const eventValues = new Set<string>();
const db1 = new SQLocal({
databasePath: path,
onConnect: (reason) => eventValues.add(`connect1(${reason})`),
});
const db2 = new SQLocal({
databasePath: path,
onConnect: (reason) => eventValues.add(`connect2(${reason})`),
});

await db2.sql`CREATE TABLE nums (num INTEGER NOT NULL)`;
await db2.sql`INSERT INTO nums (num) VALUES (123)`;

await vi.waitUntil(() => {
return (
eventValues.has('connect1(initial)') &&
eventValues.has('connect2(initial)')
);
});
eventValues.clear();

if (type !== 'memory') {
const nums1 = await db1.sql`SELECT * FROM nums`;
expect(nums1).toEqual([{ num: 123 }]);
}

const dbFile = await db2.getDatabaseFile();
await db2.sql`INSERT INTO nums (num) VALUES (456)`;
await db1.overwriteDatabaseFile(dbFile, async () => {
await db1.sql`INSERT INTO nums (num) VALUES (789)`;
eventValues.add('unlock1');
});

if (type !== 'memory') {
await vi.waitUntil(() => eventValues.size === 3);
expect(eventValues.has('unlock1')).toBe(true);
expect(eventValues.has('connect1(overwrite)')).toBe(true);
expect(eventValues.has('connect2(overwrite)')).toBe(true);
} else {
await vi.waitUntil(() => eventValues.size === 1);
expect(eventValues.has('unlock1')).toBe(true);
}

const expectedNums = [{ num: 123 }, { num: 789 }];
const nums2 = await db1.sql`SELECT * FROM nums`;
expect(nums2).toEqual(expectedNums);

if (type !== 'memory') {
const nums3 = await db2.sql`SELECT * FROM nums`;
expect(nums3).toEqual(expectedNums);
}

await db1.destroy();
await db2.deleteDatabaseFile();
await db2.destroy();
}

const expectedNums = [{ num: 123 }, { num: 789 }];
const nums2 = await db1.sql`SELECT * FROM nums`;
expect(nums2).toEqual(expectedNums);

if (type !== 'memory') {
const nums3 = await db2.sql`SELECT * FROM nums`;
expect(nums3).toEqual(expectedNums);
}

await db1.destroy();
await db2.deleteDatabaseFile();
await db2.destroy();
});
);

it('should restore user functions', async () => {
const db = new SQLocal(path);
Expand Down Expand Up @@ -197,32 +201,36 @@ describe.each([
await destroy();
});

it('should run onInit statements before other queries after overwrite', async () => {
const databasePath = path;
const onInit: ClientConfig['onInit'] = (sql) => {
return [sql`PRAGMA foreign_keys = ON`];
};

const results: number[] = [];

const db1 = new SQLocal({ databasePath, onInit });
const db2 = new SQLocal({ databasePath, onInit });

const [{ foreign_keys: result1 }] = await db1.sql`PRAGMA foreign_keys`;
results.push(result1);
await db1.sql`PRAGMA foreign_keys = OFF`;
const [{ foreign_keys: result2 }] = await db1.sql`PRAGMA foreign_keys`;
results.push(result2);
const file = await db2.getDatabaseFile();
await db1.overwriteDatabaseFile(file);
const [{ foreign_keys: result3 }] = await db1.sql`PRAGMA foreign_keys`;
results.push(result3);
const [{ foreign_keys: result4 }] = await db2.sql`PRAGMA foreign_keys`;
results.push(result4);

expect(results).toEqual([1, 0, 1, 1]);

await db1.destroy();
await db2.destroy();
});
it(
'should run onInit statements before other queries after overwrite',
{ timeout: type === 'opfs' ? 1500 : undefined },
async () => {
const databasePath = path;
const onInit: ClientConfig['onInit'] = (sql) => {
return [sql`PRAGMA foreign_keys = ON`];
};

const results: number[] = [];

const db1 = new SQLocal({ databasePath, onInit });
const db2 = new SQLocal({ databasePath, onInit });

const [{ foreign_keys: result1 }] = await db1.sql`PRAGMA foreign_keys`;
results.push(result1);
await db1.sql`PRAGMA foreign_keys = OFF`;
const [{ foreign_keys: result2 }] = await db1.sql`PRAGMA foreign_keys`;
results.push(result2);
const file = await db2.getDatabaseFile();
await db1.overwriteDatabaseFile(file);
const [{ foreign_keys: result3 }] = await db1.sql`PRAGMA foreign_keys`;
results.push(result3);
const [{ foreign_keys: result4 }] = await db2.sql`PRAGMA foreign_keys`;
results.push(result4);

expect(results).toEqual([1, 0, 1, 1]);

await db1.destroy();
await db2.destroy();
}
);
});
6 changes: 3 additions & 3 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { defineConfig } from 'vite';

export default defineConfig({
test: {
testTimeout: 1500,
hookTimeout: 1500,
teardownTimeout: 1500,
testTimeout: 1000,
hookTimeout: 1000,
teardownTimeout: 1000,
includeTaskLocation: true,
browser: {
enabled: true,
Expand Down

0 comments on commit e10a373

Please sign in to comment.