From 43fb2ba11eb32fd1606d3ada4b7204ed3dcdf75a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emanuel=20Tesa=C5=99?= Date: Mon, 9 Oct 2023 16:13:13 +0200 Subject: [PATCH] Fix bug with data pruning --- packages/pusher/src/state.test.ts | 18 ++++++++++++++++++ packages/pusher/src/state.ts | 1 + 2 files changed, 19 insertions(+) diff --git a/packages/pusher/src/state.test.ts b/packages/pusher/src/state.test.ts index f0857742..c06d8c8f 100644 --- a/packages/pusher/src/state.test.ts +++ b/packages/pusher/src/state.test.ts @@ -61,4 +61,22 @@ describe(DelayedSignedDataQueue.name, () => { expect(queue.getAll()).toStrictEqual([data2, data3]); }); + + it('keeps data in the queue if none of the items exceed maxUpdateDelay', () => { + jest.useFakeTimers().setSystemTime(new Date('2023-01-20')); + const queue = new DelayedSignedDataQueue(30); + const data = nodarySignedTemplateResponses[0]![1]; + const timestamp = Number.parseInt(data.timestamp, 10); + const oldData = [ + { ...data, timestamp: (timestamp - 20).toString() }, + { ...data, timestamp: (timestamp - 15).toString() }, + { ...data, timestamp: (timestamp - 10).toString() }, + ]; + for (const item of oldData) queue.put(item); + + queue.prune(); + + // All data points remain in the queue. + expect(queue.getAll()).toStrictEqual(oldData); + }); }); diff --git a/packages/pusher/src/state.ts b/packages/pusher/src/state.ts index 29c27af4..283b0d43 100644 --- a/packages/pusher/src/state.ts +++ b/packages/pusher/src/state.ts @@ -154,6 +154,7 @@ export class DelayedSignedDataQueue { this.isDelayedEnough(data, Date.now() / 1000 - this.maxUpdateDelay) ); + if (index === -1) return; this.storage = this.storage.slice(index); }