Skip to content

Commit cb83f54

Browse files
authored
♨️ Hot fix (recorder): don't mess retries order any more (#2668)
* don't change the original reties order any more * reverse retryRules only once * add test * add unit tests Co-authored-by: Mario Melcher <mario.melcher@seitenbau.com>
1 parent 14de90f commit cb83f54

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

lib/actor.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class Actor {
3535
retry(opts) {
3636
if (opts === undefined) opts = 1;
3737
recorder.retry(opts);
38-
// adding an empty promise to clear retries
39-
recorder.add(() => null);
4038
// remove retry once the step passed
4139
recorder.add(() => event.dispatcher.once(event.step.finished, () => recorder.retries.pop()));
4240
return this;

lib/recorder.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,9 @@ module.exports = {
181181
return Promise.resolve(res).then(fn);
182182
}
183183

184+
const retryRules = this.retries.slice().reverse();
184185
return promiseRetry(Object.assign(defaultRetryOptions, retryOpts), (retry, number) => {
185186
if (number > 1) log(`${currentQueue()}Retrying... Attempt #${number}`);
186-
187-
const retryRules = this.retries.reverse();
188-
189187
return Promise.resolve(res).then(fn).catch((err) => {
190188
for (const retryObj of retryRules) {
191189
if (!retryObj.when) return retry(err);

test/unit/actor_test.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ describe('Actor', () => {
2121
bye: () => 'bye world',
2222
die: () => { throw new Error('ups'); },
2323
_hidden: () => 'hidden',
24-
failFirst: () => {
24+
failAfter: (i = 1) => {
2525
counter++;
26-
if (counter < 2) throw new Error('ups');
26+
if (counter <= i) throw new Error('ups');
27+
counter = 0;
2728
},
2829
},
2930
MyHelper2: {
3031
greeting: () => 'greetings, world',
3132
},
32-
});
33+
}, undefined, undefined);
3334
container.translation().vocabulary.actions.hello = 'привет';
3435
I = actor();
3536
event.cleanDispatcher();
@@ -79,7 +80,7 @@ describe('Actor', () => {
7980
});
8081

8182
it('should take all methods from helpers and built in', () => {
82-
['hello', 'bye', 'die', 'failFirst', 'say', 'retry', 'greeting'].forEach(key => {
83+
['hello', 'bye', 'die', 'failAfter', 'say', 'retry', 'greeting'].forEach(key => {
8384
expect(I).toHaveProperty(key);
8485
});
8586
});
@@ -108,11 +109,40 @@ describe('Actor', () => {
108109
});
109110

110111
it('should retry failed step with #retry', () => {
111-
return I.retry(2).failFirst();
112+
recorder.start();
113+
return I.retry({ retries: 2, minTimeout: 0 }).failAfter(1);
112114
});
113115

114116
it('should retry once step with #retry', () => {
115-
return I.retry().failFirst();
117+
recorder.start();
118+
return I.retry().failAfter(1);
119+
});
120+
121+
it('should alway use the latest global retry options', () => {
122+
recorder.start();
123+
recorder.retry({
124+
retries: 0,
125+
minTimeout: 0,
126+
when: () => true,
127+
});
128+
recorder.retry({
129+
retries: 1,
130+
minTimeout: 0,
131+
when: () => true,
132+
});
133+
I.hello(); // before fix: this changed the order of retries
134+
return I.failAfter(1);
135+
});
136+
137+
it('should not delete a global retry option', () => {
138+
recorder.start();
139+
recorder.retry({
140+
retries: 2,
141+
minTimeout: 0,
142+
when: () => true,
143+
});
144+
I.retry(1).failAfter(1); // before fix: this changed the order of retries
145+
return I.failAfter(2);
116146
});
117147

118148
it('should print handle failed steps', () => {

test/unit/plugin/retryFailedStep_test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,21 @@ describe('retryFailedStep', () => {
142142
// expects to retry only once
143143
counter.should.equal(2);
144144
});
145+
146+
it('should not turn around the chain of retries', () => {
147+
recorder.retry({ retries: 2, when: (err) => { return err.message === 'someerror'; }, identifier: 'test' });
148+
recorder.retry({ retries: 2, when: (err) => { return err.message === 'othererror'; } });
149+
150+
const getRetryIndex = () => recorder.retries.indexOf(recorder.retries.find(retry => retry.identifier));
151+
let initalIndex;
152+
153+
recorder.add(() => {
154+
initalIndex = getRetryIndex();
155+
}, undefined, undefined, true);
156+
157+
recorder.add(() => {
158+
initalIndex.should.equal(getRetryIndex());
159+
}, undefined, undefined, true);
160+
return recorder.promise();
161+
});
145162
});

0 commit comments

Comments
 (0)