-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add throttle and delay to connect (#478)
- Loading branch information
1 parent
a4895cc
commit 0f28fb3
Showing
7 changed files
with
290 additions
and
28 deletions.
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,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 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
151 changes: 151 additions & 0 deletions
151
packages/web/tests/src/db/AbstractPowerSyncDatabase.test.ts
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,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.