-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61b98f3
commit 0204e02
Showing
13 changed files
with
187 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { fork } from 'child_process'; | ||
import { join } from 'path'; | ||
|
||
describe('withLock', () => { | ||
it('should block the second call until the first one is done', async () => { | ||
let combinedOutputs = []; | ||
let a = fork(join(__dirname, './fixtures/file-lock-fixture.spec.js'), { | ||
env: { | ||
LABEL: 'a', | ||
NX_NATIVE_LOGGING: 'trace', | ||
}, | ||
stdio: 'pipe', | ||
execArgv: ['--require', 'ts-node/register'], | ||
}); | ||
|
||
// Gives a bit of time to make the outputs of the tests more predictable... | ||
// if both start at the same time, its hard to guarantee that a will get the lock before b. | ||
await new Promise((r) => setTimeout(r, 500)); | ||
|
||
let b = fork(join(__dirname, './fixtures/file-lock-fixture.spec.js'), { | ||
env: { | ||
LABEL: 'b', | ||
NX_NATIVE_LOGGING: 'trace', | ||
}, | ||
stdio: 'pipe', | ||
execArgv: ['--require', 'ts-node/register'], | ||
}); | ||
|
||
a.stdout.on('data', (data) => { | ||
combinedOutputs.push('A: ' + data.toString().trim()); | ||
}); | ||
b.stdout.on('data', (data) => { | ||
combinedOutputs.push('B: ' + data.toString().trim()); | ||
}); | ||
|
||
a.stderr.pipe(process.stderr); | ||
b.stderr.pipe(process.stderr); | ||
|
||
await Promise.all([a, b].map((p) => new Promise((r) => p.once('exit', r)))); | ||
|
||
expect(combinedOutputs).toContain('A: ran with lock'); | ||
expect(combinedOutputs).toContain('B: waited for lock'); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
packages/nx/src/native/tests/fixtures/file-lock-fixture.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const { FileLock } = require('../../native-bindings.js'); | ||
const ora = require('ora'); | ||
const tmp = require('os').tmpdir(); | ||
|
||
(async () => { | ||
const lock = new FileLock( | ||
require('path').join(tmp, 'nx-unit-tests', 'file-lock-fixture') | ||
); | ||
if (lock.locked) { | ||
const s = ora('Waiting for lock').start(); | ||
await lock.wait(); | ||
s.stop(); | ||
console.log('waited for lock'); | ||
} else { | ||
await lock.lock(); | ||
await new Promise((resolve) => setTimeout(resolve, 5000)); | ||
console.log('ran with lock'); | ||
await lock.unlock(); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.