-
Notifications
You must be signed in to change notification settings - Fork 55
feat: add throttle and delay to connect #478
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 all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d1cf8c9
feat: add throttle and delay to connect
DominicGBauer 241bb81
fix: name change
DominicGBauer 69811b7
chore: add changeset
DominicGBauer 274736a
feat: add options to web
DominicGBauer 823970f
chore: improve comment
DominicGBauer 961c961
chore: pr feedback
DominicGBauer 78d21c0
chore: remove optional flag
DominicGBauer 769b46f
chore: revert previous changes
DominicGBauer 6c4db3b
chore: avoid breaking change
DominicGBauer f4e5e88
chore: add changes
DominicGBauer a366e3d
chore: remove comments
DominicGBauer c332fdd
chore: pr feedback
DominicGBauer 0e6d380
chore: update tests
DominicGBauer 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,6 @@ | ||
--- | ||
'@powersync/react-native': minor | ||
'@powersync/common': minor | ||
--- | ||
|
||
Add `retryDelayMs` and `crudUploadThrottleMs` to `connect` so that the values can be dynamically changed upon reconnecting. |
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
151 changes: 151 additions & 0 deletions
151
packages/web/tests/src/db/AbstractPowerSyncDatabase.test.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,151 @@ | ||
import { describe, it, expect, vi } from 'vitest'; | ||
import { AbstractPowerSyncDatabase, DEFAULT_RETRY_DELAY_MS, DEFAULT_CRUD_UPLOAD_THROTTLE_MS, BucketStorageAdapter, DBAdapter, PowerSyncBackendConnector, PowerSyncDatabaseOptionsWithSettings, RequiredAdditionalConnectionOptions, StreamingSyncImplementation } from '@powersync/common'; | ||
import { testSchema } from '../../utils/testDb'; | ||
|
||
class TestPowerSyncDatabase extends AbstractPowerSyncDatabase { | ||
protected openDBAdapter(options: PowerSyncDatabaseOptionsWithSettings): DBAdapter { | ||
return {} as any | ||
} | ||
protected generateSyncStreamImplementation(connector: PowerSyncBackendConnector, options: RequiredAdditionalConnectionOptions): StreamingSyncImplementation { | ||
return undefined as any; | ||
} | ||
protected generateBucketStorageAdapter(): BucketStorageAdapter { | ||
return { | ||
init: vi.fn() | ||
} as any | ||
} | ||
_initialize(): Promise<void> { | ||
return Promise.resolve(); | ||
} | ||
|
||
get database() { | ||
return { | ||
get: vi.fn().mockResolvedValue({ version: '0.3.0'}), | ||
execute: vi.fn(), | ||
refreshSchema: vi.fn(), | ||
} as any | ||
} | ||
// Expose protected method for testing | ||
public testResolvedConnectionOptions(options?: any) { | ||
return this.resolvedConnectionOptions(options); | ||
} | ||
} | ||
|
||
describe('AbstractPowerSyncDatabase', () => { | ||
describe('resolvedConnectionOptions', () => { | ||
it('should use connect options when provided', () => { | ||
const db = new TestPowerSyncDatabase({ | ||
schema: testSchema, | ||
database: { dbFilename: 'test.db' } | ||
}); | ||
|
||
const result = db.testResolvedConnectionOptions({ | ||
retryDelayMs: 1000, | ||
crudUploadThrottleMs: 2000 | ||
}); | ||
|
||
expect(result).toEqual({ | ||
retryDelayMs: 1000, | ||
crudUploadThrottleMs: 2000 | ||
}); | ||
}); | ||
|
||
it('should fallback to constructor options when connect options not provided', () => { | ||
const db = new TestPowerSyncDatabase({ | ||
schema: testSchema, | ||
database: { dbFilename: 'test.db' }, | ||
retryDelayMs: 3000, | ||
crudUploadThrottleMs: 4000 | ||
}); | ||
|
||
const result = db.testResolvedConnectionOptions(); | ||
|
||
expect(result).toEqual({ | ||
retryDelayMs: 3000, | ||
crudUploadThrottleMs: 4000 | ||
}); | ||
}); | ||
|
||
it('should convert retryDelay to retryDelayMs', () => { | ||
const db = new TestPowerSyncDatabase({ | ||
schema: testSchema, | ||
database: { dbFilename: 'test.db' }, | ||
retryDelay: 5000 | ||
}); | ||
|
||
const result = db.testResolvedConnectionOptions(); | ||
|
||
expect(result).toEqual({ | ||
retryDelayMs: 5000, | ||
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS | ||
}); | ||
}); | ||
|
||
it('should prioritize retryDelayMs over retryDelay in constructor options', () => { | ||
const db = new TestPowerSyncDatabase({ | ||
schema: testSchema, | ||
database: { dbFilename: 'test.db' }, | ||
retryDelay: 5000, | ||
retryDelayMs: 6000 | ||
}); | ||
|
||
const result = db.testResolvedConnectionOptions(); | ||
|
||
expect(result).toEqual({ | ||
retryDelayMs: 6000, | ||
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS | ||
}); | ||
}); | ||
|
||
it('should prioritize connect options over constructor options', () => { | ||
const db = new TestPowerSyncDatabase({ | ||
schema: testSchema, | ||
database: { dbFilename: 'test.db' }, | ||
retryDelayMs: 5000, | ||
crudUploadThrottleMs: 6000 | ||
}); | ||
|
||
const result = db.testResolvedConnectionOptions({ | ||
retryDelayMs: 7000, | ||
crudUploadThrottleMs: 8000 | ||
}); | ||
|
||
expect(result).toEqual({ | ||
retryDelayMs: 7000, | ||
crudUploadThrottleMs: 8000 | ||
}); | ||
}); | ||
|
||
it('should use default values when no options provided', () => { | ||
const db = new TestPowerSyncDatabase({ | ||
schema: testSchema, | ||
database: { dbFilename: 'test.db' } | ||
}); | ||
|
||
const result = db.testResolvedConnectionOptions(); | ||
|
||
expect(result).toEqual({ | ||
retryDelayMs: DEFAULT_RETRY_DELAY_MS, | ||
crudUploadThrottleMs: DEFAULT_CRUD_UPLOAD_THROTTLE_MS | ||
}); | ||
}); | ||
|
||
it('should handle partial connect options', () => { | ||
const db = new TestPowerSyncDatabase({ | ||
schema: testSchema, | ||
database: { dbFilename: 'test.db' }, | ||
retryDelayMs: 5000, | ||
crudUploadThrottleMs: 6000 | ||
}); | ||
|
||
const result = db.testResolvedConnectionOptions({ | ||
retryDelayMs: 7000 | ||
}); | ||
|
||
expect(result).toEqual({ | ||
retryDelayMs: 7000, | ||
crudUploadThrottleMs: 6000 | ||
}); | ||
}); | ||
}); | ||
}); |
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.