-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathissue-multi-crash-promise.js
More file actions
136 lines (115 loc) · 3.29 KB
/
issue-multi-crash-promise.js
File metadata and controls
136 lines (115 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env node
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const path = require('path')
const { Worker, isMainThread } = require('worker_threads')
const nodeLibcurl = require(
path.join(__dirname, 'lib', 'binding', 'node_libcurl.node'),
)
function log(message) {
if (isMainThread) {
console.log(`[main thread: ${nodeLibcurl.Curl.THREAD_ID}] ${message}`)
} else {
console.log(`[worker thread: ${nodeLibcurl.Curl.THREAD_ID}] ${message}`)
}
}
async function runMultiTest(nodeLibcurl, log) {
return new Promise((resolve, reject) => {
try {
log('Testing Multi interface...')
const multi = new nodeLibcurl.Multi()
const easy = new nodeLibcurl.Easy()
easy.setOpt('URL', 'https://httpbin.org/get')
let resolveInner
const innerPromise = new Promise((res) => {
resolveInner = res
})
multi.onMessage((error, easy, errorCode) => {
log('Multi message callback called!')
console.log(error, easy, errorCode, resolveInner)
if (easy) {
const statusCode = easy.getInfo('RESPONSE_CODE')
const effectiveUrl = easy.getInfo('EFFECTIVE_URL')
log(`Multi response code: ${statusCode.data}`)
log(`Multi effective URL: ${effectiveUrl.data}`)
multi.removeHandle(easy)
easy.close()
}
resolveInner()
})
multi.addHandle(easy)
log(`Added ${multi.getCount()} handles to multi`)
log(`Performing request...`)
easy.perform()
innerPromise
.then(() => {
log(`Performed request!`)
multi.close()
resolve()
})
.catch(reject)
} catch (error) {
log(`Error in runMultiTest: ${error.message}`)
console.error(error)
reject(error)
}
})
}
async function mainWorker() {
await runMultiTest(nodeLibcurl, log)
}
async function runWorkerTest() {
return new Promise((resolve, reject) => {
const worker = new Worker(__filename, {
workerData: { testName: 'worker-test' },
})
worker.on('message', (data) => {
if (data.type === 'log') {
console.log(`[worker] ${data.message}`)
}
})
worker.on('error', (error) => {
reject(new Error(`Worker error: ${error.message}`))
})
worker.on('exit', (code) => {
if (code === 0) {
resolve({ success: true })
} else {
reject(new Error(`Worker exited with code ${code}`))
}
})
})
}
async function main() {
try {
console.log('Testing Multi interface in main thread')
await runMultiTest(nodeLibcurl, log)
console.log('✅ Multi test in main thread completed successfully!')
console.log('\nSpawning worker thread...')
await runWorkerTest()
console.log('✅ Worker thread test completed successfully!')
} catch (error) {
console.error('❌ Test failed:', error.message)
process.exit(1)
}
}
async function workerMain() {
try {
log('Worker started')
await mainWorker()
log('Worker completed successfully')
process.exit(0)
} catch (error) {
log(`Worker error: ${error.message}`)
process.exit(1)
}
}
if (isMainThread) {
main()
} else {
workerMain()
}