Revision 5aef65a9 deps/npm/node_modules/lockfile/test/basic.js

View differences:

deps/npm/node_modules/lockfile/test/basic.js
10 10
  try { lockFile.unlockSync('stale-lock') } catch (er) {}
11 11
  try { lockFile.unlockSync('watch-lock') } catch (er) {}
12 12
  try { lockFile.unlockSync('retry-lock') } catch (er) {}
13
  try { lockFile.unlockSync('contentious-lock') } catch (er) {}
13 14
  t.end()
14 15
})
15 16

  
17
test('lock contention', function (t) {
18
  var gotlocks = 0;
19
  var N = 200
20
  var delay = 10
21
  // allow for some time for each lock acquisition and release.
22
  // note that raising N higher will mean that the overhead
23
  // increases, because we're creating more and more watchers.
24
  // irl, you should never have several hundred contenders for a
25
  // single lock, so this situation is somewhat pathological.
26
  var overhead = 200
27
  var wait = N * overhead + delay
28

  
29
  // first make it locked, so that everyone has to wait
30
  lockFile.lock('contentious-lock', function(er, lock) {
31
    t.ifError(er, 'acquiring starter')
32
    if (er) throw er;
33
    t.pass('acquired starter lock')
34
    setTimeout(function() {
35
      lockFile.unlock('contentious-lock', function (er) {
36
        t.ifError(er, 'unlocking starter')
37
        if (er) throw er
38
        t.pass('unlocked starter')
39
      })
40
    }, delay)
41
  })
42

  
43
  for (var i=0; i < N; i++)
44
    lockFile.lock('contentious-lock', { wait: wait }, function(er, lock) {
45
      if (er) throw er;
46
      lockFile.unlock('contentious-lock', function(er) {
47
        if (er) throw er
48
        gotlocks++
49
        t.pass('locked and unlocked #' + gotlocks)
50
        if (gotlocks === N) {
51
          t.pass('got all locks')
52
          t.end()
53
        }
54
      })
55
    })
56
})
57

  
16 58
test('basic test', function (t) {
17 59
  lockFile.check('basic-lock', function (er, locked) {
18 60
    if (er) throw er
......
221 263
  try { lockFile.unlockSync('stale-lock') } catch (er) {}
222 264
  try { lockFile.unlockSync('watch-lock') } catch (er) {}
223 265
  try { lockFile.unlockSync('retry-lock') } catch (er) {}
266
  try { lockFile.unlockSync('contentious-lock') } catch (er) {}
224 267
  t.end()
225 268
})
226 269

  

Also available in: Unified diff