-
Notifications
You must be signed in to change notification settings - Fork 89
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
fix: fixes cases re throttledPromised: abort misbehaviour and not awaited queue #865
Closed
Closed
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5254a51
tests: include integration tests in tsconfig
alexjoverm ac8a451
fix: Add conditional to prevent an aborted queue to keep adding promises
alexjoverm 407d4fb
fix: ensure the queue is processed in right order and awaited after c…
alexjoverm 7ebd501
test: fix typescript config in the tests
alexjoverm cb0dd31
chore: light refactor and fix types
alexjoverm bf72e24
Merge branch 'main' into fix/PRO-803-throttle-issue
alexjoverm 16bdf68
fix: update the throttle promise
alexjoverm 7289f85
Merge branch 'fix/PRO-803-throttle-issue' of https://github.com/story…
alexjoverm 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 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,48 @@ | ||
import { describe, it, expect, vi } from 'vitest' | ||
import throttledQueue from './throttlePromise' | ||
|
||
// Mock function to simulate async work with a delay | ||
const mockFn = vi.fn(async (input) => { | ||
await new Promise((resolve) => setTimeout(resolve, 200)) // Simulate async delay | ||
return input | ||
}) | ||
|
||
describe('throttledQueue', () => { | ||
it('should resolve or reject all promises after the queue finishes, even when aborting', async () => { | ||
const throttled = throttledQueue(mockFn, 3, 10) // Throttle with 3 concurrent tasks | ||
const promises: Promise<any>[] = [] | ||
|
||
// Generate 10 tasks and push them to the promises array | ||
for (let i = 0; i < 10; i++) { | ||
promises.push(throttled(i)) | ||
if (i === 5) { | ||
throttled.abort() // but abort at call #6 | ||
} | ||
} | ||
|
||
const results = await Promise.allSettled(promises) | ||
results.forEach((result) => { | ||
expect(['fulfilled', 'rejected']).toContain(result.status) | ||
}) | ||
}) | ||
it('should enforce sequential resolution when throttle limit is exceeded', async () => { | ||
const throttled = throttledQueue(mockFn, 1, 100) // Limit of 1, 100ms interval | ||
|
||
const start = Date.now() | ||
const promises = [ | ||
throttled('test1'), | ||
throttled('test2'), | ||
throttled('test3'), | ||
] | ||
|
||
const results = await Promise.all(promises) | ||
const duration = Date.now() - start | ||
|
||
// Expected behavior: | ||
// Since each call has a 200ms delay, and there's a 100ms throttle interval and limit is 1, | ||
// and each successive call should only start after the previous one completes, | ||
// then the total duration should be around 800ms (200*3 + 100*2). | ||
expect(results).toEqual(['test1', 'test2', 'test3']) | ||
expect(duration).toBeGreaterThanOrEqual(800) | ||
}) | ||
}) |
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
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,8 +1,14 @@ | ||
import { defineConfig } from 'vite' | ||
import path from 'path' | ||
|
||
export default defineConfig({ | ||
test: { | ||
include: ['./tests/**/*.e2e.ts'], | ||
setupFiles: ['./tests/setup.js'], | ||
}, | ||
resolve: { | ||
alias: { | ||
'storyblok-js-client': path.resolve(__dirname, 'dist'), | ||
}, | ||
}, | ||
}) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: @alexjoverm Can we do anything to avoid using
any
here? I will leave it to your personal criteria if it's something we should do on the scope of this PR or in the v7 refactor. In normal cases I would block a PR from merging if there areany
on the typesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do, will need some extra help as I think we can do this type a bit more precise