-
Notifications
You must be signed in to change notification settings - Fork 7
Implement polling for topics #388
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
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1ec0378
Implement polling for topics
kibertoad 4579bde
Cleanup
kibertoad 2b833a8
Cleanup
kibertoad dbc10e1
Address review comments
kibertoad 8970771
Address review comments
kibertoad cf023cc
Add missing test
kibertoad bbed226
Add non-blocking mode
kibertoad d24e84b
Cleanup
kibertoad fcada62
Add extra tests
kibertoad d03af62
Address code review comments
kibertoad f88b9e5
add more tests
kibertoad 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
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
186 changes: 186 additions & 0 deletions
186
packages/core/lib/utils/resourceAvailabilityUtils.spec.ts
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,186 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { | ||
| isResourceAvailabilityWaitingEnabled, | ||
| ResourceAvailabilityTimeoutError, | ||
| waitForResource, | ||
| } from './resourceAvailabilityUtils.ts' | ||
|
|
||
| describe('resourceAvailabilityUtils', () => { | ||
| describe('isResourceAvailabilityWaitingEnabled', () => { | ||
| it('returns true when enabled is true', () => { | ||
| expect(isResourceAvailabilityWaitingEnabled({ enabled: true })).toBe(true) | ||
| }) | ||
|
|
||
| it('returns true when enabled is not specified (defaults to true)', () => { | ||
| expect(isResourceAvailabilityWaitingEnabled({})).toBe(true) | ||
| expect(isResourceAvailabilityWaitingEnabled({ pollingIntervalMs: 1000 })).toBe(true) | ||
| expect(isResourceAvailabilityWaitingEnabled({ timeoutMs: 5000 })).toBe(true) | ||
| }) | ||
|
|
||
| it('returns false when enabled is false', () => { | ||
| expect(isResourceAvailabilityWaitingEnabled({ enabled: false })).toBe(false) | ||
| }) | ||
|
|
||
| it('returns false when config is undefined', () => { | ||
| expect(isResourceAvailabilityWaitingEnabled(undefined)).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('waitForResource', () => { | ||
| it('returns immediately when resource is available on first check', async () => { | ||
| const checkFn = vi.fn().mockResolvedValue({ isAvailable: true, result: 'test-result' }) | ||
|
|
||
| const result = await waitForResource({ | ||
| config: { enabled: true, pollingIntervalMs: 100 }, | ||
| checkFn, | ||
| resourceName: 'test-resource', | ||
| }) | ||
|
|
||
| expect(result).toBe('test-result') | ||
| expect(checkFn).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('polls until resource becomes available', async () => { | ||
| let callCount = 0 | ||
| const checkFn = vi.fn().mockImplementation(() => { | ||
| callCount++ | ||
| if (callCount < 3) { | ||
| return Promise.resolve({ isAvailable: false }) | ||
| } | ||
| return Promise.resolve({ isAvailable: true, result: 'test-result' }) | ||
| }) | ||
|
|
||
| const result = await waitForResource({ | ||
| config: { enabled: true, pollingIntervalMs: 10 }, | ||
| checkFn, | ||
| resourceName: 'test-resource', | ||
| }) | ||
|
|
||
| expect(result).toBe('test-result') | ||
| expect(checkFn).toHaveBeenCalledTimes(3) | ||
| }) | ||
|
|
||
| it('throws ResourceAvailabilityTimeoutError when timeout is reached', async () => { | ||
| const checkFn = vi.fn().mockResolvedValue({ isAvailable: false }) | ||
|
|
||
| await expect( | ||
| waitForResource({ | ||
| config: { enabled: true, pollingIntervalMs: 10, timeoutMs: 50 }, | ||
| checkFn, | ||
| resourceName: 'test-resource', | ||
| }), | ||
| ).rejects.toThrow(ResourceAvailabilityTimeoutError) | ||
|
|
||
| // Should have made at least a few attempts | ||
| expect(checkFn.mock.calls.length).toBeGreaterThan(0) | ||
| }) | ||
|
|
||
| it('throws the original error when checkFn throws', async () => { | ||
| const checkFn = vi.fn().mockRejectedValue(new Error('Unexpected error')) | ||
|
|
||
| await expect( | ||
| waitForResource({ | ||
| config: { enabled: true, pollingIntervalMs: 10 }, | ||
| checkFn, | ||
| resourceName: 'test-resource', | ||
| }), | ||
| ).rejects.toThrow('Unexpected error') | ||
|
|
||
| expect(checkFn).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it('uses default polling interval when not specified', async () => { | ||
| const checkFn = vi.fn().mockResolvedValue({ isAvailable: true, result: 'test-result' }) | ||
|
|
||
| const result = await waitForResource({ | ||
| config: { enabled: true }, | ||
| checkFn, | ||
| resourceName: 'test-resource', | ||
| }) | ||
|
|
||
| expect(result).toBe('test-result') | ||
| }) | ||
|
|
||
| it('logs progress when logger is provided', async () => { | ||
| const logger = { | ||
| info: vi.fn(), | ||
| debug: vi.fn(), | ||
| error: vi.fn(), | ||
| } | ||
| const checkFn = vi.fn().mockResolvedValue({ isAvailable: true, result: 'test-result' }) | ||
|
|
||
| await waitForResource({ | ||
| config: { enabled: true, pollingIntervalMs: 10 }, | ||
| checkFn, | ||
| resourceName: 'test-resource', | ||
| // @ts-expect-error - partial logger for testing | ||
| logger, | ||
| }) | ||
|
|
||
| expect(logger.info).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| message: expect.stringContaining('Waiting for resource'), | ||
| resourceName: 'test-resource', | ||
| }), | ||
| ) | ||
| expect(logger.info).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| message: expect.stringContaining('is now available'), | ||
| resourceName: 'test-resource', | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| it('polls indefinitely when timeoutMs is not set', async () => { | ||
| let callCount = 0 | ||
| const checkFn = vi.fn().mockImplementation(() => { | ||
| callCount++ | ||
| if (callCount < 10) { | ||
| return Promise.resolve({ isAvailable: false }) | ||
| } | ||
| return Promise.resolve({ isAvailable: true, result: 'test-result' }) | ||
| }) | ||
|
|
||
| const result = await waitForResource({ | ||
| config: { enabled: true, pollingIntervalMs: 1 }, | ||
| checkFn, | ||
| resourceName: 'test-resource', | ||
| }) | ||
|
|
||
| expect(result).toBe('test-result') | ||
| expect(checkFn).toHaveBeenCalledTimes(10) | ||
| }) | ||
|
|
||
| it('polls indefinitely when timeoutMs is 0', async () => { | ||
| let callCount = 0 | ||
| const checkFn = vi.fn().mockImplementation(() => { | ||
| callCount++ | ||
| if (callCount < 5) { | ||
| return Promise.resolve({ isAvailable: false }) | ||
| } | ||
| return Promise.resolve({ isAvailable: true, result: 'test-result' }) | ||
| }) | ||
|
|
||
| const result = await waitForResource({ | ||
| config: { enabled: true, pollingIntervalMs: 1, timeoutMs: 0 }, | ||
| checkFn, | ||
| resourceName: 'test-resource', | ||
| }) | ||
|
|
||
| expect(result).toBe('test-result') | ||
| expect(checkFn).toHaveBeenCalledTimes(5) | ||
| }) | ||
| }) | ||
|
|
||
| describe('ResourceAvailabilityTimeoutError', () => { | ||
| it('includes resource name and timeout in message', () => { | ||
| const error = new ResourceAvailabilityTimeoutError('my-queue', 5000) | ||
|
|
||
| expect(error.message).toContain('my-queue') | ||
| expect(error.message).toContain('5000') | ||
| expect(error.resourceName).toBe('my-queue') | ||
| expect(error.timeoutMs).toBe(5000) | ||
| expect(error.name).toBe('ResourceAvailabilityTimeoutError') | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.