Skip to content

Commit

Permalink
fix: build
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Jan 23, 2025
1 parent c3481e9 commit 5add70c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/client/handlers/VaultsLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class VaultsLog extends ServerHandler<
return await vaultManager.withVaults(
[vaultId],
async (vault) => {
return await vault.log(input.commitId ?? 'HEAD', input.depth, ctx);
return await vault.log(input.commitId ?? 'HEAD', input.depth);
},
tran,
ctx,
Expand Down
2 changes: 1 addition & 1 deletion src/client/handlers/VaultsSecretsRemove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class VaultsSecretsRemove extends DuplexHandler<
// release it when cleaning up.
const acquire = await vaultManager.withVaults(
[vaultId],
async (vault) => vault.acquireWrite(undefined, ctx),
async (vault) => vault.acquireWrite(ctx),
);
vaultAcquires.push(acquire);
}
Expand Down
7 changes: 2 additions & 5 deletions src/client/handlers/VaultsVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ class VaultsVersion extends UnaryHandler<
async (vault) => {
// Use default values for the ref and limit. We only care about
// passing in the relevant context.
const latestOid = (await vault.log(undefined, undefined, ctx))[0]
.commitId;
const latestOid = (await vault.log())[0].commitId;
await vault.version(versionId);
const currentVersionId = (
await vault.log(versionId, undefined, ctx)
)[0]?.commitId;
const currentVersionId = (await vault.log(versionId))[0]?.commitId;
return [latestOid, currentVersionId];
},
tran,
Expand Down
4 changes: 0 additions & 4 deletions src/vaults/VaultInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import {
ready,
} from '@matrixai/async-init/dist/CreateDestroyStartStop';
import { RWLockWriter } from '@matrixai/async-locks';
import { timedCancellable as timedCancellableF } from '@matrixai/contexts/dist/functions';
import {
context,
timed,
Expand Down Expand Up @@ -446,14 +445,11 @@ class VaultInternal {
public async log(
ref?: string | VaultRef,
limit?: number,
ctx?: Partial<ContextTimedInput>,
): Promise<Array<CommitLog>>;
@ready(new vaultsErrors.ErrorVaultNotRunning())
@timedCancellable(true)
public async log(
ref: string | VaultRef = 'HEAD',
limit: number,
@context ctx: ContextTimed,
): Promise<Array<CommitLog>> {
vaultsUtils.assertRef(ref);
if (ref === vaultsUtils.tagLast) {
Expand Down
254 changes: 123 additions & 131 deletions tests/vaults/VaultInternal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import path from 'path';
import fs from 'fs';
import git from 'isomorphic-git';
import { EncryptedFS } from 'encryptedfs';
import { timedCancellable as timedCancellableF } from '@matrixai/contexts/dist/functions';
import { DB } from '@matrixai/db';
import { withF } from '@matrixai/resources';
import Logger, { LogLevel, StreamHandler } from '@matrixai/logger';
Expand Down Expand Up @@ -641,62 +640,59 @@ describe('VaultInternal', () => {
await expect(vault.version(newRef2)).rejects.toThrow();
});
test('commit added if mutation in writeG', async () => {
const f = async (ctx: ContextTimed) => {
const commit = (await vault.log())[0].commitId;
const gen = vault.writeG(
async function* (efs): AsyncGenerator {
yield await efs.writeFile('secret-1', 'secret-content');
},
undefined,
ctx,
);
for await (const _ of gen) {
// Do nothing
}
const log = await vault.log();
expect(log).toHaveLength(2);
expect(log[0].commitId).not.toStrictEqual(commit);
};
await timedCancellableF(f, true)();
const abortController = new AbortController();
const ctx = { signal: abortController.signal } as ContextTimed;
const commit = (await vault.log())[0].commitId;
const gen = vault.writeG(
async function* (efs): AsyncGenerator {
yield await efs.writeFile('secret-1', 'secret-content');
},
undefined,
ctx,
);
for await (const _ of gen) {
// Do nothing
}
const log = await vault.log();
expect(log).toHaveLength(2);
expect(log[0].commitId).not.toStrictEqual(commit);
});
test('no commit added if no mutation in writeG', async () => {
const f = async (ctx: ContextTimed) => {
const commit = (await vault.log())[0].commitId;
const gen = vault.writeG(
async function* (_efs): AsyncGenerator {},
undefined,
ctx,
);
for await (const _ of gen) {
// Do nothing
}
const log = await vault.log();
expect(log).toHaveLength(1);
expect(log[0].message).not.toContain('secret-1');
expect(log[0].commitId).toStrictEqual(commit);
};
await timedCancellableF(f, true)();
const abortController = new AbortController();
const ctx = { signal: abortController.signal } as ContextTimed;
const commit = (await vault.log())[0].commitId;
const gen = vault.writeG(
async function* (_efs): AsyncGenerator {},
undefined,
ctx,
);
for await (const _ of gen) {
// Do nothing
}
const log = await vault.log();
expect(log).toHaveLength(1);
expect(log[0].message).not.toContain('secret-1');
expect(log[0].commitId).toStrictEqual(commit);
});
test('no mutation to vault when part of a commit operation fails in writeG', async () => {
const f = async (ctx: ContextTimed) => {
const gen = vault.writeG(
async function* (efs): AsyncGenerator {
yield await efs.writeFile(secret1.name, secret1.content);
yield await efs.rename('notValid', 'randomName'); // Throws
},
undefined,
ctx,
);
// Failing commit operation
await expect(() => consumeGenerator(gen)).rejects.toThrow();
// Make sure secret1 wasn't written when the above commit failed
await vault.readF(async (efs) => {
expect(await efs.readdir('.')).not.toContain(secret1.name);
});
// No new commit
expect(await vault.log()).toHaveLength(1);
};
await timedCancellableF(f, true)();
const abortController = new AbortController();
const ctx = { signal: abortController.signal } as ContextTimed;
const gen = vault.writeG(
async function* (efs): AsyncGenerator {
yield await efs.writeFile(secret1.name, secret1.content);
yield await efs.rename('notValid', 'randomName'); // Throws
},
undefined,
ctx,
);
// Failing commit operation
await expect(() => consumeGenerator(gen)).rejects.toThrow();
// Make sure secret1 wasn't written when the above commit failed
await vault.readF(async (efs) => {
expect(await efs.readdir('.')).not.toContain(secret1.name);
});
// No new commit
expect(await vault.log()).toHaveLength(1);
});
test('no commit after readG', async () => {
await vault.writeF(async (efs) => {
Expand Down Expand Up @@ -757,7 +753,7 @@ describe('VaultInternal', () => {
refs.push(await quickCommit(logElement.commitId, `secret-${num++}`));
}
const abortController = new AbortController();
const ctx = { signal: abortController.signal };
const ctx = { signal: abortController.signal } as ContextTimed;
// @ts-ignore: protected method
await vault.garbageCollectGitObjectsGlobal(ctx);

Expand Down Expand Up @@ -802,46 +798,45 @@ describe('VaultInternal', () => {
expect(finished).toBe(true);
});
test('writeG respects read and write locking', async () => {
const f = async (ctx: ContextTimed) => {
// Hold a write lock
const lock = vault.getLock();
const [releaseWrite] = await lock.write()();
const abortController = new AbortController();
const ctx = { signal: abortController.signal } as ContextTimed;
// Hold a write lock
const lock = vault.getLock();
const [releaseWrite] = await lock.write()();

let finished = false;
const writeGen = vault.writeG(
async function* () {
yield;
finished = true;
yield;
},
undefined,
ctx,
);
const runP = consumeGenerator(writeGen);
await sleep(waitDelay);
expect(finished).toBe(false);
await releaseWrite();
await runP;
expect(finished).toBe(true);
let finished = false;
const writeGen = vault.writeG(
async function* () {
yield;
finished = true;
yield;
},
undefined,
ctx,
);
const runP = consumeGenerator(writeGen);
await sleep(waitDelay);
expect(finished).toBe(false);
await releaseWrite();
await runP;
expect(finished).toBe(true);

const [releaseRead] = await lock.read()();
finished = false;
const writeGen2 = vault.writeG(
async function* () {
yield;
finished = true;
yield;
},
undefined,
ctx,
);
const runP2 = consumeGenerator(writeGen2);
await sleep(waitDelay);
await releaseRead();
await runP2;
expect(finished).toBe(true);
};
await timedCancellableF(f, true)();
const [releaseRead] = await lock.read()();
finished = false;
const writeGen2 = vault.writeG(
async function* () {
yield;
finished = true;
yield;
},
undefined,
ctx,
);
const runP2 = consumeGenerator(writeGen2);
await sleep(waitDelay);
await releaseRead();
await runP2;
expect(finished).toBe(true);
});
test('readF respects write locking', async () => {
const lock = vault.getLock();
Expand Down Expand Up @@ -952,49 +947,46 @@ describe('VaultInternal', () => {
await releaseRead();
});
test('can acquire a write resource', async () => {
const f = async (ctx: ContextTimed) => {
const acquireWrite = vault.acquireWrite(undefined, ctx);
await withF([acquireWrite], async ([efs]) => {
await efs.writeFile(secret1.name, secret1.content);
});
await vault.readF(async (efs) => {
const content = await efs.readFile(secret1.name);
expect(content.toString()).toEqual(secret1.content);
});
};
await timedCancellableF(f, true)();
const abortController = new AbortController();
const ctx = { signal: abortController.signal } as ContextTimed;
const acquireWrite = vault.acquireWrite(ctx);
await withF([acquireWrite], async ([efs]) => {
await efs.writeFile(secret1.name, secret1.content);
});
await vault.readF(async (efs) => {
const content = await efs.readFile(secret1.name);
expect(content.toString()).toEqual(secret1.content);
});
});
test('acquiring write resource respects write locking', async () => {
const f = async (ctx: ContextTimed) => {
const lock = vault.getLock();
const [releaseWrite] = await lock.write()();
let finished = false;
const writeP = withF([vault.acquireWrite(undefined, ctx)], async () => {
finished = true;
});
await sleep(waitDelay);
expect(finished).toBe(false);
await releaseWrite();
await writeP;
expect(finished).toBe(true);
};
await timedCancellableF(f, true)();
const abortController = new AbortController();
const ctx = { signal: abortController.signal } as ContextTimed;
const lock = vault.getLock();
const [releaseWrite] = await lock.write()();
let finished = false;
const writeP = withF([vault.acquireWrite(ctx)], async () => {
finished = true;
});
await sleep(waitDelay);
expect(finished).toBe(false);
await releaseWrite();
await writeP;
expect(finished).toBe(true);
});
test('acquiring write resource respects read locking', async () => {
const f = async (ctx: ContextTimed) => {
const lock = vault.getLock();
const [releaseRead] = await lock.read()();
let finished = false;
const writeP = withF([vault.acquireWrite(undefined, ctx)], async () => {
finished = true;
});
await sleep(waitDelay);
expect(finished).toBe(false);
await releaseRead();
await writeP;
expect(finished).toBe(true);
};
await timedCancellableF(f, true)();
const abortController = new AbortController();
const ctx = { signal: abortController.signal } as ContextTimed;
const lock = vault.getLock();
const [releaseRead] = await lock.read()();
let finished = false;
const writeP = withF([vault.acquireWrite(ctx)], async () => {
finished = true;
});
await sleep(waitDelay);
expect(finished).toBe(false);
await releaseRead();
await writeP;
expect(finished).toBe(true);
});
// Life-cycle
test('can create with CreateVaultInternal', async () => {
Expand Down

0 comments on commit 5add70c

Please sign in to comment.