/
unlock.test.js
199 lines (135 loc) · 4.77 KB
/
unlock.test.js
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
'use strict';
const fs = require('graceful-fs');
const mkdirp = require('mkdirp');
const rimraf = require('rimraf');
const pDelay = require('delay');
const clearTimeouts = require('@segment/clear-timeouts');
const lockfile = require('../');
const unlockAll = require('./util/unlockAll');
const tmpDir = `${__dirname}/tmp`;
clearTimeouts.install();
beforeAll(() => mkdirp.sync(tmpDir));
afterAll(() => rimraf.sync(tmpDir));
afterEach(async () => {
clearTimeouts();
await unlockAll();
rimraf.sync(`${tmpDir}/*`);
});
it('should fail if the lock is not acquired', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
expect.assertions(1);
try {
await lockfile.unlock(`${tmpDir}/foo`);
} catch (err) {
expect(err.code).toBe('ENOTACQUIRED');
}
});
it('should return a promise', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const promise = lockfile.unlock(`${tmpDir}/foo`);
expect(typeof promise.then).toBe('function');
await promise.catch(() => {});
});
it('should release the lock', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
await lockfile.lock(`${tmpDir}/foo`);
await lockfile.unlock(`${tmpDir}/foo`);
await lockfile.lock(`${tmpDir}/foo`);
});
it('should remove the lockfile', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
await lockfile.lock(`${tmpDir}/foo`);
await lockfile.unlock(`${tmpDir}/foo`);
expect(fs.existsSync(`${tmpDir}/foo.lock`)).toBe(false);
});
it('should fail if removing the lockfile errors out', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const customFs = {
...fs,
rmdir: (path, callback) => callback(new Error('foo')),
};
expect.assertions(1);
await lockfile.lock(`${tmpDir}/foo`);
try {
await lockfile.unlock(`${tmpDir}/foo`, { fs: customFs });
} catch (err) {
expect(err.message).toBe('foo');
}
});
it('should ignore ENOENT errors when removing the lockfile', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const customFs = {
...fs,
rmdir: jest.fn((path, callback) => callback(Object.assign(new Error(), { code: 'ENOENT' }))),
};
await lockfile.lock(`${tmpDir}/foo`);
await lockfile.unlock(`${tmpDir}/foo`, { fs: customFs });
expect(customFs.rmdir).toHaveBeenCalledTimes(1);
});
it('should stop updating the lockfile mtime', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const customFs = {
...fs,
utimes: jest.fn((path, atime, mtime, callback) => callback()),
};
await lockfile.lock(`${tmpDir}/foo`, { update: 2000, fs: customFs });
await lockfile.unlock(`${tmpDir}/foo`);
// First update occurs at 2000ms
await pDelay(2500);
expect(customFs.utimes).toHaveBeenCalledTimes(1);
}, 10000);
it('should stop updating the lockfile mtime (slow fs)', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const customFs = {
...fs,
utimes: jest.fn((...args) => setTimeout(() => fs.utimes(...args), 2000)),
};
await lockfile.lock(`${tmpDir}/foo`, { fs: customFs, update: 2000 });
await pDelay(3000);
await lockfile.unlock(`${tmpDir}/foo`);
await pDelay(3000);
expect(customFs.utimes).toHaveBeenCalledTimes(2);
}, 10000);
it('should stop updating the lockfile mtime (slow fs + new lock)', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
const customFs = {
...fs,
utimes: jest.fn((...args) => setTimeout(() => fs.utimes(...args), 2000)),
};
await lockfile.lock(`${tmpDir}/foo`, { fs: customFs, update: 2000 });
await pDelay(3000);
await lockfile.unlock(`${tmpDir}/foo`);
await lockfile.lock(`${tmpDir}/foo`);
await pDelay(3000);
expect(customFs.utimes).toHaveBeenCalledTimes(2);
}, 10000);
it('should resolve symlinks by default', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
fs.symlinkSync(`${tmpDir}/foo`, `${tmpDir}/bar`);
await lockfile.lock(`${tmpDir}/foo`);
await lockfile.unlock(`${tmpDir}/bar`);
expect(fs.existsSync(`${tmpDir}/foo.lock`)).toBe(false);
});
it('should not resolve symlinks if realpath is false', async () => {
fs.writeFileSync(`${tmpDir}/foo`, '');
fs.symlinkSync(`${tmpDir}/foo`, `${tmpDir}/bar`);
expect.assertions(1);
await lockfile.lock(`${tmpDir}/foo`);
try {
await lockfile.unlock(`${tmpDir}/bar`, { realpath: false });
} catch (err) {
expect(err.code).toBe('ENOTACQUIRED');
}
});
it('should use a custom fs', async () => {
const customFs = {
...fs,
realpath: (path, callback) => callback(new Error('foo')),
};
expect.assertions(1);
try {
await lockfile.unlock(`${tmpDir}/foo`, { fs: customFs });
} catch (err) {
expect(err.message).toBe('foo');
}
});