Skip to content

Commit dcaed48

Browse files
authored
Merge pull request nut-tree#206 from nut-tree/fix/205/timeout-endless-loop
(nut-tree#205) fixed timer handling in validateResult
2 parents 75d3b07 + 77967bf commit dcaed48

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44

55
## upcomming release
66
- Feature: Create screenshot from region [(#154)](https://github.com/nut-tree/nut.js/issues/154)
7+
- Bugfix: Endless loop in timeout function for long-running actions returning undefined [(#205)](https://github.com/nut-tree/nut.js/issues/205)
78

89
## 1.5.0
910

lib/util/poll-action.function.spec.ts

+26-5
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,34 @@ describe("poll-action", () => {
130130
});
131131

132132
// WHEN
133-
try {
134-
await timeout(updateInterval, maxDuration, action);
135-
} catch (e) {
136-
expect(e).toEqual(`Action timed out after ${maxDuration} ms`);
137-
}
133+
const SUT = () => timeout(updateInterval, maxDuration, action);
134+
135+
// THEN
136+
await expect(SUT).rejects.toBe(`Action timed out after ${maxDuration} ms`);
137+
expect(action).toBeCalledTimes(1);
138+
});
139+
140+
it("should fail after timeout if no result is returned from long running action", async (done) => {
141+
// GIVEN
142+
const updateInterval = 100;
143+
const maxDuration = 200;
144+
const action = jest.fn(() => {
145+
return new Promise<boolean>((resolve) => {
146+
setTimeout(() => {
147+
resolve((undefined as unknown) as boolean);
148+
}, 210);
149+
});
150+
});
151+
152+
// WHEN
153+
const SUT = () => timeout(updateInterval, maxDuration, action);
138154

139155
// THEN
156+
await expect(SUT).rejects.toBe(`Action timed out after ${maxDuration} ms`);
140157
expect(action).toBeCalledTimes(1);
158+
setTimeout(() => {
159+
expect(action).toBeCalledTimes(1);
160+
done();
161+
}, 500);
141162
});
142163
});

lib/util/poll-action.function.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function timeout<R>(updateIntervalMs: number, maxDurationMs: number, acti
88
}
99

1010
function validateResult(result: R){
11-
if (!result) {
11+
if (!result && !timerCleaned) {
1212
interval = setTimeout(executeInterval, updateIntervalMs);
1313
} else {
1414
cleanupTimer();

0 commit comments

Comments
 (0)