tests: fix memory leaks (#2389)
This commit is contained in:
@@ -1,31 +1,37 @@
|
||||
/* eslint-disable no-unused-expressions */
|
||||
/* eslint-disable no-undef */
|
||||
import { BaseItemCounts } from '../lib/BaseItemCounts.js'
|
||||
import * as Factory from '../lib/factory.js'
|
||||
|
||||
chai.use(chaiAsPromised)
|
||||
const expect = chai.expect
|
||||
|
||||
describe('sync integrity', () => {
|
||||
before(function () {
|
||||
localStorage.clear()
|
||||
})
|
||||
|
||||
after(function () {
|
||||
localStorage.clear()
|
||||
})
|
||||
let application
|
||||
let email
|
||||
let password
|
||||
let expectedItemCount
|
||||
|
||||
beforeEach(async function () {
|
||||
this.expectedItemCount = BaseItemCounts.DefaultItemsWithAccount
|
||||
this.application = await Factory.createInitAppWithFakeCrypto()
|
||||
this.email = UuidGenerator.GenerateUuid()
|
||||
this.password = UuidGenerator.GenerateUuid()
|
||||
localStorage.clear()
|
||||
expectedItemCount = BaseItemCounts.DefaultItemsWithAccount
|
||||
application = await Factory.createInitAppWithFakeCrypto()
|
||||
email = UuidGenerator.GenerateUuid()
|
||||
password = UuidGenerator.GenerateUuid()
|
||||
await Factory.registerUserToApplication({
|
||||
application: this.application,
|
||||
email: this.email,
|
||||
password: this.password,
|
||||
application: application,
|
||||
email: email,
|
||||
password: password,
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async function () {
|
||||
expect(application.sync.isOutOfSync()).to.equal(false)
|
||||
const rawPayloads = await application.storage.getAllRawPayloads()
|
||||
expect(rawPayloads.length).to.equal(expectedItemCount)
|
||||
await Factory.safeDeinit(application)
|
||||
localStorage.clear()
|
||||
application = undefined
|
||||
})
|
||||
|
||||
const awaitSyncEventPromise = (application, targetEvent) => {
|
||||
return new Promise((resolve) => {
|
||||
application.sync.addEventObserver((event) => {
|
||||
@@ -36,44 +42,37 @@ describe('sync integrity', () => {
|
||||
})
|
||||
}
|
||||
|
||||
afterEach(async function () {
|
||||
expect(this.application.sync.isOutOfSync()).to.equal(false)
|
||||
const rawPayloads = await this.application.storage.getAllRawPayloads()
|
||||
expect(rawPayloads.length).to.equal(this.expectedItemCount)
|
||||
await Factory.safeDeinit(this.application)
|
||||
})
|
||||
|
||||
it('should detect when out of sync', async function () {
|
||||
const item = await this.application.mutator.emitItemFromPayload(
|
||||
const item = await application.mutator.emitItemFromPayload(
|
||||
Factory.createNotePayload(),
|
||||
PayloadEmitSource.LocalChanged,
|
||||
)
|
||||
this.expectedItemCount++
|
||||
expectedItemCount++
|
||||
|
||||
const didEnterOutOfSync = awaitSyncEventPromise(this.application, SyncEvent.EnterOutOfSync)
|
||||
await this.application.sync.sync({ checkIntegrity: true })
|
||||
const didEnterOutOfSync = awaitSyncEventPromise(application, SyncEvent.EnterOutOfSync)
|
||||
await application.sync.sync({ checkIntegrity: true })
|
||||
|
||||
await this.application.items.removeItemFromMemory(item)
|
||||
await this.application.sync.sync({ checkIntegrity: true, awaitAll: true })
|
||||
await application.items.removeItemFromMemory(item)
|
||||
await application.sync.sync({ checkIntegrity: true, awaitAll: true })
|
||||
|
||||
await didEnterOutOfSync
|
||||
})
|
||||
|
||||
it('should self heal after out of sync', async function () {
|
||||
const item = await this.application.mutator.emitItemFromPayload(
|
||||
const item = await application.mutator.emitItemFromPayload(
|
||||
Factory.createNotePayload(),
|
||||
PayloadEmitSource.LocalChanged,
|
||||
)
|
||||
this.expectedItemCount++
|
||||
expectedItemCount++
|
||||
|
||||
const didEnterOutOfSync = awaitSyncEventPromise(this.application, SyncEvent.EnterOutOfSync)
|
||||
const didExitOutOfSync = awaitSyncEventPromise(this.application, SyncEvent.ExitOutOfSync)
|
||||
const didEnterOutOfSync = awaitSyncEventPromise(application, SyncEvent.EnterOutOfSync)
|
||||
const didExitOutOfSync = awaitSyncEventPromise(application, SyncEvent.ExitOutOfSync)
|
||||
|
||||
await this.application.sync.sync({ checkIntegrity: true })
|
||||
await this.application.items.removeItemFromMemory(item)
|
||||
await this.application.sync.sync({ checkIntegrity: true, awaitAll: true })
|
||||
await application.sync.sync({ checkIntegrity: true })
|
||||
await application.items.removeItemFromMemory(item)
|
||||
await application.sync.sync({ checkIntegrity: true, awaitAll: true })
|
||||
|
||||
await Promise.all([didEnterOutOfSync, didExitOutOfSync])
|
||||
expect(this.application.sync.isOutOfSync()).to.equal(false)
|
||||
expect(application.sync.isOutOfSync()).to.equal(false)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user