• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// In these tests, we do the following:
2// try for 200ms (rt=2)
3// wait for 300ms
4// try for 200ms (rt=1)
5// wait for 300ms
6// try for 200ms (rt=0)
7// fail after 1200
8// Actual time will be more like 1220-ish for setTimeout irregularity
9// But it should NOT be as slow as 2000.
10
11var lockFile = require('../')
12var touch = require('touch')
13var test = require('tap').test
14var fs = require('fs')
15
16var RETRYWAIT = 100
17var WAIT = 100
18var RETRIES = 2
19var EXPECTTIME = (RETRYWAIT * RETRIES) + (WAIT * (RETRIES + 1))
20var TOOLONG = EXPECTTIME * 1.5
21
22test('setup', function (t) {
23  touch.sync('file.lock')
24  t.end()
25})
26
27var pollPeriods = [10, 100, 10000]
28pollPeriods.forEach(function (pp) {
29  test('retry+wait, poll=' + pp, function (t) {
30    var ended = false
31    var timer = setTimeout(function() {
32      t.fail('taking too long!')
33      ended = true
34      t.end()
35    }, 2000)
36
37    if (timer.unref)
38      timer.unref()
39
40    var start = Date.now()
41    lockFile.lock('file.lock', {
42      wait: WAIT,
43      retries: RETRIES,
44      retryWait: RETRYWAIT,
45      pollPeriod: pp
46    }, function (er) {
47      if (ended) return
48      var time = Date.now() - start
49      t.ok(time >= EXPECTTIME, 'should take at least ' + EXPECTTIME)
50      t.ok(time < TOOLONG, 'should take less than ' + TOOLONG)
51      clearTimeout(timer)
52      t.end()
53    })
54  })
55})
56
57test('cleanup', function (t) {
58  fs.unlinkSync('file.lock')
59  t.end()
60  var timer = setTimeout(function() {
61    process.exit(1)
62  }, 500)
63  if (timer.unref)
64    timer.unref()
65  else
66    clearTimeout(timer)
67})
68