mocha-distributed 0.9.5 → 0.9.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -10
- package/example/suite-5-serial.js +34 -0
- package/example/suite-6-serial.js +35 -0
- package/example/util.js +18 -1
- package/index.js +50 -14
- package/package.json +3 -3
- package/test/test-retry.js +142 -0
- package/test-example-nworkers.sh +22 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# mocha-distributed
|
|
2
2
|
|
|
3
|
-
Run mocha tests
|
|
3
|
+
Run mocha tests in parallel.
|
|
4
4
|
|
|
5
5
|
Speed up your mocha tests by running them in parallel in multiple machines all
|
|
6
6
|
at once without changing a single line of code. You only need a redis server.
|
|
@@ -12,15 +12,16 @@ tests without having to change any line of code, nor having to decide
|
|
|
12
12
|
what to run where. Tests spread automatically according to the nodes you have.
|
|
13
13
|
|
|
14
14
|
The concept is very simple, basically you spawn as many runners as you wish
|
|
15
|
-
on as many nodes as you wish, and each
|
|
15
|
+
on as many nodes as you wish, and each process decides whether they should run
|
|
16
16
|
a test or the test has already been executed or is being executed somewhere
|
|
17
17
|
else.
|
|
18
18
|
|
|
19
|
-
It does not matter if you run the tests in one machine
|
|
20
|
-
|
|
19
|
+
It does not matter if you run the tests in one machine in multiple processes or
|
|
20
|
+
in multiple machines with multiple processes each. It will just work.
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
You don't need to change a single line of code, thus, this library it allows you
|
|
23
|
+
to continue developing tests as usual and launch them in parallel whenever you
|
|
24
|
+
want. No strings attached.
|
|
24
25
|
|
|
25
26
|
## Quick start
|
|
26
27
|
|
|
@@ -38,7 +39,7 @@ or machines where you want to run the tests on.
|
|
|
38
39
|
Finally, on each of the runners just run:
|
|
39
40
|
|
|
40
41
|
```bash
|
|
41
|
-
$ export MOCHA_DISTRIBUTED_EXECUTION_ID="
|
|
42
|
+
$ export MOCHA_DISTRIBUTED_EXECUTION_ID="execution__2024-01-01__20:10"
|
|
42
43
|
$ export MOCHA_DISTRIBUTED="redis://redis.address"
|
|
43
44
|
$ mocha --require mocha-distributed test/**/*.js
|
|
44
45
|
```
|
|
@@ -102,11 +103,11 @@ whether a test has already been executed or not by other of their peers.
|
|
|
102
103
|
string in each machine, BUT you can override this in case you need it for
|
|
103
104
|
whatever reason, although in theory you probably shouldn't.
|
|
104
105
|
|
|
105
|
-
- **MOCHA_DISTRIBUTED_EXPIRATION_TIME** =
|
|
106
|
+
- **MOCHA_DISTRIBUTED_EXPIRATION_TIME** = 604800
|
|
106
107
|
|
|
107
108
|
Configures to how long the data is kept in redis before it expires (in
|
|
108
|
-
seconds). The amount of data in redis is minimal,
|
|
109
|
-
to play with it.
|
|
109
|
+
seconds). 7 days is the default. The amount of data in redis is minimal,
|
|
110
|
+
so you probably don't want to play with it.
|
|
110
111
|
|
|
111
112
|
It might be helpful to increase it though, if you want to build some sort of
|
|
112
113
|
reporting on top of it, because you can directly explore test results in
|
|
@@ -172,6 +173,65 @@ Keep in mind that:
|
|
|
172
173
|
You might have a look at list-tests-from-redis.js for an example on how to
|
|
173
174
|
query redis and list all tests.
|
|
174
175
|
|
|
176
|
+
## Run tests serially
|
|
177
|
+
|
|
178
|
+
If you'd like some of your tests to run serially you can use a magic string with
|
|
179
|
+
this framework.
|
|
180
|
+
|
|
181
|
+
Simply add "[serial]" or "[serial-<ID OF YOUR CHOICE>]" to the title of your
|
|
182
|
+
test or test suite and all those tests will execute serially by the same runner.
|
|
183
|
+
|
|
184
|
+
The important part is that the test title contains "[serial" and ends with "]"
|
|
185
|
+
|
|
186
|
+
It's easier to explain with a couple of examples:
|
|
187
|
+
|
|
188
|
+
The following tests, regardless of whether they are on the same file or spreaded
|
|
189
|
+
in multiple files, will be executed all by the same runner one after another.
|
|
190
|
+
|
|
191
|
+
Might run in parallel to other tests that don't contain the "[serial]" word,
|
|
192
|
+
but will run sequentially for this group.
|
|
193
|
+
|
|
194
|
+
```javascript
|
|
195
|
+
it('Test id 1 [serial]', function() { /* ... */})
|
|
196
|
+
it('Test id 2 [serial]', function() { /* ... */})
|
|
197
|
+
it('Test id 3 [serial]', function() { /* ... */})
|
|
198
|
+
it('Test id 4 [serial]', function() { /* ... */})
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
See this other example below. Again, regardless of whether the tests are on the
|
|
202
|
+
same file or spreaded in multiple files, will be executed by two sets of
|
|
203
|
+
runners.
|
|
204
|
+
|
|
205
|
+
```javascript
|
|
206
|
+
it('Test id 1 [serial-worker]', function() { /* ... */})
|
|
207
|
+
it('Test id 2 [serial-worker]', function() { /* ... */})
|
|
208
|
+
it('Test id 3 [serial-another worker]', function() { /* ... */})
|
|
209
|
+
it('Test id 4 [serial-another worker]', function() { /* ... */})
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
Test 1 and 2 will be executed by one runner, whereas test 3 and 4 will be
|
|
213
|
+
executed by another. In both cases 1 and 2 will be executed sequentially and 3
|
|
214
|
+
and 4 also sequentially, but since they have different serial IDs, those two
|
|
215
|
+
subgroups of tests can run in parallel (e.g 1 and 2 in parallel with 3 and 4).
|
|
216
|
+
|
|
217
|
+
And now last example below:
|
|
218
|
+
|
|
219
|
+
```javascript
|
|
220
|
+
describe('[serial-my test id] test multiple things sequentially', function () {
|
|
221
|
+
it('Test id 1', function() { /* ... */})
|
|
222
|
+
it('Test id 2', function() { /* ... */})
|
|
223
|
+
it('Test id 3', function() { /* ... */})
|
|
224
|
+
it('Test id 4', function() { /* ... */})
|
|
225
|
+
})
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
The suite contains "[serial-my test id]", but the tests don't contain any serial
|
|
229
|
+
magic id. In this case, ALL those tests will run sequentially because the suite
|
|
230
|
+
contains the magic word.
|
|
231
|
+
|
|
232
|
+
Long story short. Add "[serial-whatever you want]" on the title but make sure
|
|
233
|
+
that "whatever you want" is the same for the stuff you want to run sequentially.
|
|
234
|
+
|
|
175
235
|
## Examples
|
|
176
236
|
|
|
177
237
|
### Environment-agnostic
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const util = require('./util.js');
|
|
2
|
+
|
|
3
|
+
// require('mocha-distributed')
|
|
4
|
+
require('../index.js');
|
|
5
|
+
|
|
6
|
+
describe ('suite-5.serial', async function () {
|
|
7
|
+
beforeEach (function () {
|
|
8
|
+
// console.log ('before each');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it ('[serial] test-5.0', async function () {
|
|
12
|
+
await util.serialTestConcurrency('serial5')
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it ('[serial:odd-worker] test-5.1', async function () {
|
|
16
|
+
await util.serialTestConcurrency('odd-worker')
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it ('[serial:even-worker] test-5.2', async function () {
|
|
20
|
+
await util.serialTestConcurrency('even-worker')
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it ('[serial:odd-worker] test-5.3', async function () {
|
|
24
|
+
await util.serialTestConcurrency('odd-worker')
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it ('[serial:even-worker] test-5.4', async function () {
|
|
28
|
+
await util.serialTestConcurrency('even-worker')
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it ('[serial] test-5.5', async function () {
|
|
32
|
+
await util.serialTestConcurrency('serial5')
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
const util = require('./util.js');
|
|
2
|
+
|
|
3
|
+
// require('mocha-distributed')
|
|
4
|
+
require('../index.js');
|
|
5
|
+
|
|
6
|
+
// NOTE: since "describe" has [serial] on it, all will be executed with that ID
|
|
7
|
+
describe ('[serial] suite-6.serial', async function () {
|
|
8
|
+
beforeEach (function () {
|
|
9
|
+
// console.log ('before each');
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it ('[serial] test-6.0', async function () {
|
|
13
|
+
await util.serialTestConcurrency('serial6')
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it ('[serial:odd-worker] test-6.1', async function () {
|
|
17
|
+
await util.serialTestConcurrency('serial6')
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it ('[serial:even-worker] test-6.2', async function () {
|
|
21
|
+
await util.serialTestConcurrency('serial6')
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it ('[serial:odd-worker] test-6.3', async function () {
|
|
25
|
+
await util.serialTestConcurrency('serial6')
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it ('[serial:even-worker] test-6.4', async function () {
|
|
29
|
+
await util.serialTestConcurrency('serial6')
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it ('[serial] test-6.5', async function () {
|
|
33
|
+
await util.serialTestConcurrency('serial6')
|
|
34
|
+
});
|
|
35
|
+
});
|
package/example/util.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
1
2
|
const util = require('./util.js');
|
|
2
3
|
|
|
3
4
|
async function sleep (seconds) {
|
|
@@ -6,6 +7,22 @@ async function sleep (seconds) {
|
|
|
6
7
|
});
|
|
7
8
|
}
|
|
8
9
|
|
|
10
|
+
// -----------------------------------------------------------------------------
|
|
11
|
+
// Append data to given file name, wait for some time, and then continue
|
|
12
|
+
// adding end of data.
|
|
13
|
+
//
|
|
14
|
+
// This will easily make visible on the file whether two tests have been
|
|
15
|
+
// executed concurrently or not, since it will mess up the lines on the file
|
|
16
|
+
// -----------------------------------------------------------------------------
|
|
17
|
+
async function serialTestConcurrency(name) {
|
|
18
|
+
const fname = `tmp-${name}.tmp`
|
|
19
|
+
|
|
20
|
+
fs.appendFileSync(fname, `${name}: [Start: ${Date.now()} -> `)
|
|
21
|
+
await sleep(1);
|
|
22
|
+
fs.appendFileSync(fname, ` End:${Date.now()}]\n`)
|
|
23
|
+
}
|
|
24
|
+
|
|
9
25
|
module.exports = {
|
|
10
|
-
sleep
|
|
26
|
+
sleep,
|
|
27
|
+
serialTestConcurrency
|
|
11
28
|
}
|
package/index.js
CHANGED
|
@@ -15,7 +15,7 @@ const GRANULARITY = {
|
|
|
15
15
|
const g_redisAddress = process.env.MOCHA_DISTRIBUTED || "";
|
|
16
16
|
const g_testExecutionId = process.env.MOCHA_DISTRIBUTED_EXECUTION_ID || "";
|
|
17
17
|
const g_expirationTime =
|
|
18
|
-
process.env.MOCHA_DISTRIBUTED_EXPIRATION_TIME || `${24 * 3600}`;
|
|
18
|
+
process.env.MOCHA_DISTRIBUTED_EXPIRATION_TIME || `${7 * 24 * 3600}`;
|
|
19
19
|
|
|
20
20
|
// Generate a unique random id for this runner (with almost 100% certainty
|
|
21
21
|
// to be different on any machine/environment).
|
|
@@ -34,6 +34,11 @@ let g_redis = null;
|
|
|
34
34
|
|
|
35
35
|
let g_capture = { stdout: null, stderr: null };
|
|
36
36
|
|
|
37
|
+
// Cache errors from intermediate retry attempts. Mocha only sets test.err via
|
|
38
|
+
// the reporter on the final EVENT_TEST_FAIL; for non-final retries it emits
|
|
39
|
+
// EVENT_TEST_RETRY instead and never stores the error on the test object.
|
|
40
|
+
const g_retryErrors = new Map();
|
|
41
|
+
|
|
37
42
|
// -----------------------------------------------------------------------------
|
|
38
43
|
// getTestPath
|
|
39
44
|
//
|
|
@@ -59,6 +64,29 @@ function getTestPath(testContext) {
|
|
|
59
64
|
return path.reverse();
|
|
60
65
|
}
|
|
61
66
|
|
|
67
|
+
// -----------------------------------------------------------------------------
|
|
68
|
+
// getSerialGranularity
|
|
69
|
+
//
|
|
70
|
+
// Returns the full string or the "serial string" which is whatever finds that
|
|
71
|
+
// follows this regex "[serial.*]" on the string. Only first instance is
|
|
72
|
+
// returned.
|
|
73
|
+
//
|
|
74
|
+
// This will allow serializing tests with given serial name.
|
|
75
|
+
// -----------------------------------------------------------------------------
|
|
76
|
+
function getSerialGranularity(testKey) {
|
|
77
|
+
// NOTE: a regular expression might be trickier to get right, since you can
|
|
78
|
+
// have multiple instances of [serialxxxx] on the same string
|
|
79
|
+
let index = testKey.indexOf('[serial')
|
|
80
|
+
if (index === -1)
|
|
81
|
+
return testKey
|
|
82
|
+
|
|
83
|
+
let index2 = testKey.indexOf(']', index)
|
|
84
|
+
if (index2 === -1)
|
|
85
|
+
return testKey
|
|
86
|
+
|
|
87
|
+
return testKey.substring(index, index2+1)
|
|
88
|
+
}
|
|
89
|
+
|
|
62
90
|
// -----------------------------------------------------------------------------
|
|
63
91
|
// captureStream
|
|
64
92
|
// -----------------------------------------------------------------------------
|
|
@@ -85,6 +113,11 @@ function captureStream(stream) {
|
|
|
85
113
|
// Initialize redis once before the tests
|
|
86
114
|
// -----------------------------------------------------------------------------
|
|
87
115
|
exports.mochaGlobalSetup = async function () {
|
|
116
|
+
// `this` is the Mocha Runner — store errors from non-final retry attempts
|
|
117
|
+
// so afterEach can record them (Mocha never sets test.err for those).
|
|
118
|
+
this.on('retry', (test, err) => {
|
|
119
|
+
g_retryErrors.set(test.fullTitle(), err);
|
|
120
|
+
});
|
|
88
121
|
if (g_mochaVerbose) {
|
|
89
122
|
const redisNoCredentials = g_redisAddress.replace(
|
|
90
123
|
/\/\/[^@]*@/,
|
|
@@ -137,8 +170,8 @@ exports.mochaGlobalTeardown = async function () {
|
|
|
137
170
|
exports.mochaHooks = {
|
|
138
171
|
beforeEach: async function () {
|
|
139
172
|
const testPath = getTestPath(this.currentTest);
|
|
140
|
-
const testKeyFullPath = `${g_testExecutionId}:${testPath.join(":")}`;
|
|
141
|
-
const testKeySuite = `${g_testExecutionId}:${testPath[0]}`;
|
|
173
|
+
const testKeyFullPath = `${g_testExecutionId}:${getSerialGranularity(testPath.join(":"))}`;
|
|
174
|
+
const testKeySuite = `${g_testExecutionId}:${getSerialGranularity(testPath[0])}`;
|
|
142
175
|
|
|
143
176
|
const testKey =
|
|
144
177
|
g_granularity === GRANULARITY.TEST ? testKeyFullPath : testKeySuite;
|
|
@@ -160,7 +193,7 @@ exports.mochaHooks = {
|
|
|
160
193
|
}
|
|
161
194
|
},
|
|
162
195
|
|
|
163
|
-
afterEach(
|
|
196
|
+
afterEach: async function () {
|
|
164
197
|
const SKIPPED = "pending";
|
|
165
198
|
const FAILED = "failed";
|
|
166
199
|
const PASSED = "passed";
|
|
@@ -204,7 +237,10 @@ exports.mochaHooks = {
|
|
|
204
237
|
// Error objects cannot be properly serialized with stringify, thus
|
|
205
238
|
// we need to use this hack to make it look like a normal object.
|
|
206
239
|
// Hopefully this should work as well with other sort of objects
|
|
207
|
-
const err = this.currentTest.err
|
|
240
|
+
const err = this.currentTest.err
|
|
241
|
+
|| g_retryErrors.get(this.currentTest.fullTitle())
|
|
242
|
+
|| null;
|
|
243
|
+
g_retryErrors.delete(this.currentTest.fullTitle());
|
|
208
244
|
const errObj = JSON.parse(
|
|
209
245
|
JSON.stringify(err, Object.getOwnPropertyNames(err || {}))
|
|
210
246
|
);
|
|
@@ -229,16 +265,16 @@ exports.mochaHooks = {
|
|
|
229
265
|
};
|
|
230
266
|
|
|
231
267
|
// save results as single line on purpose
|
|
232
|
-
const
|
|
233
|
-
g_redis.rPush(key, JSON.stringify(testResult));
|
|
234
|
-
g_redis.expire(key, g_expirationTime);
|
|
235
|
-
|
|
236
|
-
// increment passed_count/failed_count & set expiry time
|
|
268
|
+
const resultKey = `${g_testExecutionId}:test_result`;
|
|
237
269
|
const countKey = `${g_testExecutionId}:${stateFixed}_count`;
|
|
238
|
-
g_redis.incr(countKey);
|
|
239
|
-
g_redis.expire(countKey, g_expirationTime);
|
|
240
|
-
}
|
|
241
270
|
|
|
242
|
-
|
|
271
|
+
await g_redis
|
|
272
|
+
.multi()
|
|
273
|
+
.rPush(resultKey, JSON.stringify(testResult))
|
|
274
|
+
.expire(resultKey, g_expirationTime)
|
|
275
|
+
.incr(countKey)
|
|
276
|
+
.expire(countKey, g_expirationTime)
|
|
277
|
+
.exec();
|
|
278
|
+
}
|
|
243
279
|
},
|
|
244
280
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mocha-distributed",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.7",
|
|
4
4
|
"description": "Run multiple mocha suites and tests in parallel, from different processes and different machines. Results available on a redis database.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "mocha test/test-retry.js"
|
|
8
8
|
},
|
|
9
9
|
"keywords": [
|
|
10
10
|
"mocha",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
],
|
|
24
24
|
"repository": {
|
|
25
25
|
"type": "git",
|
|
26
|
-
"url": "https://github.com/pausan/mocha-distributed.git"
|
|
26
|
+
"url": "git+https://github.com/pausan/mocha-distributed.git"
|
|
27
27
|
},
|
|
28
28
|
"author": "Pau Sanchez",
|
|
29
29
|
"license": "MIT",
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// -----------------------------------------------------------------------------
|
|
2
|
+
// test/test-retry.js
|
|
3
|
+
//
|
|
4
|
+
// Mocha test suite verifying that mocha-distributed records err, stdout and
|
|
5
|
+
// stderr on ALL retry attempts, not just the last one.
|
|
6
|
+
//
|
|
7
|
+
// Run with: mocha test/test-retry.js
|
|
8
|
+
// -----------------------------------------------------------------------------
|
|
9
|
+
'use strict';
|
|
10
|
+
|
|
11
|
+
const assert = require('assert');
|
|
12
|
+
const Mocha = require('mocha/lib/mocha');
|
|
13
|
+
const Suite = require('mocha/lib/suite');
|
|
14
|
+
const Test = require('mocha/lib/test');
|
|
15
|
+
|
|
16
|
+
// -----------------------------------------------------------------------------
|
|
17
|
+
// Mock redis helpers
|
|
18
|
+
// -----------------------------------------------------------------------------
|
|
19
|
+
const redisResolved = require.resolve('redis');
|
|
20
|
+
|
|
21
|
+
const mockMulti = (writtenResults) => () => {
|
|
22
|
+
const cmds = [];
|
|
23
|
+
const chain = {
|
|
24
|
+
set: (...a) => { cmds.push(['set', ...a]); return chain; },
|
|
25
|
+
get: (...a) => { cmds.push(['get', ...a]); return chain; },
|
|
26
|
+
rPush: (...a) => {
|
|
27
|
+
cmds.push(['rPush', ...a]);
|
|
28
|
+
writtenResults.push(JSON.parse(a[1]));
|
|
29
|
+
return chain;
|
|
30
|
+
},
|
|
31
|
+
expire: (...a) => { cmds.push(['expire', ...a]); return chain; },
|
|
32
|
+
incr: (...a) => { cmds.push(['incr', ...a]); return chain; },
|
|
33
|
+
exec: async () => {
|
|
34
|
+
// beforeEach pipeline: SET NX + GET — this runner always wins ownership
|
|
35
|
+
if (cmds[0] && cmds[0][0] === 'set') {
|
|
36
|
+
return [null, process.env.MOCHA_DISTRIBUTED_RUNNER_ID];
|
|
37
|
+
}
|
|
38
|
+
// afterEach pipeline: rPush + expire + incr + expire
|
|
39
|
+
return [1, 1, 1, 1];
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
return chain;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function injectMockRedis(writtenResults) {
|
|
46
|
+
require.cache[redisResolved] = {
|
|
47
|
+
id: redisResolved,
|
|
48
|
+
filename: redisResolved,
|
|
49
|
+
loaded: true,
|
|
50
|
+
exports: {
|
|
51
|
+
createClient: () => ({
|
|
52
|
+
on: () => {},
|
|
53
|
+
connect: async () => {},
|
|
54
|
+
quit: async () => {},
|
|
55
|
+
multi: mockMulti(writtenResults),
|
|
56
|
+
})
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function restoreRedis() {
|
|
62
|
+
delete require.cache[redisResolved];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function loadFreshLib() {
|
|
66
|
+
const libPath = require.resolve('../index.js');
|
|
67
|
+
delete require.cache[libPath];
|
|
68
|
+
return require('../index.js');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// -----------------------------------------------------------------------------
|
|
72
|
+
// Tests
|
|
73
|
+
// -----------------------------------------------------------------------------
|
|
74
|
+
describe('mocha-distributed', function () {
|
|
75
|
+
|
|
76
|
+
describe('retry attempt recording', function () {
|
|
77
|
+
let writtenResults;
|
|
78
|
+
let lib;
|
|
79
|
+
|
|
80
|
+
before(function () {
|
|
81
|
+
writtenResults = [];
|
|
82
|
+
injectMockRedis(writtenResults);
|
|
83
|
+
|
|
84
|
+
process.env.MOCHA_DISTRIBUTED = 'redis://mock';
|
|
85
|
+
process.env.MOCHA_DISTRIBUTED_EXECUTION_ID = 'test-exec-retry';
|
|
86
|
+
process.env.MOCHA_DISTRIBUTED_RUNNER_ID = 'runner-test';
|
|
87
|
+
|
|
88
|
+
lib = loadFreshLib();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
after(function () {
|
|
92
|
+
restoreRedis();
|
|
93
|
+
delete require.cache[require.resolve('../index.js')];
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('records err, stdout and stderr on every retry attempt', async function () {
|
|
97
|
+
this.timeout(10000);
|
|
98
|
+
|
|
99
|
+
// Build the inner mocha instance with a flaky test that fails on
|
|
100
|
+
// attempts 0 and 1, and passes on attempt 2
|
|
101
|
+
const m = new Mocha({ reporter: 'min' });
|
|
102
|
+
m.rootHooks(lib.mochaHooks);
|
|
103
|
+
m.globalSetup([lib.mochaGlobalSetup]);
|
|
104
|
+
m.globalTeardown([lib.mochaGlobalTeardown]);
|
|
105
|
+
|
|
106
|
+
const suite = Suite.create(m.suite, 'retry-suite');
|
|
107
|
+
suite.retries(2);
|
|
108
|
+
|
|
109
|
+
let attempt = 0;
|
|
110
|
+
suite.addTest(new Test('flaky-test', function () {
|
|
111
|
+
attempt++;
|
|
112
|
+
console.log('stdout from attempt ' + attempt);
|
|
113
|
+
console.error('stderr from attempt ' + attempt);
|
|
114
|
+
if (attempt < 3) throw new Error('intentional failure on attempt ' + attempt);
|
|
115
|
+
}));
|
|
116
|
+
|
|
117
|
+
await new Promise(resolve => m.run(resolve));
|
|
118
|
+
|
|
119
|
+
// Sort by retryAttempt for stable assertions
|
|
120
|
+
const results = writtenResults.slice().sort((a, b) => a.retryAttempt - b.retryAttempt);
|
|
121
|
+
|
|
122
|
+
assert.strictEqual(results.length, 3, '3 results written to Redis (one per attempt)');
|
|
123
|
+
|
|
124
|
+
// Attempts 0 and 1: failed, err populated, stdout and stderr present
|
|
125
|
+
for (const i of [0, 1]) {
|
|
126
|
+
const r = results[i];
|
|
127
|
+
assert.strictEqual(r.retryAttempt, i, `attempt ${i}: retryAttempt`);
|
|
128
|
+
assert.strictEqual(r.state, 'failed', `attempt ${i}: state`);
|
|
129
|
+
assert.ok(r.err && r.err.message, `attempt ${i}: err.message should be set`);
|
|
130
|
+
assert.ok(r.stdout.includes(`attempt ${i + 1}`), `attempt ${i}: stdout`);
|
|
131
|
+
assert.ok(r.stderr.includes(`attempt ${i + 1}`), `attempt ${i}: stderr`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Attempt 2: passed, stdout and stderr present
|
|
135
|
+
const r2 = results[2];
|
|
136
|
+
assert.strictEqual(r2.retryAttempt, 2, 'attempt 2: retryAttempt');
|
|
137
|
+
assert.strictEqual(r2.state, 'passed', 'attempt 2: state');
|
|
138
|
+
assert.ok(r2.stdout.includes('attempt 3'), 'attempt 2: stdout');
|
|
139
|
+
assert.ok(r2.stderr.includes('attempt 3'), 'attempt 2: stderr');
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
export MOCHA_DISTRIBUTED_EXECUTION_ID="eid_$(date +%s)"
|
|
3
|
+
export MOCHA_DISTRIBUTED="redis://127.0.0.1"
|
|
4
|
+
|
|
5
|
+
npm install > /dev/null 2>&1
|
|
6
|
+
|
|
7
|
+
N=$1
|
|
8
|
+
COMMAND="mocha --require ./index.js example/**/*.js"
|
|
9
|
+
|
|
10
|
+
# cleanup tmp files
|
|
11
|
+
rm tmp-*
|
|
12
|
+
|
|
13
|
+
echo "Spawning $N commands in parallel with Execution ID: $MOCHA_DISTRIBUTED_EXECUTION_ID"
|
|
14
|
+
|
|
15
|
+
# Run the command N times in the background, saving stdout/stderr
|
|
16
|
+
for ((i = 1; i <= N; i++)); do
|
|
17
|
+
$COMMAND >"tmp-output-$i.log" 2>&1 &
|
|
18
|
+
done
|
|
19
|
+
|
|
20
|
+
wait
|
|
21
|
+
|
|
22
|
+
echo "All tests finished!"
|