Skip to content

Commit

Permalink
fixin: stop.test.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
tegefaulkes committed Jul 26, 2022
1 parent 35f2e98 commit e78a1d2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
33 changes: 21 additions & 12 deletions tests/bin/agent/stop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('stop', () => {
'stop LIVE agent',
async () => {
const password = 'abc123';
const { exitCode } = await testBinUtils.pkStdioSwitch(global.testCmd)(
const agentProcess = await testBinUtils.pkSpawnSwitch(global.testCmd)(
[
'agent',
'start',
Expand All @@ -46,8 +46,8 @@ describe('stop', () => {
PK_PASSWORD: password,
},
dataDir,
logger,
);
expect(exitCode).toBe(0);
const status = new Status({
statusPath: path.join(dataDir, 'polykey', config.defaults.statusBase),
statusLockPath: path.join(
Expand All @@ -58,6 +58,7 @@ describe('stop', () => {
fs,
logger,
});
await status.waitFor('LIVE');
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'stop'],
{
Expand All @@ -67,6 +68,8 @@ describe('stop', () => {
dataDir,
);
await status.waitFor('DEAD');
await sleep(5000);
agentProcess.kill();
},
global.defaultTimeout * 2,
);
Expand All @@ -86,7 +89,7 @@ describe('stop', () => {
fs,
logger,
});
const { exitCode } = await testBinUtils.pkStdioSwitch(global.testCmd)(
const agentProcess = await testBinUtils.pkSpawnSwitch(global.testCmd)(
[
'agent',
'start',
Expand All @@ -105,8 +108,8 @@ describe('stop', () => {
PK_PASSWORD: password,
},
dataDir,
logger,
);
expect(exitCode).toBe(0);
await status.waitFor('LIVE');
// Simultaneous calls to stop must use pkExec
const [agentStop1, agentStop2] = await Promise.all([
Expand Down Expand Up @@ -156,6 +159,7 @@ describe('stop', () => {
}
expect(agentStop3.exitCode).toBe(0);
expect(agentStop4.exitCode).toBe(0);
agentProcess.kill();
},
global.defaultTimeout * 2,
);
Expand All @@ -173,7 +177,7 @@ describe('stop', () => {
fs,
logger,
});
await testBinUtils.pkSpawnSwitch(global.testCmd)(
const agentProcess = await testBinUtils.pkSpawnSwitch(global.testCmd)(
[
'agent',
'start',
Expand All @@ -196,7 +200,9 @@ describe('stop', () => {
logger,
);
await status.waitFor('STARTING');
const { exitCode, stderr } = await testBinUtils.pkStdioSwitch(global.testCmd)(
const { exitCode, stderr } = await testBinUtils.pkStdioSwitch(
global.testCmd,
)(
['agent', 'stop', '--format', 'json'],
{
PK_NODE_PATH: path.join(dataDir, 'polykey'),
Expand All @@ -216,14 +222,15 @@ describe('stop', () => {
dataDir,
);
await status.waitFor('DEAD');
agentProcess.kill();
},
global.defaultTimeout * 2,
);
runTestIfPlatforms('linux', 'docker')(
'stopping while unauthenticated does not stop',
async () => {
const password = 'abc123';
await testBinUtils.pkStdioSwitch(global.testCmd)(
const agentProcess = await testBinUtils.pkSpawnSwitch(global.testCmd)(
[
'agent',
'start',
Expand All @@ -242,6 +249,7 @@ describe('stop', () => {
PK_PASSWORD: password,
},
dataDir,
logger,
);
const status = new Status({
statusPath: path.join(dataDir, 'polykey', config.defaults.statusBase),
Expand All @@ -253,7 +261,10 @@ describe('stop', () => {
fs,
logger,
});
const { exitCode, stderr } = await testBinUtils.pkStdioSwitch(global.testCmd)(
await status.waitFor('LIVE');
const { exitCode, stderr } = await testBinUtils.pkStdioSwitch(
global.testCmd,
)(
['agent', 'stop', '--format', 'json'],
{
PK_NODE_PATH: path.join(dataDir, 'polykey'),
Expand All @@ -265,10 +276,7 @@ describe('stop', () => {
new clientErrors.ErrorClientAuthDenied(),
]);
// Should still be LIVE
await sleep(500);
const statusInfo = await status.readStatus();
expect(statusInfo).toBeDefined();
expect(statusInfo?.status).toBe('LIVE');
expect((await status.readStatus())?.status).toBe('LIVE');
await testBinUtils.pkStdioSwitch(global.testCmd)(
['agent', 'stop'],
{
Expand All @@ -278,6 +286,7 @@ describe('stop', () => {
dataDir,
);
await status.waitFor('DEAD');
agentProcess.kill();
},
global.defaultTimeout * 2,
);
Expand Down
3 changes: 2 additions & 1 deletion tests/bin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,10 @@ async function pkExecTarget(
// (if not defined in the env) to ensure no attempted connections. A regular
// PolykeyAgent is expected to initially connect to the mainnet seed nodes
env['PK_SEED_NODES'] = env['PK_SEED_NODES'] ?? '';
const command = path.resolve(path.join(global.projectDir, cmd));
return new Promise((resolve, reject) => {
child_process.execFile(
cmd,
command,
[...args],
{
env,
Expand Down

0 comments on commit e78a1d2

Please sign in to comment.