-
-
Notifications
You must be signed in to change notification settings - Fork 988
fix(requests): serialize request creation per user #3377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+121
−4
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,99 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { beforeEach, describe, it, mock } from 'node:test'; | ||
|
|
||
| import ExternalAPI from '@server/api/externalapi'; | ||
| import { MediaType } from '@server/constants/media'; | ||
| import { getRepository } from '@server/datasource'; | ||
| import { | ||
| DuplicateMediaRequestError, | ||
| MediaRequest, | ||
| QuotaRestrictedError, | ||
| } from '@server/entity/MediaRequest'; | ||
| import { User } from '@server/entity/User'; | ||
| import { setupTestDb } from '@server/test/db'; | ||
|
|
||
| // get is a prototype method unlike getMovie, and replaces the cache lookup too | ||
| const externalApiGetMock = mock.method( | ||
| ExternalAPI.prototype as unknown as { | ||
| get: (endpoint: string) => Promise<unknown>; | ||
| }, | ||
| 'get', | ||
| async (endpoint: string) => { | ||
| const movieId = Number(endpoint.replace('/movie/', '')); | ||
|
|
||
| if (!movieId) { | ||
| throw new Error(`Unstubbed external endpoint: ${endpoint}`); | ||
| } | ||
|
|
||
| return { | ||
| id: movieId, | ||
| external_ids: {}, | ||
| // Skips getMovie's localized fallback call | ||
| videos: { results: [{ type: 'Trailer', key: 'trailer' }] }, | ||
| }; | ||
| } | ||
| ).mock; | ||
|
|
||
| mock.method(MediaRequest, 'sendNotification', async () => undefined); | ||
|
|
||
| setupTestDb(); | ||
|
|
||
| beforeEach(() => { | ||
| externalApiGetMock.resetCalls(); | ||
| }); | ||
|
|
||
| async function seedRequester(movieQuotaLimit: number): Promise<User> { | ||
| const userRepository = getRepository(User); | ||
|
|
||
| const requester = await userRepository.findOneOrFail({ | ||
| where: { email: 'friend@seerr.dev' }, | ||
| }); | ||
| requester.movieQuotaLimit = movieQuotaLimit; | ||
|
|
||
| return userRepository.save(requester); | ||
| } | ||
|
|
||
| function requestMovies(mediaIds: number[], requester: User) { | ||
| return Promise.allSettled( | ||
| mediaIds.map((mediaId) => | ||
| MediaRequest.request( | ||
| { mediaId, mediaType: MediaType.MOVIE, is4k: false }, | ||
| requester | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| function rejections(results: PromiseSettledResult<MediaRequest>[]) { | ||
| return results.filter( | ||
| (result): result is PromiseRejectedResult => result.status === 'rejected' | ||
| ); | ||
| } | ||
|
|
||
| describe('MediaRequest.request', () => { | ||
| it('rejects the second of two concurrent requests at the movie quota', async () => { | ||
| const requestRepository = getRepository(MediaRequest); | ||
| const requester = await seedRequester(1); | ||
|
|
||
| const results = await requestMovies([11111, 22222], requester); | ||
| const rejected = rejections(results); | ||
|
|
||
| assert.strictEqual(rejected.length, 1); | ||
| assert.ok(rejected[0].reason instanceof QuotaRestrictedError); | ||
| assert.strictEqual(await requestRepository.count(), 1); | ||
| assert.strictEqual(externalApiGetMock.callCount(), 1); | ||
| }); | ||
|
|
||
| it('rejects a concurrent duplicate request for the same movie', async () => { | ||
| const requestRepository = getRepository(MediaRequest); | ||
| const requester = await seedRequester(5); | ||
|
|
||
| const results = await requestMovies([33333, 33333], requester); | ||
| const rejected = rejections(results); | ||
|
|
||
| assert.strictEqual(rejected.length, 1); | ||
| assert.ok(rejected[0].reason instanceof DuplicateMediaRequestError); | ||
| assert.strictEqual(await requestRepository.count(), 1); | ||
| assert.strictEqual(externalApiGetMock.callCount(), 2); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 AsyncLock from '@server/utils/asyncLock'; | ||
|
|
||
| // Keyed on user id. Never dispatch from a subscriber or transaction as a waiter | ||
| // would block while holding the save's connection. | ||
|
fallenbagel marked this conversation as resolved.
|
||
| const requestLock = new AsyncLock(); | ||
|
|
||
| export default requestLock; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.