Skip to content

Commit 2f22d72

Browse files
tsuemuraDavertMik
authored andcommitted
do not retry within and session (#1633)
* hide err.stack if stack is undefined * don't retry session and within * Add test to retryFailedStep plugin
1 parent f93bd39 commit 2f22d72

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed

lib/listener/trace.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ module.exports = function () {
2626
err.stack = ''; // hide internal error stack trace in non-verbose mode
2727
}
2828

29+
if (err.stack === undefined) {
30+
err.stack = '';
31+
}
32+
2933
err.stack = msg + err.stack;
3034
test.err = err;
3135
});

lib/recorder.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,11 @@ module.exports = {
113113
* @param {*} taskName
114114
* @param {*} fn
115115
* @param {*} force
116+
* @param {boolean} retry -
117+
* true: it will retries if `retryOpts` set.
118+
* false: ignore `retryOpts` and won't retry.
116119
*/
117-
add(taskName, fn = undefined, force = false) {
120+
add(taskName, fn = undefined, force = false, retry = true) {
118121
if (typeof taskName === 'function') {
119122
fn = taskName;
120123
taskName = fn.toString();
@@ -129,7 +132,7 @@ module.exports = {
129132
return promise = Promise.resolve(promise).then((res) => {
130133
const retryOpts = this.retries.slice(-1).pop();
131134
// no retries or unnamed tasks
132-
if (!retryOpts || !taskName) {
135+
if (!retryOpts || !taskName || !retry) {
133136
return Promise.resolve(res).then(fn);
134137
}
135138

lib/session.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function session(sessionName, config, fn) {
117117
}
118118
finalize();
119119
return recorder.promise().then(() => res);
120-
});
120+
}, false, false);
121121
}
122122

123123

lib/within.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function within(context, fn) {
5858
}
5959
finalize();
6060
return recorder.promise().then(() => res);
61-
});
61+
}, false, false);
6262
}
6363

6464
module.exports = within;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const retryFailedStep = require('../../../lib/plugin/retryFailedStep');
2+
const sinon = require('sinon');
3+
const within = require('../../../lib/within');
4+
const session = require('../../../lib/session');
5+
const container = require('../../../lib/container');
6+
const event = require('../../../lib/event');
7+
const recorder = require('../../../lib/recorder');
8+
const assert = require('assert');
9+
10+
describe('retryFailedStep', () => {
11+
beforeEach(() => {
12+
container.clear({
13+
mock: {
14+
_session: () => {},
15+
},
16+
});
17+
recorder.start();
18+
});
19+
it('should retry failed step', async () => {
20+
retryFailedStep({ retries: 2, minTimeout: 1 });
21+
event.dispatcher.emit(event.test.before);
22+
23+
let counter = 0;
24+
recorder.add(() => {
25+
counter++;
26+
if (counter < 3) {
27+
throw new Error();
28+
}
29+
});
30+
return recorder.promise();
31+
});
32+
it('should not retry within', async () => {
33+
retryFailedStep({ retries: 1, minTimeout: 1 });
34+
event.dispatcher.emit(event.test.before);
35+
36+
let counter = 0;
37+
38+
try {
39+
within('foo', () => {
40+
recorder.add(() => {
41+
counter++;
42+
throw new Error();
43+
});
44+
});
45+
await recorder.promise();
46+
} catch (e) {
47+
recorder.catchWithoutStop((err) => {});
48+
}
49+
50+
// expects to retry only once
51+
counter.should.equal(2);
52+
});
53+
it('should not retry session', async () => {
54+
retryFailedStep({ retries: 1, minTimeout: 1 });
55+
event.dispatcher.emit(event.test.before);
56+
57+
let counter = 0;
58+
59+
try {
60+
session('foo', () => {
61+
recorder.add(() => {
62+
counter++;
63+
throw new Error();
64+
});
65+
});
66+
await recorder.promise();
67+
} catch (e) {
68+
recorder.catchWithoutStop((err) => {});
69+
}
70+
71+
// expects to retry only once
72+
counter.should.equal(2);
73+
});
74+
});

0 commit comments

Comments
 (0)