Skip to content

Commit 92c6847

Browse files
committed
test: clean up some dtls tests
Use timers/promise setTimeout and fix a hang in a test Signed-off-by: James M Snell <[email protected]>
1 parent 728bf74 commit 92c6847

10 files changed

Lines changed: 41 additions & 50 deletions

test/parallel/test-dtls-error-queue.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
// "SSL routines::record too small".
1212

1313
import { hasCrypto, skip } from '../common/index.mjs';
14+
import { setTimeout } from 'node:timers/promises';
1415
import * as fixtures from '../common/fixtures.mjs';
1516
import assert from 'node:assert';
16-
import crypto from 'node:crypto';
17-
import dgram from 'node:dgram';
1817

1918
if (!hasCrypto) {
2019
skip('missing crypto');
@@ -24,6 +23,9 @@ if (!process.features.dtls) {
2423
skip('DTLS is not enabled');
2524
}
2625

26+
const crypto = await import('node:crypto');
27+
const dgram = await import('node:dgram');
28+
2729
const { connect, listen } = await import('node:dtls');
2830

2931
const cert = fixtures.readKey('agent1-cert.pem');
@@ -61,7 +63,7 @@ for (let i = 0; i < 32; i++) {
6163
}
6264
// Let the datagrams reach the endpoint and be processed before closing;
6365
// dgram.close() does not flush queued sends.
64-
await new Promise((resolve) => setTimeout(resolve, 200));
66+
await setTimeout(200);
6567
await new Promise((resolve) => socket.close(resolve));
6668

6769
// An unrelated crypto failure must report its own cause. Before the error

test/parallel/test-dtls-handshake-timeout.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// which has to keep its own timing.
1111

1212
import { hasCrypto, skip } from '../common/index.mjs';
13+
import { setTimeout } from 'node:timers/promises';
1314
import assert from 'node:assert';
1415

1516
if (!hasCrypto) {
@@ -96,10 +97,10 @@ function serve(options = {}) {
9697
stalled.push(client);
9798
}
9899

99-
await new Promise((resolve) => setTimeout(resolve, 150));
100+
await setTimeout(150);
100101
assert.strictEqual(Number(getDTLSEndpointState(endpoint).sessionCount), 3);
101102

102-
await new Promise((resolve) => setTimeout(resolve, 600));
103+
await setTimeout(600);
103104
assert.strictEqual(Number(getDTLSEndpointState(endpoint).sessionCount), 0);
104105

105106
// And the endpoint is usable again.
@@ -123,7 +124,7 @@ function serve(options = {}) {
123124
});
124125
await client.opened;
125126

126-
await new Promise((resolve) => setTimeout(resolve, 600));
127+
await setTimeout(600);
127128
assert.strictEqual(getDTLSSessionState(client).destroyed, false);
128129

129130
// Still usable well after the deadline would have passed.
@@ -149,7 +150,7 @@ function serve(options = {}) {
149150
const pending = Symbol('pending');
150151
const outcome = await Promise.race([
151152
client.opened.then(() => 'opened', () => 'failed'),
152-
new Promise((resolve) => setTimeout(resolve, 500, pending)),
153+
setTimeout(500, pending),
153154
]);
154155
assert.strictEqual(outcome, pending);
155156

@@ -167,7 +168,7 @@ function serve(options = {}) {
167168
const pending = Symbol('pending');
168169
const outcome = await Promise.race([
169170
client.opened.then(() => 'opened', () => 'failed'),
170-
new Promise((resolve) => setTimeout(resolve, 500, pending)),
171+
setTimeout(500, pending),
171172
]);
172173
assert.strictEqual(outcome, pending);
173174

test/parallel/test-dtls-max-sessions.mjs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// nothing used to bound how many of them a peer could accumulate.
77

88
import { hasCrypto, skip } from '../common/index.mjs';
9+
import { setTimeout } from 'node:timers/promises';
910
import * as fixtures from '../common/fixtures.mjs';
1011
import assert from 'node:assert';
1112

@@ -35,9 +36,7 @@ const ca = fixtures.readKey('ca1-cert.pem');
3536
// retransmit schedule.
3637
async function tryConnect(port, timeout = 1500) {
3738
const client = connect('127.0.0.1', port, { servername: 'agent1', ca: [ca] });
38-
const timer = new Promise((resolve) => {
39-
setTimeout(resolve, timeout).unref();
40-
});
39+
const timer = setTimeout(timeout, undefined, { ref: false });
4140
const opened = await Promise.race([
4241
client.opened.then(() => true, () => false),
4342
timer.then(() => false),
@@ -50,9 +49,7 @@ async function tryConnect(port, timeout = 1500) {
5049
async function waitForSessionCount(server, expected) {
5150
for (let i = 0; i < 100; i++) {
5251
if (getDTLSEndpointState(server).sessionCount === expected) return;
53-
await new Promise((resolve) => {
54-
setTimeout(resolve, 10).unref();
55-
});
52+
await setTimeout(10, undefined, { ref: false });
5653
}
5754
assert.strictEqual(getDTLSEndpointState(server).sessionCount, expected);
5855
}

test/parallel/test-dtls-message-listener-gate.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// drained.
1515

1616
import { hasCrypto, skip } from '../common/index.mjs';
17+
import { setTimeout } from 'node:timers/promises';
1718
import * as fixtures from '../common/fixtures.mjs';
1819
import assert from 'node:assert';
1920

@@ -49,7 +50,7 @@ await session.opened;
4950
async function waitForMessages(count) {
5051
for (let i = 0; i < 100; i++) {
5152
if (session.stats.messagesReceived >= count) return;
52-
await new Promise((resolve) => setTimeout(resolve, 20));
53+
await setTimeout(20);
5354
}
5455
assert.fail(`only ${session.stats.messagesReceived} of ${count} messages ` +
5556
'were read; data is not being drained');

test/parallel/test-dtls-opened-settles.mjs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// left anyone awaiting `opened` waiting forever, with no error and no timeout.
88

99
import { hasCrypto, skip } from '../common/index.mjs';
10+
import { setTimeout } from 'node:timers/promises';
1011
import * as fixtures from '../common/fixtures.mjs';
1112
import assert from 'node:assert';
1213

@@ -24,13 +25,18 @@ const cert = fixtures.readKey('agent1-cert.pem');
2425
const key = fixtures.readKey('agent1-key.pem');
2526
const ca = fixtures.readKey('ca1-cert.pem');
2627

28+
async function sleep(n) {
29+
await setTimeout(1000);
30+
return 'pending';
31+
}
32+
2733
// Resolves to the promise's outcome, or 'pending' if it has not settled by
2834
// the time the microtask queue and a timer have both drained. Awaiting
2935
// `opened` directly would hang the test rather than fail it.
3036
function outcome(promise) {
3137
return Promise.race([
3238
promise.then(() => 'resolved', (err) => err),
33-
new Promise((resolve) => setTimeout(() => resolve('pending'), 1000)),
39+
sleep(1000),
3440
]);
3541
}
3642

@@ -173,12 +179,12 @@ function newClient() {
173179
{
174180
const endpoint = listen(() => {}, { cert, key, host: '127.0.0.1', port: 0 });
175181
const client = connect('127.0.0.1', endpoint.address.port, {
182+
handshakeTimeout: 500,
176183
rejectUnauthorized: false,
177184
});
178185

179186
// Before the handshake can finish.
180187
endpoint.destroy();
181-
182188
await assert.rejects(client.opened, { name: 'Error' });
183189
await client.close();
184190
}

test/parallel/test-dtls-peer-x509-certificate.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import { hasCrypto, skip } from '../common/index.mjs';
1111
import * as fixtures from '../common/fixtures.mjs';
1212
import assert from 'node:assert';
13-
import { X509Certificate } from 'node:crypto';
1413

1514
if (!hasCrypto) {
1615
skip('missing crypto');
@@ -20,6 +19,7 @@ if (!process.features.dtls) {
2019
skip('DTLS is not enabled');
2120
}
2221

22+
const { X509Certificate } = await import('node:crypto');
2323
const { connect, listen } = await import('node:dtls');
2424

2525
// A leaf issued by an intermediate, so there is a real chain to walk. The

test/parallel/test-dtls-psk.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// mandatory to implement for CoAP.
88

99
import { hasCrypto, mustCall, mustNotCall, skip } from '../common/index.mjs';
10+
import { setTimeout } from 'node:timers/promises';
1011
import * as fixtures from '../common/fixtures.mjs';
1112
import assert from 'node:assert';
1213

@@ -119,7 +120,7 @@ function open(endpoint, options) {
119120
const stalled = Symbol('stalled');
120121
const outcome = await Promise.race([
121122
client.opened.then(() => 'opened', () => 'rejected'),
122-
new Promise((resolve) => setTimeout(resolve, 200, stalled)),
123+
setTimeout(200, stalled),
123124
]);
124125
assert.strictEqual(outcome, stalled);
125126

test/parallel/test-dtls-server-handshake-error.mjs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// connect and heard nothing at all.
99

1010
import { hasCrypto, mustCall, skip } from '../common/index.mjs';
11+
import { setTimeout } from 'node:timers/promises';
1112
import * as fixtures from '../common/fixtures.mjs';
1213
import assert from 'node:assert';
1314

@@ -36,18 +37,10 @@ async function within(promise, what, ms = 5000) {
3637
const late = Symbol('late');
3738
// The timer has to be cleared: left pending it holds the loop open for its
3839
// full duration after the race is already decided.
39-
let timer;
40-
try {
41-
const result = await Promise.race([
42-
promise,
43-
new Promise((resolve) => { timer = setTimeout(resolve, ms, late); }),
44-
]);
45-
assert.notStrictEqual(result, late,
46-
`${what} did not happen within ${ms}ms`);
47-
return result;
48-
} finally {
49-
clearTimeout(timer);
50-
}
40+
const result = await Promise.race([promise, setTimeout(ms, late, { ref: false })]);
41+
assert.notStrictEqual(result, late,
42+
`${what} did not happen within ${ms}ms`);
43+
return result;
5144
}
5245
const key = (name) => fixtures.readKey(name).toString();
5346
const cert = key('agent1-cert.pem');
@@ -137,7 +130,7 @@ const privateKey = key('agent1-key.pem');
137130
const pending = Symbol('pending');
138131
await Promise.race([
139132
client.opened.then(() => 'opened', () => 'failed'),
140-
new Promise((resolve) => setTimeout(resolve, 300, pending)),
133+
setTimeout(300, pending),
141134
]);
142135

143136
assert.strictEqual(Number(getDTLSEndpointState(endpoint).sessionCount), 0);
@@ -148,7 +141,7 @@ const privateKey = key('agent1-key.pem');
148141
});
149142
const outcome = await Promise.race([
150143
good.opened.then(() => 'opened', () => 'failed'),
151-
new Promise((resolve) => setTimeout(resolve, 500, pending)),
144+
setTimeout(500, pending),
152145
]);
153146
assert.notStrictEqual(outcome, pending);
154147

test/parallel/test-dtls-session-resumption.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import { hasCrypto, skip } from '../common/index.mjs';
1313
import * as fixtures from '../common/fixtures.mjs';
1414
import assert from 'node:assert';
15-
import { randomBytes } from 'node:crypto';
1615
import { inspect } from 'node:util';
1716

1817
if (!hasCrypto) {
@@ -23,8 +22,8 @@ if (!process.features.dtls) {
2322
skip('DTLS is not enabled');
2423
}
2524

26-
const dtls = await import('node:dtls');
27-
const { connect, createSecureContext, listen } = dtls;
25+
const { randomBytes } = await import('node:crypto');
26+
const { connect, createSecureContext, listen } = await import('node:dtls');
2827

2928
const key = (name) => fixtures.readKey(name).toString();
3029
const cert = key('agent1-cert.pem');

test/parallel/test-dtls-sni.mjs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// suspended DTLS handshake keeps retransmitting.
1111

1212
import { hasCrypto, mustNotCall, skip } from '../common/index.mjs';
13+
import { setTimeout } from 'node:timers/promises';
1314
import * as fixtures from '../common/fixtures.mjs';
1415
import assert from 'node:assert';
1516

@@ -30,20 +31,10 @@ const key = (name) => fixtures.readKey(name).toString();
3031
// until the runner gives up.
3132
async function within(promise, what, ms = 5000) {
3233
const late = Symbol('late');
33-
// The timer has to be cleared: left pending it holds the loop open for its
34-
// full duration after the race is already decided.
35-
let timer;
36-
try {
37-
const result = await Promise.race([
38-
promise,
39-
new Promise((resolve) => { timer = setTimeout(resolve, ms, late); }),
40-
]);
41-
assert.notStrictEqual(result, late,
42-
`${what} did not happen within ${ms}ms`);
43-
return result;
44-
} finally {
45-
clearTimeout(timer);
46-
}
34+
const result = await Promise.race([promise, setTimeout(ms, late, { ref: false })]);
35+
assert.notStrictEqual(result, late,
36+
`${what} did not happen within ${ms}ms`);
37+
return result;
4738
}
4839

4940
const agent1Cert = key('agent1-cert.pem');

0 commit comments

Comments
 (0)