Skip to content

Commit

Permalink
test: pLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
lihbr committed Sep 6, 2024
1 parent 580f2f1 commit 8759c81
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 16 deletions.
19 changes: 3 additions & 16 deletions src/lib/pLimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ type AnyFunction = (...arguments_: readonly any[]) => unknown
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

export type LimitFunction = {
/**
* The number of queued items waiting to be executed.
*/
readonly queueSize: number

/**
* @param fn - Promise-returning/async function.
* @param arguments - Any arguments to pass through to `fn`. Support for
Expand Down Expand Up @@ -97,16 +92,8 @@ export const pLimit = ({
})()
}

const generator = (function_: AnyFunction, ...arguments_: unknown[]) =>
new Promise((resolve) => {
return ((function_: AnyFunction, ...arguments_: unknown[]) =>
new Promise<unknown>((resolve) => {
enqueue(function_, resolve, arguments_)
})

Object.defineProperties(generator, {
queueSize: {
get: () => queue.length,
},
})

return generator as LimitFunction
})) as LimitFunction
}
16 changes: 16 additions & 0 deletions test/writeClient-createAsset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,19 @@ it.concurrent("respects unknown rate limit", async (ctx) => {

expect(Date.now() - start).toBeGreaterThanOrEqual(UNKNOWN_RATE_LIMIT_DELAY)
})

it("throws fetch errors as-is", async (ctx) => {
const client = createTestWriteClient({
ctx,
clientConfig: {
fetch: () => {
throw new Error(ctx.task.name)
},
},
})

await expect(() =>
// @ts-expect-error - testing purposes
client.createAsset("foo", "foo.png"),
).rejects.toThrowError(ctx.task.name)
})
16 changes: 16 additions & 0 deletions test/writeClient-createAssetTag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,19 @@ it.concurrent("respects unknown rate limit", async (ctx) => {

expect(Date.now() - start).toBeGreaterThanOrEqual(UNKNOWN_RATE_LIMIT_DELAY)
})

it("throws fetch errors as-is", async (ctx) => {
const client = createTestWriteClient({
ctx,
clientConfig: {
fetch: () => {
throw new Error(ctx.task.name)
},
},
})

await expect(() =>
// @ts-expect-error - testing purposes
client.createAssetTag("foo"),
).rejects.toThrowError(ctx.task.name)
})
24 changes: 24 additions & 0 deletions test/writeClient-createDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,27 @@ it.concurrent("respects unknown rate limit", async (ctx) => {

expect(Date.now() - start).toBeGreaterThanOrEqual(UNKNOWN_RATE_LIMIT_DELAY)
})

it("throws fetch errors as-is", async (ctx) => {
const client = createTestWriteClient({
ctx,
clientConfig: {
fetch: () => {
throw new Error(ctx.task.name)
},
},
})

await expect(() =>
// @ts-expect-error - testing purposes
client.createDocument(
{
type: "type",
uid: "uid",
lang: "lang",
data: {},
},
"Foo",
),
).rejects.toThrowError(ctx.task.name)
})
19 changes: 19 additions & 0 deletions test/writeClient-updateDocument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,22 @@ it.concurrent("supports custom headers", async (ctx) => {
.resolves.toBeUndefined()
ctx.expect.assertions(2)
})

it("throws fetch errors as-is", async (ctx) => {
const client = createTestWriteClient({
ctx,
clientConfig: {
fetch: () => {
throw new Error(ctx.task.name)
},
},
})

await expect(() =>
// @ts-expect-error - testing purposes
client.updateDocument("foo", {
uid: "uid",
data: {},
}),
).rejects.toThrowError(ctx.task.name)
})

0 comments on commit 8759c81

Please sign in to comment.