Skip to content

Commit 0856010

Browse files
committed
Rename withTimeout() to waitForResultWithTimeLimit()
The name withTimeout() gives the impression that it would limit the execution of the promise to the given time bound. But that is not the case: it is only the _waiting_ that is limited, and the promise would keep running beyond the time bound. This commit renames withTimeout() to waitForResultWithTimeLimit() so that developers are more likely to understand the actual behavior of this function.
1 parent b73659a commit 0856010

File tree

9 files changed

+56
-44
lines changed

9 files changed

+56
-44
lines changed

lib/analyze-action.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action-post.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/init-action.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-lib.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/upload-sarif-action.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/overlay-database-utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { type CodeQL } from "./codeql";
1010
import { type Config } from "./config-utils";
1111
import { getCommitOid, getFileOidsUnderPath } from "./git-utils";
1212
import { Logger, withGroupAsync } from "./logging";
13-
import { isInTestMode, tryGetFolderBytes, withTimeout } from "./util";
13+
import {
14+
isInTestMode,
15+
tryGetFolderBytes,
16+
waitForResultWithTimeLimit,
17+
} from "./util";
1418

1519
export enum OverlayDatabaseMode {
1620
Overlay = "overlay",
@@ -268,7 +272,7 @@ export async function uploadOverlayBaseDatabaseToCache(
268272
);
269273

270274
try {
271-
const cacheId = await withTimeout(
275+
const cacheId = await waitForResultWithTimeLimit(
272276
MAX_CACHE_OPERATION_MS,
273277
actionsCache.saveCache([dbLocation], cacheSaveKey),
274278
() => {},
@@ -346,7 +350,7 @@ export async function downloadOverlayBaseDatabaseFromCache(
346350
let databaseDownloadDurationMs = 0;
347351
try {
348352
const databaseDownloadStart = performance.now();
349-
const foundKey = await withTimeout(
353+
const foundKey = await waitForResultWithTimeLimit(
350354
MAX_CACHE_OPERATION_MS,
351355
actionsCache.restoreCache([dbLocation], cacheRestoreKeyPrefix),
352356
() => {

src/trap-caching.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
getErrorMessage,
1717
isHTTPError,
1818
tryGetFolderBytes,
19-
withTimeout,
19+
waitForResultWithTimeLimit,
2020
} from "./util";
2121

2222
// This constant should be bumped if we make a breaking change
@@ -96,7 +96,7 @@ export async function downloadTrapCaches(
9696
logger.info(
9797
`Looking in Actions cache for TRAP cache with key ${preferredKey}`,
9898
);
99-
const found = await withTimeout(
99+
const found = await waitForResultWithTimeLimit(
100100
MAX_CACHE_OPERATION_MS,
101101
actionsCache.restoreCache([cacheDir], preferredKey, [
102102
// Fall back to any cache with the right key prefix
@@ -156,7 +156,7 @@ export async function uploadTrapCaches(
156156
process.env.GITHUB_SHA || "unknown",
157157
);
158158
logger.info(`Uploading TRAP cache to Actions cache with key ${key}`);
159-
await withTimeout(
159+
await waitForResultWithTimeLimit(
160160
MAX_CACHE_OPERATION_MS,
161161
actionsCache.saveCache([cacheDir], key),
162162
() => {

src/util.test.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -297,43 +297,51 @@ test("listFolder", async (t) => {
297297
const longTime = 999_999;
298298
const shortTime = 10;
299299

300-
test("withTimeout on long task", async (t) => {
300+
test("waitForResultWithTimeLimit on long task", async (t) => {
301301
let longTaskTimedOut = false;
302302
const longTask = new Promise((resolve) => {
303303
const timer = setTimeout(() => {
304304
resolve(42);
305305
}, longTime);
306306
t.teardown(() => clearTimeout(timer));
307307
});
308-
const result = await util.withTimeout(shortTime, longTask, () => {
309-
longTaskTimedOut = true;
310-
});
308+
const result = await util.waitForResultWithTimeLimit(
309+
shortTime,
310+
longTask,
311+
() => {
312+
longTaskTimedOut = true;
313+
},
314+
);
311315
t.deepEqual(longTaskTimedOut, true);
312316
t.deepEqual(result, undefined);
313317
});
314318

315-
test("withTimeout on short task", async (t) => {
319+
test("waitForResultWithTimeLimit on short task", async (t) => {
316320
let shortTaskTimedOut = false;
317321
const shortTask = new Promise((resolve) => {
318322
setTimeout(() => {
319323
resolve(99);
320324
}, shortTime);
321325
});
322-
const result = await util.withTimeout(longTime, shortTask, () => {
323-
shortTaskTimedOut = true;
324-
});
326+
const result = await util.waitForResultWithTimeLimit(
327+
longTime,
328+
shortTask,
329+
() => {
330+
shortTaskTimedOut = true;
331+
},
332+
);
325333
t.deepEqual(shortTaskTimedOut, false);
326334
t.deepEqual(result, 99);
327335
});
328336

329-
test("withTimeout doesn't call callback if promise resolves", async (t) => {
337+
test("waitForResultWithTimeLimit doesn't call callback if promise resolves", async (t) => {
330338
let shortTaskTimedOut = false;
331339
const shortTask = new Promise((resolve) => {
332340
setTimeout(() => {
333341
resolve(99);
334342
}, shortTime);
335343
});
336-
const result = await util.withTimeout(100, shortTask, () => {
344+
const result = await util.waitForResultWithTimeLimit(100, shortTask, () => {
337345
shortTaskTimedOut = true;
338346
});
339347
await new Promise((r) => setTimeout(r, 200));

src/util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ let hadTimeout = false;
864864
* @param onTimeout A callback to call if the promise times out.
865865
* @returns The result of the promise, or undefined if the promise times out.
866866
*/
867-
export async function withTimeout<T>(
867+
export async function waitForResultWithTimeLimit<T>(
868868
timeoutMs: number,
869869
promise: Promise<T>,
870870
onTimeout: () => void,
@@ -894,7 +894,7 @@ export async function withTimeout<T>(
894894
* Check if the global hadTimeout variable has been set, and if so then
895895
* exit the process to ensure any background tasks that are still running
896896
* are killed. This should be called at the end of execution if the
897-
* `withTimeout` function has been used.
897+
* `waitForResultWithTimeLimit` function has been used.
898898
*/
899899
export async function checkForTimeout() {
900900
if (hadTimeout === true) {

0 commit comments

Comments
 (0)