chore: global sync per minute safety limit (#2765)

This commit is contained in:
Karol Sójko
2024-01-11 15:26:09 +01:00
committed by GitHub
parent 3d895ca499
commit cf4b5ccf0a
11 changed files with 140 additions and 10 deletions

View File

@@ -16,13 +16,14 @@ const MaximumSyncOptions = {
let GlobalSubscriptionIdCounter = 1001
export class AppContext {
constructor({ identifier, crypto, email, password, passcode, host } = {}) {
constructor({ identifier, crypto, email, password, passcode, host, syncCallsThresholdPerMinute } = {}) {
this.identifier = identifier || `${Math.random()}`
this.crypto = crypto
this.email = email || UuidGenerator.GenerateUuid()
this.password = password || UuidGenerator.GenerateUuid()
this.passcode = passcode || 'mypasscode'
this.host = host || Defaults.getDefaultHost()
this.syncCallsThresholdPerMinute = syncCallsThresholdPerMinute
}
enableLogging() {
@@ -46,6 +47,7 @@ export class AppContext {
undefined,
this.host,
this.crypto || new FakeWebCrypto(),
this.syncCallsThresholdPerMinute,
)
this.application.dependencies.get(TYPES.Logger).setLevel('error')

View File

@@ -2,7 +2,7 @@ import WebDeviceInterface from './web_device_interface.js'
import FakeWebCrypto from './fake_web_crypto.js'
import * as Defaults from './Defaults.js'
export function createApplicationWithOptions({ identifier, environment, platform, host, crypto, device }) {
export function createApplicationWithOptions({ identifier, environment, platform, host, crypto, device, syncCallsThresholdPerMinute }) {
if (!device) {
device = new WebDeviceInterface()
device.environment = environment
@@ -22,11 +22,12 @@ export function createApplicationWithOptions({ identifier, environment, platform
defaultHost: host || Defaults.getDefaultHost(),
appVersion: Defaults.getAppVersion(),
webSocketUrl: Defaults.getDefaultWebSocketUrl(),
syncCallsThresholdPerMinute,
})
}
export function createApplication(identifier, environment, platform, host, crypto) {
return createApplicationWithOptions({ identifier, environment, platform, host, crypto })
export function createApplication(identifier, environment, platform, host, crypto, syncCallsThresholdPerMinute) {
return createApplicationWithOptions({ identifier, environment, platform, host, crypto, syncCallsThresholdPerMinute })
}
export function createApplicationWithFakeCrypto(identifier, environment, platform, host) {

View File

@@ -43,16 +43,16 @@ export async function createAndInitSimpleAppContext(
}
}
export async function createAppContextWithFakeCrypto(identifier, email, password) {
return createAppContext({ identifier, crypto: new FakeWebCrypto(), email, password })
export async function createAppContextWithFakeCrypto(identifier, email, password, syncCallsThresholdPerMinute) {
return createAppContext({ identifier, crypto: new FakeWebCrypto(), email, password, syncCallsThresholdPerMinute })
}
export async function createAppContextWithRealCrypto(identifier) {
return createAppContext({ identifier, crypto: new SNWebCrypto() })
}
export async function createAppContext({ identifier, crypto, email, password, host } = {}) {
const context = new AppContext({ identifier, crypto, email, password, host })
export async function createAppContext({ identifier, crypto, email, password, host, syncCallsThresholdPerMinute } = {}) {
const context = new AppContext({ identifier, crypto, email, password, host, syncCallsThresholdPerMinute })
await context.initialize()
return context
}

View File

@@ -13,6 +13,7 @@ describe('online syncing', function () {
let password
let expectedItemCount
let context
let safeGuard
const syncOptions = {
checkIntegrity: true,
@@ -37,6 +38,10 @@ describe('online syncing', function () {
email: email,
password: password,
})
safeGuard = application.dependencies.get(TYPES.SyncFrequencyGuard)
safeGuard.clear()
})
afterEach(async function () {
@@ -50,8 +55,11 @@ describe('online syncing', function () {
await Factory.safeDeinit(application)
localStorage.clear()
safeGuard.clear()
application = undefined
context = undefined
safeGuard = undefined
})
function noteObjectsFromObjects(items) {
@@ -433,6 +441,20 @@ describe('online syncing', function () {
expect(allItems.length).to.equal(expectedItemCount)
})
it('should defer syncing if syncing is breaching the sync calls per minute threshold', async function () {
let syncCount = 0
while(!safeGuard.isSyncCallsThresholdReachedThisMinute()) {
await application.sync.sync({
onPresyncSave: () => {
syncCount++
}
})
}
expect(safeGuard.isSyncCallsThresholdReachedThisMinute()).to.equal(true)
expect(syncCount == 200).to.equal(true)
})
it('items that are never synced and deleted should not be uploaded to server', async function () {
const note = await Factory.createMappedNote(application)
await application.mutator.setItemDirty(note)
@@ -575,7 +597,7 @@ describe('online syncing', function () {
it('should sync all items including ones that are breaching transfer limit', async function () {
const response = await fetch('/mocha/assets/small_file.md')
const buffer = new Uint8Array(await response.arrayBuffer())
const numberOfNotesToExceedThe1MBTransferLimit = 80
const numberOfNotesToExceedThe1MBTransferLimit = Math.ceil(100_000 / buffer.length) + 1
const testContext = await Factory.createAppContextWithFakeCrypto()
await testContext.launch()