Skip to content

Commit c7642af

Browse files
committed
(#205) fixed timer handling in validateResult
1 parent 5ef317f commit c7642af

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
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

+27
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,31 @@ describe("poll-action", () => {
139139
// THEN
140140
expect(action).toBeCalledTimes(1);
141141
});
142+
143+
it("should fail after timeout if no result is returned from long running action", async (done) => {
144+
// GIVEN
145+
const updateInterval = 100;
146+
const maxDuration = 200;
147+
const action = jest.fn(() => {
148+
return new Promise<boolean>((resolve) => {
149+
setTimeout(() => {
150+
resolve((undefined as unknown) as boolean);
151+
}, 210);
152+
});
153+
});
154+
155+
// WHEN
156+
try {
157+
await timeout(updateInterval, maxDuration, action);
158+
} catch (e) {
159+
expect(e).toBe(`Action timed out after ${maxDuration} ms`);
160+
}
161+
162+
// THEN
163+
expect(action).toBeCalledTimes(1);
164+
setTimeout(() => {
165+
expect(action).toBeCalledTimes(1);
166+
done();
167+
}, 500);
168+
});
142169
});

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)