Skip to content

Commit dad0d06

Browse files
committed
test: add a test for parallel rebuilds
Assisted-by: Claude Opus 4.6 Signed-off-by: David Sanders <dsanders11@ucsbalum.com>
1 parent 310887b commit dad0d06

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

test/test-addon.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict'
22

3-
const { describe, it } = require('mocha')
3+
const { describe, it, beforeEach, afterEach } = require('mocha')
44
const assert = require('assert')
55
const path = require('path')
66
const fs = require('graceful-fs')
7+
const { rm, mkdtemp } = require('fs/promises')
78
const os = require('os')
89
const cp = require('child_process')
910
const util = require('../lib/util')
10-
const { platformTimeout } = require('./common')
11+
const { FULL_TEST, platformTimeout } = require('./common')
1112

1213
const addonPath = path.resolve(__dirname, 'node_modules', 'hello_world')
1314
const nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')
@@ -129,4 +130,61 @@ describe('addon', function () {
129130
assert.strictEqual(runHello(notNodePath), 'world')
130131
fs.unlinkSync(notNodePath)
131132
})
133+
134+
describe('parallel', function () {
135+
let devDir
136+
let addonCopiesDir
137+
138+
beforeEach(async () => {
139+
devDir = await mkdtemp(path.join(os.tmpdir(), 'node-gyp-test-'))
140+
addonCopiesDir = await mkdtemp(path.join(os.tmpdir(), 'node-gyp-test-addons-'))
141+
})
142+
143+
afterEach(async () => {
144+
await Promise.all([
145+
rm(devDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 1000 }),
146+
rm(addonCopiesDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 1000 })
147+
])
148+
devDir = null
149+
addonCopiesDir = null
150+
})
151+
152+
const runIt = (name, fn) => {
153+
if (!FULL_TEST) {
154+
return it.skip('Skipping parallel rebuild test due to test environment configuration')
155+
}
156+
157+
if (process.platform === 'darwin' && process.arch === 'x64') {
158+
return it.skip('Skipping parallel rebuild test on x64 macOS')
159+
}
160+
161+
return it(name, async function () {
162+
this.timeout(platformTimeout(4, { win32: 20 }))
163+
await fn.call(this)
164+
})
165+
}
166+
167+
runIt('parallel rebuild', async function () {
168+
// Install dependencies (nan) so copies in temp directories can resolve them
169+
const [npmErr] = await util.execFile('npm', ['install', '--ignore-scripts'], { cwd: addonPath, shell: process.platform === 'win32' })
170+
assert.strictEqual(npmErr, null)
171+
172+
const copies = await Promise.all(new Array(5).fill(0).map(async (_, i) => {
173+
const copyDir = path.join(addonCopiesDir, `hello_world_${i}`)
174+
await fs.promises.cp(addonPath, copyDir, { recursive: true })
175+
return copyDir
176+
}))
177+
await Promise.all(copies.map(async (copyDir, i) => {
178+
const cmd = [nodeGyp, 'rebuild', '-C', copyDir, '--loglevel=verbose', `--devdir=${devDir}`]
179+
const title = `${' '.repeat(8)}parallel rebuild ${(i + 1).toString().padEnd(2, ' ')}`
180+
console.log(`${title} : Start`)
181+
console.time(title)
182+
const [err, logLines] = await execFile(cmd)
183+
console.timeEnd(title)
184+
const lastLine = logLines[logLines.length - 1]
185+
assert.strictEqual(err, null)
186+
assert.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
187+
}))
188+
})
189+
})
132190
})

0 commit comments

Comments
 (0)