summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/lockfile/test/basic.js
diff options
context:
space:
mode:
Diffstat (limited to 'deps/npm/node_modules/lockfile/test/basic.js')
-rw-r--r--deps/npm/node_modules/lockfile/test/basic.js75
1 files changed, 39 insertions, 36 deletions
diff --git a/deps/npm/node_modules/lockfile/test/basic.js b/deps/npm/node_modules/lockfile/test/basic.js
index 23e8248796..bc66cc3531 100644
--- a/deps/npm/node_modules/lockfile/test/basic.js
+++ b/deps/npm/node_modules/lockfile/test/basic.js
@@ -4,6 +4,11 @@ var path = require('path')
var fs = require('fs')
var touch = require('touch')
+// On Unix systems, it uses ctime by default for staleness checks, since it's
+// the most reliable. However, because this test artificially sets some locks
+// to an earlier time to simulate staleness, we use mtime here.
+lockFile.filetime = 'mtime'
+
test('setup', function (t) {
try { lockFile.unlockSync('basic-lock') } catch (er) {}
try { lockFile.unlockSync('sync-lock') } catch (er) {}
@@ -127,36 +132,35 @@ test('staleness test', function (t) {
lockFile.lock('stale-lock', function (er) {
if (er) throw er
+ // simulate 2s old
+ touch.sync('stale-lock', { time: new Date(Date.now() - 2000) })
+
var opts = { stale: 1 }
- setTimeout(next, 1000)
- function next () {
- lockFile.check('stale-lock', opts, function (er, locked) {
+ lockFile.check('stale-lock', opts, function (er, locked) {
+ if (er) throw er
+ t.notOk(locked)
+ lockFile.lock('stale-lock', opts, function (er) {
if (er) throw er
- t.notOk(locked)
- lockFile.lock('stale-lock', opts, function (er) {
+ lockFile.unlock('stale-lock', function (er) {
if (er) throw er
- lockFile.unlock('stale-lock', function (er) {
- if (er) throw er
- t.end()
- })
+ t.end()
})
})
- }
+ })
})
})
test('staleness sync test', function (t) {
var opts = { stale: 1 }
lockFile.lockSync('stale-lock')
- setTimeout(next, 1000)
- function next () {
- var locked
- locked = lockFile.checkSync('stale-lock', opts)
- t.notOk(locked)
- lockFile.lockSync('stale-lock', opts)
- lockFile.unlockSync('stale-lock')
- t.end()
- }
+ // simulate 2s old
+ touch.sync('stale-lock', { time: new Date(Date.now() - 2000) })
+ var locked
+ locked = lockFile.checkSync('stale-lock', opts)
+ t.notOk(locked)
+ lockFile.lockSync('stale-lock', opts)
+ lockFile.unlockSync('stale-lock')
+ t.end()
})
test('retries', function (t) {
@@ -238,7 +242,7 @@ test('wait and stale together', function (t) {
}, 10)
// try to get another lock. this must fail!
- var opt = { stale: 1000, wait: 2000 }
+ var opt = { stale: 1000, wait: 2000, pollInterval: 1000 }
lockFile.lock('stale-wait-lock', opt, function (er) {
if (!er)
t.fail('got second lock? that unpossible!')
@@ -256,21 +260,20 @@ test('stale windows file tunneling test', function (t) {
// nt file system tunneling feature will make file creation time not updated
var opts = { stale: 1000 }
lockFile.lockSync('stale-windows-lock')
- setTimeout(next, 2000)
- function next () {
- var locked
- lockFile.unlockSync('stale-windows-lock')
- lockFile.lockSync('stale-windows-lock', opts)
- locked = lockFile.checkSync('stale-windows-lock', opts)
- t.ok(locked, "should be locked and not stale")
- lockFile.lock('stale-windows-lock', opts, function (er) {
- if (!er)
- t.fail('got second lock? impossible, windows file tunneling problem!')
- else
- t.pass('second lock failed, windows file tunneling problem fixed')
- t.end()
- })
- }
+ touch.sync('stale-windows-lock', { time: new Date(Date.now() - 3000) })
+
+ var locked
+ lockFile.unlockSync('stale-windows-lock')
+ lockFile.lockSync('stale-windows-lock', opts)
+ locked = lockFile.checkSync('stale-windows-lock', opts)
+ t.ok(locked, "should be locked and not stale")
+ lockFile.lock('stale-windows-lock', opts, function (er) {
+ if (!er)
+ t.fail('got second lock? impossible, windows file tunneling problem!')
+ else
+ t.pass('second lock failed, windows file tunneling problem fixed')
+ t.end()
+ })
})
@@ -283,7 +286,7 @@ test('cleanup', function (t) {
try { lockFile.unlockSync('retry-lock') } catch (er) {}
try { lockFile.unlockSync('contentious-lock') } catch (er) {}
try { lockFile.unlockSync('stale-wait-lock') } catch (er) {}
- try { lockFile.unlockSync('stale-windows-lock') } catch (er) {}
+ try { lockFile.unlockSync('stale-windows-lock') } catch (er) {}
t.end()
})