-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite
waitForBatch
to reject early on non-batch errors
- Loading branch information
Showing
3 changed files
with
129 additions
and
27 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
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,68 @@ | ||
import { PrismaClientKnownRequestError } from '@prisma/engine-core' | ||
|
||
import { waitForBatch } from './waitForBatch' | ||
|
||
test('resolves when all promises succesfully resolve', async () => { | ||
const result = await waitForBatch([Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]) | ||
|
||
expect(result).toEqual([1, 2, 3]) | ||
}) | ||
|
||
test('returns results in order even if they resolve out of order', async () => { | ||
const result = await waitForBatch([ | ||
Promise.resolve(1), | ||
new Promise((resolve) => setTimeout(() => resolve(2), 200)), | ||
Promise.resolve(3), | ||
]) | ||
|
||
expect(result).toEqual([1, 2, 3]) | ||
}) | ||
|
||
test('rejects with error, which batchRequestIdx matches its position in batch', async () => { | ||
const err1 = new PrismaClientKnownRequestError('one', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 2 }) | ||
const err2 = new PrismaClientKnownRequestError('two', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 1 }) | ||
const err3 = new PrismaClientKnownRequestError('three', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 0 }) | ||
const result = waitForBatch([Promise.reject(err1), Promise.reject(err2), Promise.reject(err3)]) | ||
|
||
await expect(result).rejects.toBe(err2) | ||
}) | ||
|
||
test('rejects with error, which batchRequestIdx matches its position in batch, even if it rejects later', async () => { | ||
const err1 = new PrismaClientKnownRequestError('one', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 2 }) | ||
const err2 = new PrismaClientKnownRequestError('two', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 1 }) | ||
const err3 = new PrismaClientKnownRequestError('three', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 0 }) | ||
const result = waitForBatch([ | ||
Promise.reject(err1), | ||
new Promise((_, reject) => setTimeout(() => reject(err2), 200)), | ||
Promise.reject(err3), | ||
]) | ||
|
||
await expect(result).rejects.toBe(err2) | ||
}) | ||
|
||
test('rejects with the first error if no batchRequestIdxes match position', async () => { | ||
const err1 = new PrismaClientKnownRequestError('one', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 1 }) | ||
const err2 = new PrismaClientKnownRequestError('two', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 2 }) | ||
const err3 = new PrismaClientKnownRequestError('three', { code: 'P1', clientVersion: '0.0.0', batchRequestIdx: 0 }) | ||
const result = waitForBatch([ | ||
Promise.reject(err1), | ||
new Promise((_, reject) => setTimeout(() => reject(err2), 200)), | ||
Promise.reject(err3), | ||
]) | ||
|
||
await expect(result).rejects.toBe(err1) | ||
}) | ||
|
||
test('rejects with error if one of the promises rejects with non-batch error', async () => { | ||
const error = new Error('nope') | ||
const result = waitForBatch([Promise.resolve(1), Promise.reject(error), Promise.resolve(3)]) | ||
|
||
await expect(result).rejects.toBe(error) | ||
}) | ||
|
||
test('rejects with error if one of the promises rejects with non-batch error, even if other promsies never resolve', async () => { | ||
const error = new Error('nope') | ||
const result = waitForBatch([new Promise(() => {}), Promise.reject(error)]) | ||
|
||
await expect(result).rejects.toBe(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