Skip to content

Commit ea8493f

Browse files
committed
Added timeouts to bootstrapping
1 parent fd2236a commit ea8493f

File tree

1 file changed

+121
-113
lines changed

1 file changed

+121
-113
lines changed

tests/bin/bootstrap.test.ts

Lines changed: 121 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -51,130 +51,138 @@ describe('bootstrap', () => {
5151
recoveryCode.split(' ').length === 24,
5252
).toBe(true);
5353
},
54-
global.defaultTimeout,
54+
global.defaultTimeout * 2,
5555
);
56-
test('bootstrapping occupied node state', async () => {
57-
const password = 'password';
58-
await fs.promises.mkdir(path.join(dataDir, 'polykey'));
59-
await fs.promises.writeFile(path.join(dataDir, 'polykey', 'test'), '');
60-
let exitCode, stdout, stderr;
61-
({ exitCode, stdout, stderr } = await testBinUtils.pkStdio(
62-
[
63-
'bootstrap',
64-
'--node-path',
65-
path.join(dataDir, 'polykey'),
66-
'--root-key-pair-bits',
67-
'1024',
68-
'--verbose',
69-
],
70-
{
71-
PK_PASSWORD: password,
72-
},
73-
dataDir,
74-
));
75-
const errorBootstrapExistingState =
76-
new bootstrapErrors.ErrorBootstrapExistingState();
77-
expect(exitCode).toBe(errorBootstrapExistingState.exitCode);
78-
const stdErrLine = stderr.trim().split('\n').pop();
79-
const eOutput = binUtils
80-
.outputFormatter({
81-
type: 'error',
82-
name: errorBootstrapExistingState.name,
83-
description: errorBootstrapExistingState.description,
84-
message: errorBootstrapExistingState.message,
85-
})
86-
.trim();
87-
expect(stdErrLine).toBe(eOutput);
88-
({ exitCode, stdout, stderr } = await testBinUtils.pkStdio(
89-
[
90-
'bootstrap',
91-
'--node-path',
92-
path.join(dataDir, 'polykey'),
93-
'--root-key-pair-bits',
94-
'1024',
95-
'--fresh',
96-
'--verbose',
97-
],
98-
{
99-
PK_PASSWORD: password,
100-
},
101-
dataDir,
102-
));
103-
expect(exitCode).toBe(0);
104-
const recoveryCode = stdout.trim();
105-
expect(
106-
recoveryCode.split(' ').length === 12 ||
107-
recoveryCode.split(' ').length === 24,
108-
).toBe(true);
109-
});
110-
test('concurrent bootstrapping are coalesced', async () => {
111-
const password = 'password';
112-
const [bootstrapProcess1, bootstrapProcess2] = await Promise.all([
113-
testBinUtils.pkSpawn(
114-
['bootstrap', '--root-key-pair-bits', '1024', '--verbose'],
56+
test(
57+
'bootstrapping occupied node state',
58+
async () => {
59+
const password = 'password';
60+
await fs.promises.mkdir(path.join(dataDir, 'polykey'));
61+
await fs.promises.writeFile(path.join(dataDir, 'polykey', 'test'), '');
62+
let exitCode, stdout, stderr;
63+
({ exitCode, stdout, stderr } = await testBinUtils.pkStdio(
64+
[
65+
'bootstrap',
66+
'--node-path',
67+
path.join(dataDir, 'polykey'),
68+
'--root-key-pair-bits',
69+
'1024',
70+
'--verbose',
71+
],
11572
{
116-
PK_NODE_PATH: path.join(dataDir, 'polykey'),
11773
PK_PASSWORD: password,
11874
},
11975
dataDir,
120-
logger.getChild('bootstrapProcess1'),
121-
),
122-
testBinUtils.pkSpawn(
123-
['bootstrap', '--root-key-pair-bits', '1024', '--verbose'],
76+
));
77+
const errorBootstrapExistingState =
78+
new bootstrapErrors.ErrorBootstrapExistingState();
79+
expect(exitCode).toBe(errorBootstrapExistingState.exitCode);
80+
const stdErrLine = stderr.trim().split('\n').pop();
81+
const eOutput = binUtils
82+
.outputFormatter({
83+
type: 'error',
84+
name: errorBootstrapExistingState.name,
85+
description: errorBootstrapExistingState.description,
86+
message: errorBootstrapExistingState.message,
87+
})
88+
.trim();
89+
expect(stdErrLine).toBe(eOutput);
90+
({ exitCode, stdout, stderr } = await testBinUtils.pkStdio(
91+
[
92+
'bootstrap',
93+
'--node-path',
94+
path.join(dataDir, 'polykey'),
95+
'--root-key-pair-bits',
96+
'1024',
97+
'--fresh',
98+
'--verbose',
99+
],
124100
{
125-
PK_NODE_PATH: path.join(dataDir, 'polykey'),
126101
PK_PASSWORD: password,
127102
},
128103
dataDir,
129-
logger.getChild('bootstrapProcess2'),
130-
),
131-
]);
132-
// These will be the last line of STDERR
133-
// The readline library will automatically trim off newlines
134-
let stdErrLine1;
135-
let stdErrLine2;
136-
const rlErr1 = readline.createInterface(bootstrapProcess1.stderr!);
137-
const rlErr2 = readline.createInterface(bootstrapProcess2.stderr!);
138-
rlErr1.on('line', (l) => {
139-
stdErrLine1 = l;
140-
});
141-
rlErr2.on('line', (l) => {
142-
stdErrLine2 = l;
143-
});
144-
const [index, exitCode, signal] = await new Promise<
145-
[number, number | null, NodeJS.Signals | null]
146-
>((resolve) => {
147-
bootstrapProcess1.once('exit', (code, signal) => {
148-
resolve([0, code, signal]);
104+
));
105+
expect(exitCode).toBe(0);
106+
const recoveryCode = stdout.trim();
107+
expect(
108+
recoveryCode.split(' ').length === 12 ||
109+
recoveryCode.split(' ').length === 24,
110+
).toBe(true);
111+
},
112+
global.defaultTimeout * 2,
113+
);
114+
test(
115+
'concurrent bootstrapping are coalesced',
116+
async () => {
117+
const password = 'password';
118+
const [bootstrapProcess1, bootstrapProcess2] = await Promise.all([
119+
testBinUtils.pkSpawn(
120+
['bootstrap', '--root-key-pair-bits', '1024', '--verbose'],
121+
{
122+
PK_NODE_PATH: path.join(dataDir, 'polykey'),
123+
PK_PASSWORD: password,
124+
},
125+
dataDir,
126+
logger.getChild('bootstrapProcess1'),
127+
),
128+
testBinUtils.pkSpawn(
129+
['bootstrap', '--root-key-pair-bits', '1024', '--verbose'],
130+
{
131+
PK_NODE_PATH: path.join(dataDir, 'polykey'),
132+
PK_PASSWORD: password,
133+
},
134+
dataDir,
135+
logger.getChild('bootstrapProcess2'),
136+
),
137+
]);
138+
// These will be the last line of STDERR
139+
// The readline library will automatically trim off newlines
140+
let stdErrLine1;
141+
let stdErrLine2;
142+
const rlErr1 = readline.createInterface(bootstrapProcess1.stderr!);
143+
const rlErr2 = readline.createInterface(bootstrapProcess2.stderr!);
144+
rlErr1.on('line', (l) => {
145+
stdErrLine1 = l;
149146
});
150-
bootstrapProcess2.once('exit', (code, signal) => {
151-
resolve([1, code, signal]);
147+
rlErr2.on('line', (l) => {
148+
stdErrLine2 = l;
152149
});
153-
});
154-
const errorStatusLocked = new statusErrors.ErrorStatusLocked();
155-
expect(exitCode).toBe(errorStatusLocked.exitCode);
156-
expect(signal).toBe(null);
157-
const eOutput = binUtils
158-
.outputFormatter({
159-
type: 'error',
160-
name: errorStatusLocked.name,
161-
description: errorStatusLocked.description,
162-
message: errorStatusLocked.message,
163-
})
164-
.trim();
165-
// It's either the first or second process
166-
if (index === 0) {
167-
expect(stdErrLine1).toBeDefined();
168-
expect(stdErrLine1).toBe(eOutput);
169-
const [exitCode] = await testBinUtils.processExit(bootstrapProcess2);
170-
expect(exitCode).toBe(0);
171-
} else if (index === 1) {
172-
expect(stdErrLine2).toBeDefined();
173-
expect(stdErrLine2).toBe(eOutput);
174-
const [exitCode] = await testBinUtils.processExit(bootstrapProcess1);
175-
expect(exitCode).toBe(0);
176-
}
177-
});
150+
const [index, exitCode, signal] = await new Promise<
151+
[number, number | null, NodeJS.Signals | null]
152+
>((resolve) => {
153+
bootstrapProcess1.once('exit', (code, signal) => {
154+
resolve([0, code, signal]);
155+
});
156+
bootstrapProcess2.once('exit', (code, signal) => {
157+
resolve([1, code, signal]);
158+
});
159+
});
160+
const errorStatusLocked = new statusErrors.ErrorStatusLocked();
161+
expect(exitCode).toBe(errorStatusLocked.exitCode);
162+
expect(signal).toBe(null);
163+
const eOutput = binUtils
164+
.outputFormatter({
165+
type: 'error',
166+
name: errorStatusLocked.name,
167+
description: errorStatusLocked.description,
168+
message: errorStatusLocked.message,
169+
})
170+
.trim();
171+
// It's either the first or second process
172+
if (index === 0) {
173+
expect(stdErrLine1).toBeDefined();
174+
expect(stdErrLine1).toBe(eOutput);
175+
const [exitCode] = await testBinUtils.processExit(bootstrapProcess2);
176+
expect(exitCode).toBe(0);
177+
} else if (index === 1) {
178+
expect(stdErrLine2).toBeDefined();
179+
expect(stdErrLine2).toBe(eOutput);
180+
const [exitCode] = await testBinUtils.processExit(bootstrapProcess1);
181+
expect(exitCode).toBe(0);
182+
}
183+
},
184+
global.defaultTimeout * 2,
185+
);
178186
test(
179187
'bootstrap when interrupted, requires fresh on next bootstrap',
180188
async () => {

0 commit comments

Comments
 (0)