From 54f5cce5eb540e3e9fb8f5c0bf6197acec3bf68d Mon Sep 17 00:00:00 2001 From: Erwan Guyader Date: Tue, 5 Nov 2024 17:15:02 +0100 Subject: [PATCH] test: Prevent excessive errors during tests Some tests are the cause of errors (while not turning red) as they start background processings which are not stopped at the end of the tests and end up trying to access a destroyed database. Stopping these processings will prevent those errors and might improve test performance. --- core/local/channel_watcher/dispatch.js | 12 ++++- test/integration/add.js | 3 ++ test/integration/case_or_encoding_change.js | 3 ++ test/integration/conflict_resolution.js | 3 ++ test/integration/differential_sync.js | 3 ++ test/integration/executable.js | 3 ++ test/integration/full_loop.js | 3 ++ test/integration/id_conflict.js | 3 ++ test/integration/interrupted_sync.js | 3 ++ test/integration/move.js | 3 ++ test/integration/mtime-update.js | 3 ++ test/integration/notes.js | 3 ++ test/integration/permanent_deletion.js | 3 ++ .../integration/platform_incompatibilities.js | 3 ++ test/integration/sync_state.js | 3 ++ test/integration/trash.js | 3 ++ test/integration/update.js | 3 ++ test/unit/remote/offline.js | 23 ++++++---- test/unit/remote/realtime_manager.js | 45 ++++++++++++------- 19 files changed, 103 insertions(+), 25 deletions(-) diff --git a/core/local/channel_watcher/dispatch.js b/core/local/channel_watcher/dispatch.js index c6f7cd956..daa8a7f18 100644 --- a/core/local/channel_watcher/dispatch.js +++ b/core/local/channel_watcher/dispatch.js @@ -117,7 +117,17 @@ async function dispatchEvent( // Lock to prevent Merge/Sync conflicts const release = await opts.pouch.lock(component) try { - await actions[event.action + event.kind](event, opts) + switch (event.action) { + case 'scan': + case 'created': + case 'modified': + case 'renamed': + case 'deleted': + await actions[event.action + event.kind](event, opts) + break + default: + log.warn('could not dispatch event with invalid action', { event }) + } try { const target = ( await opts.pouch.db.changes({ diff --git a/test/integration/add.js b/test/integration/add.js index 23961e59d..8371d453a 100644 --- a/test/integration/add.js +++ b/test/integration/add.js @@ -38,6 +38,9 @@ describe('Add', () => { helpers.spyPouch() }) + afterEach(async function () { + await helpers.stop() + }) afterEach(() => helpers.local.clean()) afterEach(pouchHelpers.cleanDatabase) diff --git a/test/integration/case_or_encoding_change.js b/test/integration/case_or_encoding_change.js index 6a114168e..bd30dd1c6 100644 --- a/test/integration/case_or_encoding_change.js +++ b/test/integration/case_or_encoding_change.js @@ -27,6 +27,9 @@ describe('Case or encoding change', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) describe('directory', () => { let dir, dir2 diff --git a/test/integration/conflict_resolution.js b/test/integration/conflict_resolution.js index ed7e6883a..73c84efdb 100644 --- a/test/integration/conflict_resolution.js +++ b/test/integration/conflict_resolution.js @@ -37,6 +37,9 @@ describe('Conflict resolution', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) describe('local', () => { beforeEach('create and merge conflicting remote file', async () => { diff --git a/test/integration/differential_sync.js b/test/integration/differential_sync.js index 9662b1ffd..65685bcca 100644 --- a/test/integration/differential_sync.js +++ b/test/integration/differential_sync.js @@ -45,6 +45,9 @@ describe('Differential synchronization', () => { builders = new Builders({ cozy }) }) + afterEach(async function () { + await helpers.stop() + }) let remoteDir, remoteFile beforeEach(async function () { diff --git a/test/integration/executable.js b/test/integration/executable.js index 2078ed2f8..1774d9ed8 100644 --- a/test/integration/executable.js +++ b/test/integration/executable.js @@ -33,6 +33,9 @@ describe('Executable handling', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) const executableStatus = async relpath => { const mode = await syncDir.octalMode(relpath) diff --git a/test/integration/full_loop.js b/test/integration/full_loop.js index ed780a995..3c9847311 100644 --- a/test/integration/full_loop.js +++ b/test/integration/full_loop.js @@ -32,6 +32,9 @@ describe('Full watch/merge/sync/repeat loop', () => { helpers.spyPouch() }) + afterEach(async function () { + await helpers.stop() + }) it('remote -> local add file', async () => { await helpers._local.watcher.start() diff --git a/test/integration/id_conflict.js b/test/integration/id_conflict.js index 950258468..3ef26e18d 100644 --- a/test/integration/id_conflict.js +++ b/test/integration/id_conflict.js @@ -35,6 +35,9 @@ describe('Identity conflict', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) describe('between two dirs', () => { describe('both remote', () => { diff --git a/test/integration/interrupted_sync.js b/test/integration/interrupted_sync.js index 0ff340174..c72a6106f 100644 --- a/test/integration/interrupted_sync.js +++ b/test/integration/interrupted_sync.js @@ -33,6 +33,9 @@ describe('Sync gets interrupted, initialScan occurs', () => { helpers.spyPouch() }) + afterEach(async function () { + await helpers.stop() + }) it('move Folder', async () => { const docs = await helpers.remote.createTree(['/a/', '/b/']) diff --git a/test/integration/move.js b/test/integration/move.js index 7077511a9..2cc60fedd 100644 --- a/test/integration/move.js +++ b/test/integration/move.js @@ -55,6 +55,9 @@ describe('Move', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) afterEach(() => helpers.local.clean()) afterEach(pouchHelpers.cleanDatabase) diff --git a/test/integration/mtime-update.js b/test/integration/mtime-update.js index 20002ab92..33c259b44 100644 --- a/test/integration/mtime-update.js +++ b/test/integration/mtime-update.js @@ -26,6 +26,9 @@ describe('Update only mtime', () => { helpers = TestHelpers.init(this) helpers.local.setupTrash() }) + afterEach(async function () { + await helpers.stop() + }) afterEach(() => helpers.local.clean()) afterEach(pouchHelpers.cleanDatabase) diff --git a/test/integration/notes.js b/test/integration/notes.js index 2c38fd145..405f51e23 100644 --- a/test/integration/notes.js +++ b/test/integration/notes.js @@ -33,6 +33,9 @@ describe('Update', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) describe('Cozy Note', () => { let note diff --git a/test/integration/permanent_deletion.js b/test/integration/permanent_deletion.js index 56efc760f..383444aaa 100644 --- a/test/integration/permanent_deletion.js +++ b/test/integration/permanent_deletion.js @@ -27,6 +27,9 @@ describe('Permanent deletion remote', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) it('file', async () => { const file = await cozy.files.create('File content', { name: 'file' }) diff --git a/test/integration/platform_incompatibilities.js b/test/integration/platform_incompatibilities.js index 504a675d5..179a0c606 100644 --- a/test/integration/platform_incompatibilities.js +++ b/test/integration/platform_incompatibilities.js @@ -54,6 +54,9 @@ describe('Platform incompatibilities', () => { helpers._sync.lifecycle.unblockFor(err.code) }) }) + afterEach(async function () { + await helpers.stop() + }) const shouldHaveBlockedFor = dpath => Array.isArray(dpath) diff --git a/test/integration/sync_state.js b/test/integration/sync_state.js index f21a37bfc..432b9bf81 100644 --- a/test/integration/sync_state.js +++ b/test/integration/sync_state.js @@ -31,6 +31,9 @@ describe('Sync state', () => { await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) it('1 sync error (missing remote file)', async () => { // XXX: Get current PouchDB sequence (it's not 0 since we have design docs) diff --git a/test/integration/trash.js b/test/integration/trash.js index 4126d6b27..470a4b8a1 100644 --- a/test/integration/trash.js +++ b/test/integration/trash.js @@ -39,6 +39,9 @@ describe('Trash', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) describe('file', () => { let parent, file diff --git a/test/integration/update.js b/test/integration/update.js index 7a756b6b6..f03136dae 100644 --- a/test/integration/update.js +++ b/test/integration/update.js @@ -43,6 +43,9 @@ describe('Update file', () => { await helpers.local.setupTrash() await helpers.remote.ignorePreviousChanges() }) + afterEach(async function () { + await helpers.stop() + }) describe('local offline change with unsynced previous local change', () => { beforeEach(async () => { diff --git a/test/unit/remote/offline.js b/test/unit/remote/offline.js index 4fedd13af..37b18196f 100644 --- a/test/unit/remote/offline.js +++ b/test/unit/remote/offline.js @@ -48,18 +48,23 @@ describe('Remote', function () { sinon.spy(this.events, 'emit') await this.remote.start() - should(this.events.emit).have.been.calledWithMatch( - 'RemoteWatcher:error', - { code: remoteErrors.UNREACHABLE_COZY_CODE } - ) - fetchStub.restore() - this.events.emit.resetHistory() + try { + should(this.events.emit).have.been.calledWithMatch( + 'RemoteWatcher:error', + { code: remoteErrors.UNREACHABLE_COZY_CODE } + ) - await this.remote.watcher.watch() - should(this.events.emit).not.have.been.calledWith('RemoteWatcher:error') + fetchStub.restore() + this.events.emit.resetHistory() - this.events.emit.restore() + await this.remote.watcher.watch() + should(this.events.emit).not.have.been.calledWith('RemoteWatcher:error') + + this.events.emit.restore() + } finally { + await this.remote.stop() + } }).timeout(120000) }) }) diff --git a/test/unit/remote/realtime_manager.js b/test/unit/remote/realtime_manager.js index 42e772dc1..b683f9abb 100644 --- a/test/unit/remote/realtime_manager.js +++ b/test/unit/remote/realtime_manager.js @@ -22,7 +22,8 @@ const setup = async () => { return { eventHandler, realtime: realtimeManager.realtime, - realtimeManager + realtimeManager, + teardown: client.logout } } @@ -33,7 +34,7 @@ describe('RealtimeManager', function () { describe('start', () => { it('subscribes to all io.cozy.files realtime events', async function () { - const { realtime, realtimeManager } = await setup() + const { teardown, realtime, realtimeManager } = await setup() const subscribeSpy = sinon.spy(realtime, 'subscribe') @@ -57,13 +58,14 @@ describe('RealtimeManager', function () { ) } finally { subscribeSpy.restore() + await teardown() } }) }) describe('stop', () => { it('removes all subscriptions', async function () { - const { realtime, realtimeManager } = await setup() + const { teardown, realtime, realtimeManager } = await setup() const unsubscribeSpy = sinon.spy(realtime, 'unsubscribe') @@ -87,40 +89,53 @@ describe('RealtimeManager', function () { ) } finally { unsubscribeSpy.restore() + await teardown() } }) }) describe('onCreated', () => { it('calls event handler for a created realtime event', async function () { - const { eventHandler, realtimeManager } = await setup() + const { teardown, eventHandler, realtimeManager } = await setup() - // $FlowFixMe we don't care about the type of doc passed here - realtimeManager.onCreated({}) + try { + // $FlowFixMe we don't care about the type of doc passed here + realtimeManager.onCreated({}) - should(eventHandler).have.been.calledOnce() + should(eventHandler).have.been.calledOnce() + } finally { + await teardown() + } }) }) describe('onUpdated', () => { it('calls event handler for an updated realtime event', async function () { - const { eventHandler, realtimeManager } = await setup() + const { teardown, eventHandler, realtimeManager } = await setup() - // $FlowFixMe we don't care about the type of doc passed here - realtimeManager.onUpdated({}) + try { + // $FlowFixMe we don't care about the type of doc passed here + realtimeManager.onUpdated({}) - should(eventHandler).have.been.calledOnce() + should(eventHandler).have.been.calledOnce() + } finally { + await teardown() + } }) }) describe('onDeleted', () => { it('calls event handler for a deleted realtime event', async function () { - const { eventHandler, realtimeManager } = await setup() + const { teardown, eventHandler, realtimeManager } = await setup() - // $FlowFixMe we don't care about the type of doc passed here - realtimeManager.onDeleted({}) + try { + // $FlowFixMe we don't care about the type of doc passed here + realtimeManager.onDeleted({}) - should(eventHandler).have.been.calledOnce() + should(eventHandler).have.been.calledOnce() + } finally { + await teardown() + } }) }) })