-
Notifications
You must be signed in to change notification settings - Fork 36
fix: await upload session registration #50
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
Merged
+250
−41
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,107 @@ | ||
| import { describe, expect, test } from 'bun:test'; | ||
| import { createUploadSessionRegistrar } from './upload-session'; | ||
|
|
||
| function deferred(): { | ||
| promise: Promise<void>; | ||
| resolve: () => void; | ||
| reject: (error: Error) => void; | ||
| } { | ||
| let resolve!: () => void; | ||
| let reject!: (error: Error) => void; | ||
| const promise = new Promise<void>((resolvePromise, rejectPromise) => { | ||
| resolve = resolvePromise; | ||
| reject = rejectPromise; | ||
| }); | ||
| return { promise, resolve, reject }; | ||
| } | ||
|
|
||
| describe('createUploadSessionRegistrar', () => { | ||
| test('waits for session registration before forwarding a file', async () => { | ||
| const registration = deferred(); | ||
| const events: string[] = []; | ||
| const ensureSessionRegistered = createUploadSessionRegistrar((sessionKey) => { | ||
| events.push(`session:set:start:${sessionKey}`); | ||
| return registration.promise.then(() => { | ||
| events.push('session:set:done'); | ||
| }); | ||
| }); | ||
|
|
||
| const result = ensureSessionRegistered('user:user-1').then(async () => { | ||
| events.push('file:put'); | ||
| return 'uploaded'; | ||
| }); | ||
|
|
||
| await Promise.resolve(); | ||
| expect(events).toEqual(['session:set:start:user:user-1']); | ||
|
|
||
| registration.resolve(); | ||
| expect(await result).toBe('uploaded'); | ||
| expect(events).toEqual(['session:set:start:user:user-1', 'session:set:done', 'file:put']); | ||
| }); | ||
|
|
||
| test('shares one pending registration across every file in a batch', async () => { | ||
| const registration = deferred(); | ||
| let registrations = 0; | ||
| const forwarded: string[] = []; | ||
| const ensureSessionRegistered = createUploadSessionRegistrar(() => { | ||
| registrations += 1; | ||
| return registration.promise; | ||
| }); | ||
|
|
||
| const uploads = [ | ||
| ensureSessionRegistered('user:user-1').then(async () => { | ||
| forwarded.push('first'); | ||
| return 'first'; | ||
| }), | ||
| ensureSessionRegistered('user:user-1').then(async () => { | ||
| forwarded.push('second'); | ||
| return 'second'; | ||
| }), | ||
| ]; | ||
|
|
||
| await Promise.resolve(); | ||
| expect(registrations).toBe(1); | ||
| expect(forwarded).toEqual([]); | ||
|
|
||
| registration.resolve(); | ||
| expect(await Promise.all(uploads)).toEqual(['first', 'second']); | ||
| expect(forwarded).toEqual(['first', 'second']); | ||
| }); | ||
|
|
||
| test('does not forward files when session registration fails', async () => { | ||
| const registration = deferred(); | ||
| let forwarded = false; | ||
| const ensureSessionRegistered = createUploadSessionRegistrar(() => registration.promise); | ||
| const result = ensureSessionRegistered('user:user-1').then(async () => { | ||
| forwarded = true; | ||
| return 'uploaded'; | ||
| }); | ||
|
|
||
| registration.reject(new Error('Redis unavailable')); | ||
|
|
||
| await expect(result).rejects.toThrow('Redis unavailable'); | ||
| expect(forwarded).toBe(false); | ||
| }); | ||
|
|
||
| test('keeps a pending registration timeout terminal when Redis rejects later', async () => { | ||
| const registration = deferred(); | ||
| let forwarded = false; | ||
| const ensureSessionRegistered = createUploadSessionRegistrar(() => registration.promise); | ||
| const result = ensureSessionRegistered('user:user-1').then(async () => { | ||
| forwarded = true; | ||
| return 'uploaded'; | ||
| }); | ||
|
|
||
| expect(ensureSessionRegistered.rejectPending( | ||
| new Error('Upload session registration timed out'), | ||
| )).toBe(true); | ||
| await expect(result).rejects.toThrow('Upload session registration timed out'); | ||
| expect(forwarded).toBe(false); | ||
|
|
||
| registration.reject(new Error('Redis unavailable after timeout')); | ||
| await Promise.resolve(); | ||
|
|
||
| expect(forwarded).toBe(false); | ||
| expect(ensureSessionRegistered.rejectPending(new Error('too late'))).toBe(false); | ||
| }); | ||
| }); |
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,67 @@ | ||
| type RegisterUploadSession = (sessionKey: string) => Promise<unknown>; | ||
|
|
||
| export interface UploadSessionRegistrar { | ||
| (sessionKey: string): Promise<unknown>; | ||
| /** | ||
| * Rejects the shared registration barrier only while Redis is still | ||
| * pending. The underlying Redis promise remains observed, but its eventual | ||
| * outcome cannot reopen the barrier or forward a file after timeout. | ||
| */ | ||
| rejectPending(error: Error): boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a request-scoped session registrar. Every file shares the first | ||
| * registration promise, allowing callers to wait for the Redis write without | ||
| * issuing duplicate SETs for a batch. | ||
| */ | ||
| export function createUploadSessionRegistrar( | ||
| registerSession: RegisterUploadSession, | ||
| ): UploadSessionRegistrar { | ||
| let sessionRegistered: Promise<unknown> | undefined; | ||
| let registrationPending = false; | ||
| let resolveRegistration!: (value: unknown) => void; | ||
| let rejectRegistration!: (error: unknown) => void; | ||
|
|
||
| const ensureSessionRegistered = (sessionKey: string): Promise<unknown> => { | ||
| if (!sessionRegistered) { | ||
| registrationPending = true; | ||
| sessionRegistered = new Promise((resolve, reject) => { | ||
| resolveRegistration = resolve; | ||
| rejectRegistration = reject; | ||
| }); | ||
|
|
||
| let registration: Promise<unknown>; | ||
| try { | ||
| registration = registerSession(sessionKey); | ||
| } catch (error) { | ||
| registrationPending = false; | ||
| rejectRegistration(error); | ||
| return sessionRegistered; | ||
| } | ||
|
|
||
| void registration.then( | ||
| (value) => { | ||
| if (!registrationPending) return; | ||
| registrationPending = false; | ||
| resolveRegistration(value); | ||
| }, | ||
| (error: unknown) => { | ||
| if (!registrationPending) return; | ||
| registrationPending = false; | ||
| rejectRegistration(error); | ||
| }, | ||
| ); | ||
| } | ||
| return sessionRegistered; | ||
| }; | ||
|
|
||
| ensureSessionRegistered.rejectPending = (error: Error): boolean => { | ||
| if (!registrationPending) return false; | ||
| registrationPending = false; | ||
| rejectRegistration(error); | ||
| return true; | ||
| }; | ||
|
|
||
| return ensureSessionRegistered; | ||
| } |
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.