diff --git a/backend/src/realtime/realtime-note/realtime-note.spec.ts b/backend/src/realtime/realtime-note/realtime-note.spec.ts index 8a7cf7225..3685f0173 100644 --- a/backend/src/realtime/realtime-note/realtime-note.spec.ts +++ b/backend/src/realtime/realtime-note/realtime-note.spec.ts @@ -13,11 +13,16 @@ import { MockConnectionBuilder } from './test-utils/mock-connection'; describe('realtime note', () => { let mockedNote: Note; + beforeAll(() => { + jest.useFakeTimers(); + }); + beforeEach(() => { mockedNote = Mock.of({ id: 4711 }); }); afterAll(() => { + jest.useRealTimers(); jest.resetAllMocks(); jest.resetModules(); }); diff --git a/backend/src/realtime/realtime-note/test-utils/mock-connection.ts b/backend/src/realtime/realtime-note/test-utils/mock-connection.ts index d0a264af1..7d5da6db4 100644 --- a/backend/src/realtime/realtime-note/test-utils/mock-connection.ts +++ b/backend/src/realtime/realtime-note/test-utils/mock-connection.ts @@ -4,7 +4,6 @@ * SPDX-License-Identifier: AGPL-3.0-only */ import { - MessageTransporter, MockedBackendTransportAdapter, YDocSyncServerAdapter, } from '@hedgedoc/commons'; @@ -15,6 +14,7 @@ import { Username } from '../../../utils/username'; import { RealtimeConnection } from '../realtime-connection'; import { RealtimeNote } from '../realtime-note'; import { RealtimeUserStatusAdapter } from '../realtime-user-status-adapter'; +import { MockMessageTransporter } from './mock-message-transporter'; enum RealtimeUserState { WITHOUT, @@ -83,7 +83,7 @@ export class MockConnectionBuilder { public build(): RealtimeConnection { const displayName = this.deriveDisplayName(); - const transporter = new MessageTransporter(); + const transporter = new MockMessageTransporter(); transporter.setAdapter(new MockedBackendTransportAdapter('')); const realtimeUserStateAdapter: RealtimeUserStatusAdapter = this.includeRealtimeUserStatus === RealtimeUserState.WITHOUT @@ -125,6 +125,9 @@ export class MockConnectionBuilder { this.realtimeNote.addClient(connection); + transporter.markAsReady(); + jest.advanceTimersByTime(0); + return connection; } diff --git a/backend/src/realtime/realtime-note/test-utils/mock-message-transporter.ts b/backend/src/realtime/realtime-note/test-utils/mock-message-transporter.ts new file mode 100644 index 000000000..4c9c555e3 --- /dev/null +++ b/backend/src/realtime/realtime-note/test-utils/mock-message-transporter.ts @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { MessageTransporter, MessageType } from '@hedgedoc/commons'; + +/** + * A message transporter that is only used in testing where certain conditions like resending of requests isn't needed. + */ +export class MockMessageTransporter extends MessageTransporter { + protected startSendingOfReadyRequests(): void { + this.sendMessage({ + type: MessageType.READY_REQUEST, + }); + } + + protected stopSendingOfReadyRequests(): void { + //intentionally left blank + } +} diff --git a/commons/src/message-transporters/message-transporter.ts b/commons/src/message-transporters/message-transporter.ts index 5b3c4d371..c80b5089e 100644 --- a/commons/src/message-transporters/message-transporter.ts +++ b/commons/src/message-transporters/message-transporter.ts @@ -126,7 +126,7 @@ export class MessageTransporter extends EventEmitter2 { this.emit('ready') } - private stopSendingOfReadyRequests() { + protected stopSendingOfReadyRequests() { if (this.readyInterval !== undefined) { clearInterval(this.readyInterval) this.readyInterval = undefined @@ -242,7 +242,7 @@ export class MessageTransporter extends EventEmitter2 { this.startSendingOfReadyRequests() } - private startSendingOfReadyRequests(): void { + protected startSendingOfReadyRequests(): void { this.readyInterval = setInterval(() => { this.sendMessage({ type: MessageType.READY_REQUEST