Debian Codegraph @

test/fixtures/stress.js

raw
1 'use strict';
2
3 const cluster = require('cluster');
4 const fs = require('fs');
5 const os = require('os');
6 const pDelay = require('delay');
7 const sort = require('stable');
8 const lockfile = require('../../');
9
10 const tmpDir = `${__dirname}/../tmp`;
11
12 const maxTryDelay = 50;
13 const maxLockTime = 200;
14 const totalTestTime = 60000;
15
16 function printExcerpt(logs, index) {
17 const startIndex = Math.max(0, index - 50);
18 const endIndex = index + 50;
19
20 logs
21 .slice(startIndex, endIndex)
22 .forEach((log, index) => process.stdout.write(`${startIndex + index + 1} ${log.timestamp} ${log.message}\n`));
23 }
24
25 async function master() {
26 const numCPUs = os.cpus().length;
27 let logs = [];
28
29 fs.writeFileSync(`${tmpDir}/foo`, '');
30
31 for (let i = 0; i < numCPUs; i += 1) {
32 cluster.fork();
33 }
34
35 cluster.on('online', (worker) =>
36 worker.on('message', (data) =>
37 logs.push(data.toString().trim())));
38
39 cluster.on('exit', () => {
40 throw new Error('Child died prematurely');
41 });
42
43 await pDelay(totalTestTime);
44
45 cluster.removeAllListeners('exit');
46
47 cluster.disconnect(() => {
48 let acquired;
49
50 // Parse & sort logs
51 logs = logs.map((log) => {
52 const split = log.split(' ');
53
54 return { timestamp: Number(split[0]), message: split[1] };
55 });
56
57 logs = sort(logs, (log1, log2) => {
58 if (log1.timestamp > log2.timestamp) {
59 return 1;
60 }
61 if (log1.timestamp < log2.timestamp) {
62 return -1;
63 }
64 if (log1.message === 'LOCK_RELEASE_CALLED') {
65 return -1;
66 }
67 if (log2.message === 'LOCK_RELEASE_CALLED') {
68 return 1;
69 }
70
71 return 0;
72 });
73
74 // Validate logs
75 logs.forEach((log, index) => {
76 switch (log.message) {
77 case 'LOCK_ACQUIRED':
78 if (acquired) {
79 process.stdout.write(`\nInconsistent at line ${index + 1}\n`);
80 printExcerpt(logs, index);
81 process.exit(1);
82 }
83
84 acquired = true;
85 break;
86 case 'LOCK_RELEASE_CALLED':
87 if (!acquired) {
88 process.stdout.write(`\nInconsistent at line ${index + 1}\n`);
89 printExcerpt(logs, index);
90 process.exit(1);
91 }
92
93 acquired = false;
94 break;
95 default:
96 // Do nothing
97 }
98 });
99
100 process.exit(0);
101 });
102 }
103
104 function worker() {
105 process.on('disconnect', () => process.exit(0));
106
107 const tryLock = async () => {
108 await pDelay(Math.max(Math.random(), 10) * maxTryDelay);
109
110 process.send(`${Date.now()} LOCK_TRY\n`);
111
112 let release;
113
114 try {
115 release = await lockfile.lock(`${tmpDir}/foo`);
116 } catch (err) {
117 process.send(`${Date.now()} LOCK_BUSY\n`);
118 tryLock();
119
120 return;
121 }
122
123 process.send(`${Date.now()} LOCK_ACQUIRED\n`);
124
125 await pDelay(Math.max(Math.random(), 10) * maxLockTime);
126
127 process.send(`${Date.now()} LOCK_RELEASE_CALLED\n`);
128
129 await release();
130
131 tryLock();
132 };
133
134 tryLock();
135 }
136
137 // Any unhandled promise should cause the process to exit
138 process.on('unhandledRejection', (err) => {
139 console.error(err.stack);
140 process.exit(1);
141 });
142
143 if (cluster.isMaster) {
144 master();
145 } else {
146 worker();
147 }