The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / npm / node_modules / lockfile / test / basic.js @ 5aef65a9
History | View | Annotate | Download (7.27 KB)
1 |
var test = require('tap').test |
---|---|
2 |
var lockFile = require('../lockfile.js') |
3 |
var path = require('path') |
4 |
var fs = require('fs') |
5 |
|
6 |
test('setup', function (t) { |
7 |
try { lockFile.unlockSync('basic-lock') } catch (er) {} |
8 |
try { lockFile.unlockSync('sync-lock') } catch (er) {} |
9 |
try { lockFile.unlockSync('never-forget') } catch (er) {} |
10 |
try { lockFile.unlockSync('stale-lock') } catch (er) {} |
11 |
try { lockFile.unlockSync('watch-lock') } catch (er) {} |
12 |
try { lockFile.unlockSync('retry-lock') } catch (er) {} |
13 |
try { lockFile.unlockSync('contentious-lock') } catch (er) {} |
14 |
t.end() |
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 |
|
58 |
test('basic test', function (t) { |
59 |
lockFile.check('basic-lock', function (er, locked) { |
60 |
if (er) throw er |
61 |
t.notOk(locked) |
62 |
lockFile.lock('basic-lock', function (er) { |
63 |
if (er) throw er |
64 |
lockFile.lock('basic-lock', function (er) { |
65 |
t.ok(er) |
66 |
lockFile.check('basic-lock', function (er, locked) { |
67 |
if (er) throw er |
68 |
t.ok(locked) |
69 |
lockFile.unlock('basic-lock', function (er) { |
70 |
if (er) throw er |
71 |
lockFile.check('basic-lock', function (er, locked) { |
72 |
if (er) throw er |
73 |
t.notOk(locked) |
74 |
t.end() |
75 |
}) |
76 |
}) |
77 |
}) |
78 |
}) |
79 |
}) |
80 |
}) |
81 |
}) |
82 |
|
83 |
test('sync test', function (t) { |
84 |
var locked
|
85 |
locked = lockFile.checkSync('sync-lock')
|
86 |
t.notOk(locked) |
87 |
lockFile.lockSync('sync-lock')
|
88 |
locked = lockFile.checkSync('sync-lock')
|
89 |
t.ok(locked) |
90 |
lockFile.unlockSync('sync-lock')
|
91 |
locked = lockFile.checkSync('sync-lock')
|
92 |
t.notOk(locked) |
93 |
t.end() |
94 |
}) |
95 |
|
96 |
test('exit cleanup test', function (t) { |
97 |
var child = require.resolve('./fixtures/child.js') |
98 |
var node = process.execPath
|
99 |
var spawn = require('child_process').spawn |
100 |
spawn(node, [child]).on('exit', function () { |
101 |
setTimeout(function () {
|
102 |
var locked = lockFile.checkSync('never-forget') |
103 |
t.notOk(locked) |
104 |
t.end() |
105 |
}, 100)
|
106 |
}) |
107 |
}) |
108 |
|
109 |
test('error exit cleanup test', function (t) { |
110 |
var child = require.resolve('./fixtures/bad-child.js') |
111 |
var node = process.execPath
|
112 |
var spawn = require('child_process').spawn |
113 |
spawn(node, [child]).on('exit', function () { |
114 |
setTimeout(function () {
|
115 |
var locked = lockFile.checkSync('never-forget') |
116 |
t.notOk(locked) |
117 |
t.end() |
118 |
}, 100)
|
119 |
}) |
120 |
}) |
121 |
|
122 |
|
123 |
test('staleness test', function (t) { |
124 |
lockFile.lock('stale-lock', function (er) { |
125 |
if (er) throw er |
126 |
|
127 |
var opts = { stale: 1 } |
128 |
setTimeout(next, 10)
|
129 |
function next () { |
130 |
lockFile.check('stale-lock', opts, function (er, locked) { |
131 |
if (er) throw er |
132 |
t.notOk(locked) |
133 |
lockFile.lock('stale-lock', opts, function (er) { |
134 |
if (er) throw er |
135 |
lockFile.unlock('stale-lock', function (er) { |
136 |
if (er) throw er |
137 |
t.end() |
138 |
}) |
139 |
}) |
140 |
}) |
141 |
} |
142 |
}) |
143 |
}) |
144 |
|
145 |
test('staleness sync test', function (t) { |
146 |
var opts = { stale: 1 } |
147 |
lockFile.lockSync('stale-lock')
|
148 |
setTimeout(next, 10)
|
149 |
function next () { |
150 |
var locked
|
151 |
locked = lockFile.checkSync('stale-lock', opts)
|
152 |
t.notOk(locked) |
153 |
lockFile.lockSync('stale-lock', opts)
|
154 |
lockFile.unlockSync('stale-lock')
|
155 |
t.end() |
156 |
} |
157 |
}) |
158 |
|
159 |
test('watch test', function (t) { |
160 |
var opts = { wait: 100 } |
161 |
var fdx
|
162 |
lockFile.lock('watch-lock', function (er, fd1) { |
163 |
if (er) throw er |
164 |
setTimeout(unlock, 10)
|
165 |
function unlock () { |
166 |
console.error('unlocking it')
|
167 |
lockFile.unlockSync('watch-lock')
|
168 |
// open another file, so the fd gets reused
|
169 |
// so we can know that it actually re-opened it fresh,
|
170 |
// rather than just getting the same lock as before.
|
171 |
fdx = fs.openSync('x', 'w') |
172 |
fdy = fs.openSync('x', 'w') |
173 |
} |
174 |
|
175 |
// should have gotten a new fd
|
176 |
lockFile.lock('watch-lock', opts, function (er, fd2) { |
177 |
if (er) throw er |
178 |
t.notEqual(fd1, fd2) |
179 |
fs.closeSync(fdx) |
180 |
fs.closeSync(fdy) |
181 |
fs.unlinkSync('x')
|
182 |
lockFile.unlockSync('watch-lock')
|
183 |
t.end() |
184 |
}) |
185 |
}) |
186 |
}) |
187 |
|
188 |
test('retries', function (t) { |
189 |
// next 5 opens will fail.
|
190 |
var opens = 5 |
191 |
fs._open = fs.open |
192 |
fs.open = function (path, mode, cb) { |
193 |
if (--opens === 0) { |
194 |
fs.open = fs._open |
195 |
return fs.open(path, mode, cb)
|
196 |
} |
197 |
var er = new Error('bogus') |
198 |
// to be, or not to be, that is the question.
|
199 |
er.code = opens % 2 ? 'EEXIST' : 'ENOENT' |
200 |
process.nextTick(cb.bind(null, er))
|
201 |
} |
202 |
|
203 |
lockFile.lock('retry-lock', { retries: opens }, function (er, fd) { |
204 |
if (er) throw er |
205 |
t.equal(opens, 0)
|
206 |
t.ok(fd) |
207 |
lockFile.unlockSync('retry-lock')
|
208 |
t.end() |
209 |
}) |
210 |
}) |
211 |
|
212 |
test('retryWait', function (t) { |
213 |
// next 5 opens will fail.
|
214 |
var opens = 5 |
215 |
fs._open = fs.open |
216 |
fs.open = function (path, mode, cb) { |
217 |
if (--opens === 0) { |
218 |
fs.open = fs._open |
219 |
return fs.open(path, mode, cb)
|
220 |
} |
221 |
var er = new Error('bogus') |
222 |
// to be, or not to be, that is the question.
|
223 |
er.code = opens % 2 ? 'EEXIST' : 'ENOENT' |
224 |
process.nextTick(cb.bind(null, er))
|
225 |
} |
226 |
|
227 |
var opts = { retries: opens, retryWait: 100 } |
228 |
lockFile.lock('retry-lock', opts, function (er, fd) { |
229 |
if (er) throw er |
230 |
t.equal(opens, 0)
|
231 |
t.ok(fd) |
232 |
lockFile.unlockSync('retry-lock')
|
233 |
t.end() |
234 |
}) |
235 |
}) |
236 |
|
237 |
test('retry sync', function (t) { |
238 |
// next 5 opens will fail.
|
239 |
var opens = 5 |
240 |
fs._openSync = fs.openSync |
241 |
fs.openSync = function (path, mode) { |
242 |
if (--opens === 0) { |
243 |
fs.openSync = fs._openSync |
244 |
return fs.openSync(path, mode)
|
245 |
} |
246 |
var er = new Error('bogus') |
247 |
// to be, or not to be, that is the question.
|
248 |
er.code = opens % 2 ? 'EEXIST' : 'ENOENT' |
249 |
throw er
|
250 |
} |
251 |
|
252 |
var opts = { retries: opens } |
253 |
lockFile.lockSync('retry-lock', opts)
|
254 |
t.equal(opens, 0)
|
255 |
lockFile.unlockSync('retry-lock')
|
256 |
t.end() |
257 |
}) |
258 |
|
259 |
test('cleanup', function (t) { |
260 |
try { lockFile.unlockSync('basic-lock') } catch (er) {} |
261 |
try { lockFile.unlockSync('sync-lock') } catch (er) {} |
262 |
try { lockFile.unlockSync('never-forget') } catch (er) {} |
263 |
try { lockFile.unlockSync('stale-lock') } catch (er) {} |
264 |
try { lockFile.unlockSync('watch-lock') } catch (er) {} |
265 |
try { lockFile.unlockSync('retry-lock') } catch (er) {} |
266 |
try { lockFile.unlockSync('contentious-lock') } catch (er) {} |
267 |
t.end() |
268 |
}) |
269 |
|