-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time out lint and test integrations (#835)
- Loading branch information
Showing
10 changed files
with
210 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'skuba': patch | ||
--- | ||
|
||
lint, test: Set timeout for Buildkite and GitHub integrations | ||
|
||
Transient network failures can impact annotations and autofixes. We now specify a 30 second timeout for these integration features to prevent them from hanging and indefinitely preoccupying your build agents. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { inspect } from 'util'; | ||
|
||
import { createTerseError } from './error'; | ||
|
||
describe('createTerseError', () => { | ||
it('creates a terse error for `util.inspect`', () => { | ||
const message = 'Badness!'; | ||
|
||
const err = createTerseError(message); | ||
|
||
// Retains standard message and stack behaviour | ||
expect(err.message).toBe(message); | ||
expect(err.stack).not.toBe(message); | ||
expect(err.stack).toContain(message); | ||
|
||
// Declares custom inspect function | ||
expect(inspect(err)).toBe(message); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import * as wait from './wait'; | ||
|
||
const delayMicrotask = () => | ||
Promise.resolve() | ||
.then(() => undefined) | ||
.then(() => undefined); | ||
|
||
const sleep = jest.spyOn(wait, 'sleep'); | ||
|
||
beforeEach(jest.resetAllMocks); | ||
|
||
describe('throwOnTimeout', () => { | ||
it('propagates a fulfilled promise within the timeout', async () => { | ||
sleep.mockImplementation(delayMicrotask); | ||
|
||
const value = 123; | ||
const promise = jest.fn().mockResolvedValue(value); | ||
|
||
await expect(wait.throwOnTimeout(promise(), { s: 3 })).resolves.toBe(value); | ||
|
||
expect(sleep).toHaveBeenCalledTimes(1); | ||
expect(sleep).toHaveBeenNthCalledWith(1, 3_000); | ||
}); | ||
|
||
it('propagates a rejected promise within the timeout', async () => { | ||
sleep.mockImplementation(delayMicrotask); | ||
|
||
const err = new Error('Badness!'); | ||
|
||
const promise = jest.fn().mockRejectedValue(err); | ||
|
||
await expect(wait.throwOnTimeout(promise(), { s: 2 })).rejects.toThrow(err); | ||
|
||
expect(sleep).toHaveBeenCalledTimes(1); | ||
expect(sleep).toHaveBeenNthCalledWith(1, 2_000); | ||
}); | ||
|
||
it('enforces the timeout', async () => { | ||
sleep.mockResolvedValue(); | ||
|
||
const promise = jest.fn().mockImplementation(delayMicrotask); | ||
|
||
await expect( | ||
wait.throwOnTimeout(promise(), { s: 1 }), | ||
).rejects.toThrowErrorMatchingInlineSnapshot(`"Timed out after 1 second"`); | ||
|
||
expect(sleep).toHaveBeenCalledTimes(1); | ||
expect(sleep).toHaveBeenNthCalledWith(1, 1_000); | ||
}); | ||
}); | ||
|
||
describe('withTimeout', () => { | ||
it('propagates a static value for easier `jest.mock`ing', async () => { | ||
sleep.mockImplementation(delayMicrotask); | ||
|
||
const value = 123; | ||
|
||
await expect(wait.withTimeout(value, { s: 1 })).resolves.toStrictEqual({ | ||
ok: true, | ||
value, | ||
}); | ||
|
||
expect(sleep).toHaveBeenCalledTimes(1); | ||
expect(sleep).toHaveBeenNthCalledWith(1, 1_000); | ||
}); | ||
|
||
it('propagates a fulfilled promise within the timeout', async () => { | ||
sleep.mockImplementation(delayMicrotask); | ||
|
||
const value = 123; | ||
const promise = jest.fn().mockResolvedValue(value); | ||
|
||
await expect(wait.withTimeout(promise(), { s: 1 })).resolves.toStrictEqual({ | ||
ok: true, | ||
value, | ||
}); | ||
|
||
expect(sleep).toHaveBeenCalledTimes(1); | ||
expect(sleep).toHaveBeenNthCalledWith(1, 1_000); | ||
}); | ||
|
||
it('propagates a rejected promise within the timeout', async () => { | ||
sleep.mockImplementation(delayMicrotask); | ||
|
||
const err = new Error('Badness!'); | ||
|
||
const promise = jest.fn().mockRejectedValue(err); | ||
|
||
await expect(wait.withTimeout(promise(), { s: 2 })).rejects.toThrow(err); | ||
|
||
expect(sleep).toHaveBeenCalledTimes(1); | ||
expect(sleep).toHaveBeenNthCalledWith(1, 2_000); | ||
}); | ||
|
||
it('enforces the timeout', async () => { | ||
sleep.mockResolvedValue(); | ||
|
||
const promise = jest.fn().mockImplementation(delayMicrotask); | ||
|
||
await expect(wait.withTimeout(promise(), { s: 3 })).resolves.toStrictEqual({ | ||
ok: false, | ||
}); | ||
|
||
expect(sleep).toHaveBeenCalledTimes(1); | ||
expect(sleep).toHaveBeenNthCalledWith(1, 3_000); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createTerseError } from './error'; | ||
import { pluralise } from './logging'; | ||
|
||
export const sleep = (ms: number) => | ||
new Promise<void>((resolve) => setTimeout(resolve, ms)); | ||
|
||
export const throwOnTimeout = async <T>( | ||
promise: PromiseLike<T>, | ||
{ s }: { s: number }, | ||
): Promise<T> => { | ||
const result = await withTimeout(promise, { s }); | ||
|
||
if (!result.ok) { | ||
throw createTerseError(`Timed out after ${pluralise(s, 'second')}`); | ||
} | ||
|
||
return result.value; | ||
}; | ||
|
||
type TimeoutResult<T> = { ok: true; value: T } | { ok: false }; | ||
|
||
export const withTimeout = <T>( | ||
promise: T | PromiseLike<T>, | ||
{ s }: { s: number }, | ||
): Promise<TimeoutResult<T>> => | ||
Promise.race<TimeoutResult<T>>([ | ||
Promise.resolve(promise).then((value) => ({ ok: true, value })), | ||
sleep(s * 1_000).then(() => ({ ok: false })), | ||
]); |