Skip to content

Commit 4780cd8

Browse files
committed
chore: added test for cancellation to VaultsSecretsRemove
1 parent a027966 commit 4780cd8

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

tests/client/handlers/vaults.test.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import os from 'os';
1414
import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
1515
import { DB } from '@matrixai/db';
1616
import { RPCClient } from '@matrixai/rpc';
17+
import { ErrorRPCTimedOut } from '@matrixai/rpc/dist/errors';
1718
import { WebSocketClient } from '@matrixai/ws';
1819
import TaskManager from '@/tasks/TaskManager';
1920
import ACL from '@/acl/ACL';
@@ -2463,6 +2464,62 @@ describe('vaultsSecretsRemove', () => {
24632464
vaultsErrors.ErrorVaultsVaultUndefined,
24642465
);
24652466
});
2467+
test('should fail when cancelled', async () => {
2468+
// Inducing a cancellation by a timeout
2469+
const response = await rpcClient.methods.vaultsSecretsRemove({
2470+
timer: 100,
2471+
});
2472+
// Read response
2473+
const consumeP = async () => {
2474+
for await (const _ of response.readable) {
2475+
// Consume values
2476+
}
2477+
};
2478+
await expect(consumeP()).rejects.toThrow(ErrorRPCTimedOut);
2479+
});
2480+
test('should cancel in the midst of an operation', async () => {
2481+
// Create secrets
2482+
const secretName = 'test-secret1';
2483+
const vaultId = await vaultManager.createVault('test-vault');
2484+
const vaultIdEncoded = vaultsUtils.encodeVaultId(vaultId);
2485+
await vaultManager.withVaults([vaultId], async (vault) => {
2486+
await vault.writeF(async (efs) => {
2487+
await efs.writeFile(secretName, secretName);
2488+
});
2489+
});
2490+
// Inducing a cancellation by a timeout
2491+
const response = await rpcClient.methods.vaultsSecretsRemove({
2492+
timer: 100,
2493+
});
2494+
// Header message
2495+
const writer = response.writable.getWriter();
2496+
await writer.write({
2497+
type: 'VaultNamesHeaderMessage',
2498+
vaultNames: [vaultIdEncoded],
2499+
});
2500+
// Set a timeout so that the method will execute after RPC timeout
2501+
setTimeout(async () => {
2502+
// Content messages
2503+
await writer.write({
2504+
type: 'SecretIdentifierMessage',
2505+
nameOrId: vaultIdEncoded,
2506+
secretName: secretName,
2507+
});
2508+
await writer.close();
2509+
// Read response
2510+
const consumeP = async () => {
2511+
for await (const _ of response.readable) {
2512+
// Consume values
2513+
}
2514+
};
2515+
await expect(consumeP()).rejects.toThrow(ErrorRPCTimedOut);
2516+
await vaultManager.withVaults([vaultId], async (vault) => {
2517+
await vault.readF(async (efs) => {
2518+
expect(await efs.exists(secretName)).toBeTruthy();
2519+
});
2520+
});
2521+
}, 150);
2522+
});
24662523
test('fails deleting vault root', async () => {
24672524
// Create secrets
24682525
const secretName = 'test-secret1';

0 commit comments

Comments
 (0)