Skip to content

Commit c00d4c1

Browse files
committed
Simplify exec keepalive test coverage
1 parent 5e95b5b commit c00d4c1

1 file changed

Lines changed: 21 additions & 53 deletions

File tree

‎src/exec_test.ts‎

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import { TerminalSize } from './terminal-size-queue.js';
1313
import { WebSocketHandler, WebSocketInterface } from './web-socket-handler.js';
1414

1515
describe('Exec', () => {
16+
const startExec = (exec: Exec, options?: ExecOptions) =>
17+
exec.exec('ns', 'pod', 'container', 'command', null, null, null, false, undefined, options);
18+
1619
describe('basic', () => {
1720
it('should correctly exec to a url', async () => {
1821
const kc = new KubeConfig();
@@ -164,8 +167,7 @@ describe('Exec', () => {
164167
let pingCount: number;
165168
let connectCount: number;
166169
let exec: Exec;
167-
const start = (options?: ExecOptions) =>
168-
exec.exec('ns', 'pod', 'container', 'command', null, null, null, false, undefined, options);
170+
const start = (options?: ExecOptions) => startExec(exec, options);
169171

170172
beforeEach((t) => {
171173
ok('mock' in t);
@@ -201,29 +203,30 @@ describe('Exec', () => {
201203
strictEqual(pingCount, 2);
202204
});
203205

204-
for (const options of [undefined, {}, { pingIntervalMs: undefined }]) {
205-
it(`keeps pings disabled for ${JSON.stringify(options)}`, async (t) => {
206+
const disabledCases: { name: string; options?: ExecOptions; socket?: object }[] = [
207+
{ name: 'omitted options' },
208+
{ name: 'empty options', options: {} },
209+
{ name: 'undefined interval', options: { pingIntervalMs: undefined } },
210+
{ name: 'missing ping', socket: { ping: undefined } },
211+
{ name: 'non-function on', socket: { on: true } },
212+
{ name: 'missing removeListener', socket: { removeListener: undefined } },
213+
{ name: 'connecting socket', socket: { readyState: WebSocket.CONNECTING } },
214+
{ name: 'closing socket', socket: { readyState: WebSocket.CLOSING } },
215+
{ name: 'closed socket', socket: { readyState: WebSocket.CLOSED } },
216+
];
217+
for (const { name, options, socket } of disabledCases) {
218+
it(`keeps pings disabled for ${name}`, async (t) => {
219+
Object.assign(conn, socket);
206220
const interval = t.mock.method(globalThis, 'setInterval');
207-
await start(options);
221+
await start(socket ? { pingIntervalMs: 10 } : options);
208222
t.mock.timers.tick(100);
209223
strictEqual(pingCount, 0);
210224
strictEqual(interval.mock.callCount(), 0);
211225
deepStrictEqual(conn.eventNames(), []);
212226
});
213227
}
214228

215-
for (const value of [
216-
0,
217-
-1,
218-
1.5,
219-
NaN,
220-
Infinity,
221-
-Infinity,
222-
2147483648,
223-
Number.MAX_SAFE_INTEGER,
224-
'10',
225-
null,
226-
]) {
229+
for (const value of [0, -1, 1.5, NaN, Infinity, 2147483648, '10', null]) {
227230
it(`rejects invalid interval ${String(value)} before connecting`, async () => {
228231
await rejects(
229232
start({ pingIntervalMs: value as number }),
@@ -243,18 +246,6 @@ describe('Exec', () => {
243246
});
244247
}
245248

246-
for (const method of ['ping', 'on', 'removeListener']) {
247-
for (const value of [undefined, null, true]) {
248-
it(`does not start a timer when ${method} is ${String(value)}`, async (t) => {
249-
Object.defineProperty(conn, method, { value });
250-
const interval = t.mock.method(globalThis, 'setInterval');
251-
await start({ pingIntervalMs: 10 });
252-
strictEqual(interval.mock.callCount(), 0);
253-
deepStrictEqual(conn.eventNames(), []);
254-
});
255-
}
256-
}
257-
258249
for (const event of ['close', 'error']) {
259250
it(`cleans up its timer and listeners on ${event}`, async (t) => {
260251
const externalListener = () => {};
@@ -278,16 +269,6 @@ describe('Exec', () => {
278269
});
279270
}
280271

281-
for (const readyState of [WebSocket.CONNECTING, WebSocket.CLOSING, WebSocket.CLOSED]) {
282-
it(`does not start keepalive if already in state ${readyState}`, async (t) => {
283-
conn.readyState = readyState;
284-
const interval = t.mock.method(globalThis, 'setInterval');
285-
await start({ pingIntervalMs: 10 });
286-
strictEqual(interval.mock.callCount(), 0);
287-
deepStrictEqual(conn.eventNames(), []);
288-
});
289-
}
290-
291272
it('cleans up if the socket stops being open without a close event', async (t) => {
292273
await start({ pingIntervalMs: 10 });
293274
conn.readyState = WebSocket.CLOSING;
@@ -354,20 +335,7 @@ describe('Exec', () => {
354335
return client;
355336
},
356337
});
357-
const conn = await exec.exec(
358-
'ns',
359-
'pod',
360-
'container',
361-
'command',
362-
null,
363-
null,
364-
null,
365-
false,
366-
undefined,
367-
{
368-
pingIntervalMs: 10,
369-
},
370-
);
338+
const conn = await startExec(exec, { pingIntervalMs: 10 });
371339
await once(conn, 'pong');
372340
await once(conn, 'pong');
373341
strictEqual(pingCount, 2);

0 commit comments

Comments
 (0)