]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/lockfile/test/retry-time.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / lockfile / test / retry-time.js
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
11 var lockFile = require('../')
12 var touch = require('touch')
13 var test = require('tap').test
14 var fs = require('fs')
15
16 var RETRYWAIT = 100
17 var WAIT = 100
18 var RETRIES = 2
19 var EXPECTTIME = (RETRYWAIT * RETRIES) + (WAIT * (RETRIES + 1))
20 var TOOLONG = EXPECTTIME * 1.1
21
22 test('setup', function (t) {
23   touch.sync('file.lock')
24   t.end()
25 })
26
27 var pollPeriods = [10, 100, 10000]
28 pollPeriods.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     timer.unref()
37
38     var start = Date.now()
39     lockFile.lock('file.lock', {
40       wait: WAIT,
41       retries: RETRIES,
42       retryWait: RETRYWAIT,
43       pollPeriod: pp
44     }, function (er) {
45       if (ended) return
46       var time = Date.now() - start
47       console.error('t=%d', time)
48       t.ok(time >= EXPECTTIME, 'should take at least ' + EXPECTTIME)
49       t.ok(time < TOOLONG, 'should take less than ' + TOOLONG)
50       clearTimeout(timer)
51       t.end()
52     })
53   })
54 })
55
56 test('cleanup', function (t) {
57   fs.unlinkSync('file.lock')
58   t.end()
59   setTimeout(function() {
60     process.exit(1)
61   }, 500).unref()
62 })