|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | | -const { describe, it } = require('mocha') |
| 3 | +const { describe, it, beforeEach, afterEach } = require('mocha') |
4 | 4 | const assert = require('assert') |
5 | 5 | const path = require('path') |
6 | 6 | const fs = require('graceful-fs') |
| 7 | +const { rm, mkdtemp } = require('fs/promises') |
7 | 8 | const os = require('os') |
8 | 9 | const cp = require('child_process') |
9 | 10 | const util = require('../lib/util') |
10 | | -const { platformTimeout } = require('./common') |
| 11 | +const { FULL_TEST, platformTimeout } = require('./common') |
11 | 12 |
|
12 | 13 | const addonPath = path.resolve(__dirname, 'node_modules', 'hello_world') |
13 | 14 | const nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js') |
@@ -129,4 +130,61 @@ describe('addon', function () { |
129 | 130 | assert.strictEqual(runHello(notNodePath), 'world') |
130 | 131 | fs.unlinkSync(notNodePath) |
131 | 132 | }) |
| 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 | + }) |
132 | 190 | }) |
0 commit comments