From b54492592f99065013add1b2a75328e97c8e797b Mon Sep 17 00:00:00 2001 From: gabaldon Date: Tue, 6 Feb 2024 16:26:05 +0100 Subject: [PATCH] fix(test): `syncingTimeEstimator.spec.ts` --- src/services/SyncingTimeEstimator.js | 5 +- .../src/services/SyncingTimeEstimator.spec.js | 57 +++++++++++-------- test/unit/utils.js | 8 +++ 3 files changed, 43 insertions(+), 27 deletions(-) diff --git a/src/services/SyncingTimeEstimator.js b/src/services/SyncingTimeEstimator.js index dac99a27d..81234030b 100644 --- a/src/services/SyncingTimeEstimator.js +++ b/src/services/SyncingTimeEstimator.js @@ -35,8 +35,9 @@ export default class SyncingTimeEstimator { if (this.window.length === this.size) { this.window.shift() } - - this.window.push(timeToSync) + if (timeToSync != Infinity && !isNaN(timeToSync)) { + this.window.push(timeToSync) + } this.previousTime = currentTimestamp this.previousBlock = currentBlock diff --git a/test/unit/src/services/SyncingTimeEstimator.spec.js b/test/unit/src/services/SyncingTimeEstimator.spec.js index 98f773974..18eade9ab 100644 --- a/test/unit/src/services/SyncingTimeEstimator.spec.js +++ b/test/unit/src/services/SyncingTimeEstimator.spec.js @@ -1,6 +1,7 @@ import SyncingTimeEstimator from '@/services/SyncingTimeEstimator' import { SYNCING_TIME_WINDOW_LENGTH } from '@/constants' import { describe, expect, test } from 'vitest' +import { sleep } from '../../utils' describe('formatDuration', () => { describe('should start method initialize the estimator', () => { @@ -60,26 +61,6 @@ describe('formatDuration', () => { expect(estimator.window.length).toBe(0) }) - test.skip('should remove oldest value if size is reached', () => { - const estimator = new SyncingTimeEstimator() - estimator.start() - estimator.addSample({ currentBlock: 1, lastBlock: 50 }) - Array(SYNCING_TIME_WINDOW_LENGTH - 1) - .fill(null) - .map((_, index) => - estimator.addSample({ - currentBlock: (index + 1) * 50, - lastBlock: (index + 1) * 50 + 50, - }), - ) - - const oldestSample = estimator.window[0] - estimator.addSample({ currentBlock: 101 * 50, lastBlock: 101 * 50 + 50 }) - console.log(estimator.window) - - expect(estimator.window[0] !== oldestSample).toBe(true) - }) - test('should keep max size after add a greater number of more samples', () => { const estimator = new SyncingTimeEstimator() estimator.start() @@ -95,25 +76,51 @@ describe('formatDuration', () => { expect(estimator.window.length).toBe(100) }) + + test('should remove oldest value if size is reached', async () => { + const estimator = new SyncingTimeEstimator() + estimator.start() + + const mockWindow = Array(SYNCING_TIME_WINDOW_LENGTH + 1).fill(null) + + for (const index of mockWindow.keys()) { + await sleep(index * 2) + estimator.addSample({ + currentBlock: index * 50, + lastBlock: index * 50 + 50, + }) + } + const oldestSample = estimator.window?.[0] + estimator.addSample({ currentBlock: 101 * 50, lastBlock: 101 * 50 + 50 }) + expect(estimator.window[0] !== oldestSample).toBe(true) + }, 30000) }) describe('calculate method returns the average of the calculated samples', () => { - test.skip('should calculate the average if samples have been added', () => { + test('should calculate the average if samples have been added', async () => { const estimator = new SyncingTimeEstimator() estimator.start() - estimator.addSample({ currentBlock: 50, lastBlock: 100 }) + Array(SYNCING_TIME_WINDOW_LENGTH + 1) + .fill(null) + .forEach((item, index) => { + estimator.addSample({ + currentBlock: index * 50, + lastBlock: index * 50 + 50, + }) + }) + const average = estimator.calculate() - expect(average).toBe(0) + expect(average).toBeTruthy() }) - test.skip('should return 0 if no samples have been added', () => { + test('should return 0 if no samples have been added', () => { const estimator = new SyncingTimeEstimator() estimator.start() const average = estimator.calculate() - expect(average).toBeTruthy() + expect(average).toBe(0) }) }) diff --git a/test/unit/utils.js b/test/unit/utils.js index 7fb241b0e..64019c82b 100644 --- a/test/unit/utils.js +++ b/test/unit/utils.js @@ -31,3 +31,11 @@ export const createMocks = ({ slots: { ...slots }, } } + +export const sleep = (timeout = 1000) => { + return new Promise(resolve => { + setTimeout(() => { + resolve() + }, timeout) + }) +}