From 96879286ce803eabb788645eede6f2a1520182f6 Mon Sep 17 00:00:00 2001 From: feywind <57276408+feywind@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:12:02 -0400 Subject: [PATCH] tests: add unit tests for maxBytes handling --- src/message-queues.ts | 2 +- test/message-queues.ts | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/message-queues.ts b/src/message-queues.ts index 828228e0a..b660012a4 100644 --- a/src/message-queues.ts +++ b/src/message-queues.ts @@ -64,7 +64,7 @@ export interface BatchOptions { // This is the maximum number of bytes we will send for a batch of // ack/modack messages. The server itself has a maximum of 512KiB, so // we just pull back a little from that in case of unknown fenceposts. -const MAX_BATCH_BYTES = 510 * 1024 * 1024; +export const MAX_BATCH_BYTES = 510 * 1024 * 1024; /** * Error class used to signal a batch failure. diff --git a/test/message-queues.ts b/test/message-queues.ts index 9505d0521..b5d42b768 100644 --- a/test/message-queues.ts +++ b/test/message-queues.ts @@ -24,7 +24,7 @@ import defer = require('p-defer'); import * as messageTypes from '../src/message-queues'; import {BatchError} from '../src/message-queues'; -import {AckError, Message, Subscriber} from '../src/subscriber'; +import {Message, Subscriber} from '../src/subscriber'; import {DebugMessage} from '../src/debug'; class FakeClient { @@ -160,6 +160,15 @@ describe('MessageQueues', () => { assert.strictEqual(stub.callCount, 1); }); + it('should flush the queue if at byte capacity', () => { + const stub = sandbox.stub(messageQueue, 'flush'); + + messageQueue.bytes = messageTypes.MAX_BATCH_BYTES - 10; + messageQueue.add(new FakeMessage() as Message); + + assert.strictEqual(stub.callCount, 1); + }); + it('should schedule a flush if needed', () => { const clock = sandbox.useFakeTimers(); const stub = sandbox.stub(messageQueue, 'flush'); @@ -214,6 +223,13 @@ describe('MessageQueues', () => { assert.strictEqual(messageQueue.numPendingRequests, 0); }); + it('should remove the bytes of messages from the queue', () => { + messageQueue.add(new FakeMessage() as Message); + messageQueue.flush(); + + assert.strictEqual(messageQueue.bytes, 0); + }); + it('should send the batch', () => { const message = new FakeMessage(); const deadline = 10;