-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(*): wait for gameserver to start (#5)
- Loading branch information
1 parent
6e2fee1
commit 5aad086
Showing
9 changed files
with
184 additions
and
1 deletion.
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
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export { HttpClientError } from './http-client.error'; | ||
export { InvalidReservationStatusError } from './invalid-reservation-state.error'; | ||
export { ServemeTfApiError } from './serveme-tf-api.error'; | ||
export { TimeoutError } from './timeout.error'; |
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 @@ | ||
import { ReservationStatus } from '../types/reservation-status'; | ||
|
||
export class InvalidReservationStatusError extends Error { | ||
constructor(public readonly reservationStatus: ReservationStatus) { | ||
super(`invalid reservation status: ${reservationStatus}`); | ||
} | ||
} |
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,5 @@ | ||
export class TimeoutError extends Error { | ||
constructor(public readonly timeoutMs: number) { | ||
super(`timed out after ${timeoutMs}ms`); | ||
} | ||
} |
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,20 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { withTimeout } from './with-timeout'; | ||
import { TimeoutError } from './errors/timeout.error'; | ||
|
||
describe('when fn does not timeout', () => { | ||
it('should return fn result', async () => { | ||
const result = await withTimeout( | ||
new Promise(resolve => setTimeout(() => resolve('foo'), 50)), | ||
100, | ||
); | ||
expect(result).toEqual('foo'); | ||
}); | ||
}); | ||
|
||
describe('when fn times out', () => { | ||
it('should throw', async () => { | ||
const promise = new Promise(resolve => setTimeout(resolve, 100)); | ||
await expect(withTimeout(promise, 50)).rejects.toThrow(TimeoutError); | ||
}); | ||
}); |
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,16 @@ | ||
import { TimeoutError } from './errors/timeout.error'; | ||
|
||
export const withTimeout = async <T>( | ||
promise: Promise<T>, | ||
timeoutMs: number, | ||
): Promise<T> => { | ||
let timeout: ReturnType<typeof setTimeout>; | ||
return Promise.race([ | ||
promise, | ||
new Promise<never>((_resolve, reject) => { | ||
timeout = setTimeout(() => { | ||
reject(new TimeoutError(timeoutMs)); | ||
}, timeoutMs); | ||
}), | ||
]).finally(() => clearTimeout(timeout)); | ||
}; |