Skip to content

Commit d2902fe

Browse files
SUZUKI Sosukecrysmags
authored andcommitted
feat: port async_hooks.js tests to node:test runner (nodejs#2568)
1 parent 2e7b7ef commit d2902fe

1 file changed

Lines changed: 55 additions & 48 deletions

File tree

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
'use strict'
22

3-
const { test } = require('tap')
4-
const { Client } = require('..')
3+
const { test } = require('node:test')
4+
const { Client } = require('../..')
55
const { createServer } = require('http')
66
const { createHook, executionAsyncId } = require('async_hooks')
77
const { readFile } = require('fs')
88
const { PassThrough } = require('stream')
9+
const { tspl } = require('@matteo.collina/tspl')
910

1011
const transactions = new Map()
1112

@@ -32,13 +33,13 @@ const hook = createHook({
3233

3334
hook.enable()
3435

35-
test('async hooks', (t) => {
36-
t.plan(31)
36+
test('async hooks', async (t) => {
37+
const p = tspl(t, { plan: 31 })
3738

3839
const server = createServer((req, res) => {
3940
res.setHeader('content-type', 'text/plain')
4041
readFile(__filename, (err, buf) => {
41-
t.error(err)
42+
p.ifError(err)
4243
const buf1 = buf.slice(0, buf.length / 2)
4344
const buf2 = buf.slice(buf.length / 2)
4445
// we split the file so that it's received in 2 chunks
@@ -49,158 +50,164 @@ test('async hooks', (t) => {
4950
}, 10)
5051
})
5152
})
52-
t.teardown(server.close.bind(server))
53+
t.after(() => server.close.bind(server)())
5354

5455
server.listen(0, () => {
5556
const client = new Client(`http://localhost:${server.address().port}`)
56-
t.teardown(client.destroy.bind(client))
57+
t.after(() => client.destroy.bind(client)())
5758

5859
client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
59-
t.error(err)
60+
p.ifError(err)
6061
body.resume()
61-
t.strictSame(getCurrentTransaction(), null)
62+
p.deepStrictEqual(getCurrentTransaction(), null)
6263

6364
setCurrentTransaction({ hello: 'world2' })
6465

6566
client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
66-
t.error(err)
67-
t.strictSame(getCurrentTransaction(), { hello: 'world2' })
67+
p.ifError(err)
68+
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })
6869

6970
body.once('data', () => {
70-
t.pass()
71+
p.ok(1, 1)
7172
body.resume()
7273
})
7374

7475
body.on('end', () => {
75-
t.pass()
76+
p.ok(1, 1)
7677
})
7778
})
7879
})
7980

8081
client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
81-
t.error(err)
82+
p.ifError(err)
8283
body.resume()
83-
t.strictSame(getCurrentTransaction(), null)
84+
p.deepStrictEqual(getCurrentTransaction(), null)
8485

8586
setCurrentTransaction({ hello: 'world' })
8687

8788
client.request({ path: '/', method: 'GET' }, (err, { statusCode, headers, body }) => {
88-
t.error(err)
89-
t.strictSame(getCurrentTransaction(), { hello: 'world' })
89+
p.ifError(err)
90+
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
9091

9192
body.once('data', () => {
92-
t.pass()
93+
p.ok(1)
9394
body.resume()
9495
})
9596

9697
body.on('end', () => {
97-
t.pass()
98+
p.ok(1)
9899
})
99100
})
100101
})
101102

102103
client.request({ path: '/', method: 'HEAD' }, (err, { statusCode, headers, body }) => {
103-
t.error(err)
104+
p.ifError(err)
104105
body.resume()
105-
t.strictSame(getCurrentTransaction(), null)
106+
p.deepStrictEqual(getCurrentTransaction(), null)
106107

107108
setCurrentTransaction({ hello: 'world' })
108109

109110
client.request({ path: '/', method: 'HEAD' }, (err, { statusCode, headers, body }) => {
110-
t.error(err)
111-
t.strictSame(getCurrentTransaction(), { hello: 'world' })
111+
p.ifError(err)
112+
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
112113

113114
body.once('data', () => {
114-
t.pass()
115+
p.ok(1)
115116
body.resume()
116117
})
117118

118119
body.on('end', () => {
119-
t.pass()
120+
p.ok(1)
120121
})
121122
})
122123
})
123124

124125
client.stream({ path: '/', method: 'GET' }, () => {
125-
t.strictSame(getCurrentTransaction(), null)
126+
p.strictEqual(getCurrentTransaction(), null)
126127
return new PassThrough().resume()
127128
}, (err) => {
128-
t.error(err)
129-
t.strictSame(getCurrentTransaction(), null)
129+
p.ifError(err)
130+
p.deepStrictEqual(getCurrentTransaction(), null)
130131

131132
setCurrentTransaction({ hello: 'world' })
132133

133134
client.stream({ path: '/', method: 'GET' }, () => {
134-
t.strictSame(getCurrentTransaction(), { hello: 'world' })
135+
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
135136
return new PassThrough().resume()
136137
}, (err) => {
137-
t.error(err)
138-
t.strictSame(getCurrentTransaction(), { hello: 'world' })
138+
p.ifError(err)
139+
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world' })
139140
})
140141
})
141142
})
143+
144+
await p.completed
142145
})
143146

144-
test('async hooks client is destroyed', (t) => {
145-
t.plan(7)
147+
test('async hooks client is destroyed', async (t) => {
148+
const p = tspl(t, { plan: 7 })
146149

147150
const server = createServer((req, res) => {
148151
res.setHeader('content-type', 'text/plain')
149152
readFile(__filename, (err, buf) => {
150-
t.error(err)
153+
p.ifError(err)
151154
res.write('asd')
152155
})
153156
})
154-
t.teardown(server.close.bind(server))
157+
t.after(server.close.bind(server))
155158

156159
server.listen(0, () => {
157160
const client = new Client(`http://localhost:${server.address().port}`)
158-
t.teardown(client.destroy.bind(client))
161+
t.after(client.destroy.bind(client))
159162

160163
client.request({ path: '/', method: 'GET', throwOnError: true }, (err, { body }) => {
161-
t.error(err)
164+
p.ifError(err)
162165
body.resume()
163166
body.on('error', (err) => {
164-
t.ok(err)
167+
p.ok(err)
165168
})
166-
t.strictSame(getCurrentTransaction(), null)
169+
p.deepStrictEqual(getCurrentTransaction(), null)
167170

168171
setCurrentTransaction({ hello: 'world2' })
169172

170173
client.request({ path: '/', method: 'GET' }, (err) => {
171-
t.equal(err.message, 'The client is destroyed')
172-
t.strictSame(getCurrentTransaction(), { hello: 'world2' })
174+
p.strictEqual(err.message, 'The client is destroyed')
175+
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })
173176
})
174177
client.destroy((err) => {
175-
t.error(err)
178+
p.ifError(err)
176179
})
177180
})
178181
})
182+
183+
await p.completed
179184
})
180185

181-
test('async hooks pipeline handler', (t) => {
182-
t.plan(2)
186+
test('async hooks pipeline handler', async (t) => {
187+
const p = tspl(t, { plan: 2 })
183188

184189
const server = createServer((req, res) => {
185190
res.end('hello')
186191
})
187-
t.teardown(server.close.bind(server))
192+
t.after(server.close.bind(server))
188193

189194
server.listen(0, () => {
190195
const client = new Client(`http://localhost:${server.address().port}`)
191-
t.teardown(client.close.bind(client))
196+
t.after(() => client.close.bind(client)())
192197

193198
setCurrentTransaction({ hello: 'world2' })
194199

195200
client
196201
.pipeline({ path: '/', method: 'GET' }, ({ body }) => {
197-
t.strictSame(getCurrentTransaction(), { hello: 'world2' })
202+
p.deepStrictEqual(getCurrentTransaction(), { hello: 'world2' })
198203
return body
199204
})
200205
.on('close', () => {
201-
t.pass()
206+
p.ok(1)
202207
})
203208
.resume()
204209
.end()
205210
})
211+
212+
await p.completed
206213
})

0 commit comments

Comments
 (0)