Skip to content

Commit

Permalink
WIP - #305 Expanding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Mar 7, 2022
1 parent 845e33a commit f28aea6
Show file tree
Hide file tree
Showing 3 changed files with 353 additions and 160 deletions.
20 changes: 16 additions & 4 deletions src/vaults/VaultInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ class VaultInternal {
// Make the directory where the .git files will be auto generated and
// where the contents will be cloned to ('contents' file)
await efs.mkdir(vault.vaultDataDir, { recursive: true });
let vaultName: VaultName, remoteVaultId: VaultId, remote: RemoteInfo;
let vaultName: VaultName;
let remoteVaultId: VaultId;
let remote: RemoteInfo;
try {
[vaultName, remoteVaultId] = await nodeConnectionManager.withConnF(
targetNodeId,
Expand All @@ -150,13 +152,13 @@ class VaultInternal {
remoteNode: nodesUtils.encodeNodeId(targetNodeId),
remoteVault: vaultsUtils.encodeVaultId(remoteVaultId),
};
} catch (err) {
} catch (e) {
// If the error flag set and we have the generalised SmartHttpError from
// isomorphic git then we need to throw the polykey error
if (err instanceof git.Errors.SmartHttpError && error) {
if (e instanceof git.Errors.SmartHttpError && error) {
throw error;
}
throw err;
throw e;
}

await vault.start({ vaultName });
Expand Down Expand Up @@ -667,7 +669,17 @@ class VaultInternal {
gitdir: this.vaultGitDir,
author: vaultsUtils.commitAuthor(this.keyManager.getNodeId()),
message: 'Initial Commit',
ref: 'HEAD',
})) as CommitId;
// Update master ref
await git.writeRef({
fs: this.efs,
dir: this.vaultDataDir,
gitdir: this.vaultGitDir,
ref: vaultsUtils.canonicalBranchRef,
value: commitIdLatest,
force: true,
});
} else {
// Checking for dirty
if (
Expand Down
42 changes: 28 additions & 14 deletions src/vaults/VaultManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,26 +225,13 @@ class VaultManager {
// the working directory commit state is saved

for (const [vaultIdString, vaultAndLock] of this.vaultMap) {
// This is locking each vault... before it tries to do this
// but if we are calling stop now
// we will have blocked all the other methods
// so in this sense, it actually waits for all vault locks to be relinquished
// before attempting to do anything here
// now if start stop has their own lock
// this this applies already just be calling stop
// in that it waits for stop to finish

const vaultId = IdInternal.fromString<VaultId>(vaultIdString);
await withF([this.getWriteLock(vaultId)], async () => {
await vaultAndLock.vault?.stop();
});
this.vaultMap.delete(vaultIdString);
}

// Need to figure out if this id thing is a good idea
// the id should already be workable as a string
// i forgot if it also works under map

await this.efs.stop();
this.vaultMap = new Map();
this.logger.info(`Stopped ${this.constructor.name}`);
Expand Down Expand Up @@ -588,8 +575,35 @@ class VaultManager {
efs: this.efs,
logger: this.logger.getChild(VaultInternal.name),
});
// TODO: We need to add the cloned vaultName to the name->id mapping
this.vaultMap.set(vaultIdString, { lock, vault });
const vaultMetadata = (await this.getVaultMeta(vaultId))!;
const baseVaultName = vaultMetadata.vaultName;
// Need to check if the name is taken, 10 attempts
let newVaultName = baseVaultName;
let attempts = 1;
while (true) {
const existingVaultId = await this.db.get(
this.vaultsNamesDbDomain,
newVaultName,
);
if (existingVaultId == null) break;
newVaultName = `${baseVaultName}-${attempts}`;
if (attempts >= 100) throw Error('MAKE PROPER ERROR');
attempts++;
}
// Set the vaultName -> vaultId mapping
await this.db.put(
this.vaultsNamesDbDomain,
newVaultName,
vaultId.toBuffer(),
true,
);
// Update vault metadata
await this.db.put(
[...this.vaultsDbDomain, vaultsUtils.encodeVaultId(vaultId)],
VaultInternal.nameKey,
newVaultName,
);
this.logger.info(
`Cloned Vault ${vaultsUtils.encodeVaultId(vaultId)} on Node ${nodeId}`,
);
Expand Down
Loading

0 comments on commit f28aea6

Please sign in to comment.