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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
var test = require('tap').test
var lockFile = require('../lockfile.js')
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) {}
try { lockFile.unlockSync('never-forget') } catch (er) {}
try { lockFile.unlockSync('stale-lock') } catch (er) {}
try { lockFile.unlockSync('watch-lock') } catch (er) {}
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) {}
t.end()
})
test('lock contention', function (t) {
var gotlocks = 0;
var N = 200
var delay = 10
// allow for some time for each lock acquisition and release.
// note that raising N higher will mean that the overhead
// increases, because we're creating more and more watchers.
// irl, you should never have several hundred contenders for a
// single lock, so this situation is somewhat pathological.
var overhead = 200
var wait = N * overhead + delay
// first make it locked, so that everyone has to wait
lockFile.lock('contentious-lock', function(er, lock) {
t.ifError(er, 'acquiring starter')
if (er) throw er;
t.pass('acquired starter lock')
setTimeout(function() {
lockFile.unlock('contentious-lock', function (er) {
t.ifError(er, 'unlocking starter')
if (er) throw er
t.pass('unlocked starter')
})
}, delay)
})
for (var i=0; i < N; i++)
lockFile.lock('contentious-lock', { wait: wait }, function(er, lock) {
if (er) throw er;
lockFile.unlock('contentious-lock', function(er) {
if (er) throw er
gotlocks++
t.pass('locked and unlocked #' + gotlocks)
if (gotlocks === N) {
t.pass('got all locks')
t.end()
}
})
})
})
test('basic test', function (t) {
lockFile.check('basic-lock', function (er, locked) {
if (er) throw er
t.notOk(locked)
lockFile.lock('basic-lock', function (er) {
if (er) throw er
lockFile.lock('basic-lock', function (er) {
t.ok(er)
lockFile.check('basic-lock', function (er, locked) {
if (er) throw er
t.ok(locked)
lockFile.unlock('basic-lock', function (er) {
if (er) throw er
lockFile.check('basic-lock', function (er, locked) {
if (er) throw er
t.notOk(locked)
t.end()
})
})
})
})
})
})
})
test('sync test', function (t) {
var locked
locked = lockFile.checkSync('sync-lock')
t.notOk(locked)
lockFile.lockSync('sync-lock')
locked = lockFile.checkSync('sync-lock')
t.ok(locked)
lockFile.unlockSync('sync-lock')
locked = lockFile.checkSync('sync-lock')
t.notOk(locked)
t.end()
})
test('exit cleanup test', function (t) {
var child = require.resolve('./fixtures/child.js')
var node = process.execPath
var spawn = require('child_process').spawn
spawn(node, [child]).on('exit', function () {
setTimeout(function () {
var locked = lockFile.checkSync('never-forget')
t.notOk(locked)
t.end()
}, 100)
})
})
test('error exit cleanup test', function (t) {
var child = require.resolve('./fixtures/bad-child.js')
var node = process.execPath
var spawn = require('child_process').spawn
spawn(node, [child]).on('exit', function () {
setTimeout(function () {
var locked = lockFile.checkSync('never-forget')
t.notOk(locked)
t.end()
}, 100)
})
})
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 }
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
lockFile.unlock('stale-lock', function (er) {
if (er) throw er
t.end()
})
})
})
})
})
test('staleness sync test', function (t) {
var opts = { stale: 1 }
lockFile.lockSync('stale-lock')
// 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) {
// next 5 opens will fail.
var opens = 5
fs._open = fs.open
fs.open = function (path, mode, cb) {
if (--opens === 0) {
fs.open = fs._open
return fs.open(path, mode, cb)
}
var er = new Error('bogus')
// to be, or not to be, that is the question.
er.code = opens % 2 ? 'EEXIST' : 'ENOENT'
process.nextTick(cb.bind(null, er))
}
lockFile.lock('retry-lock', { retries: opens }, function (er) {
if (er) throw er
t.equal(opens, 0)
lockFile.unlockSync('retry-lock')
t.end()
})
})
test('retryWait', function (t) {
// next 5 opens will fail.
var opens = 5
fs._open = fs.open
fs.open = function (path, mode, cb) {
if (--opens === 0) {
fs.open = fs._open
return fs.open(path, mode, cb)
}
var er = new Error('bogus')
// to be, or not to be, that is the question.
er.code = opens % 2 ? 'EEXIST' : 'ENOENT'
process.nextTick(cb.bind(null, er))
}
var opts = { retries: opens, retryWait: 100 }
lockFile.lock('retry-lock', opts, function (er) {
if (er) throw er
t.equal(opens, 0)
lockFile.unlockSync('retry-lock')
t.end()
})
})
test('retry sync', function (t) {
// next 5 opens will fail.
var opens = 5
fs._openSync = fs.openSync
fs.openSync = function (path, mode) {
if (--opens === 0) {
fs.openSync = fs._openSync
return fs.openSync(path, mode)
}
var er = new Error('bogus')
// to be, or not to be, that is the question.
er.code = opens % 2 ? 'EEXIST' : 'ENOENT'
throw er
}
var opts = { retries: opens }
lockFile.lockSync('retry-lock', opts)
t.equal(opens, 0)
lockFile.unlockSync('retry-lock')
t.end()
})
test('wait and stale together', function (t) {
// first locker.
var interval
lockFile.lock('stale-wait-lock', function(er) {
// keep refreshing the lock, so we keep it forever
interval = setInterval(function() {
touch.sync('stale-wait-lock')
}, 10)
// try to get another lock. this must fail!
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!')
else
t.pass('second lock failed, as i have foreseen it')
clearInterval(interval)
t.end()
})
})
})
test('stale windows file tunneling test', function (t) {
// for windows only
// nt file system tunneling feature will make file creation time not updated
var opts = { stale: 1000 }
lockFile.lockSync('stale-windows-lock')
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()
})
})
test('cleanup', function (t) {
try { lockFile.unlockSync('basic-lock') } catch (er) {}
try { lockFile.unlockSync('sync-lock') } catch (er) {}
try { lockFile.unlockSync('never-forget') } catch (er) {}
try { lockFile.unlockSync('stale-lock') } catch (er) {}
try { lockFile.unlockSync('watch-lock') } catch (er) {}
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) {}
t.end()
})
|