Skip to content

Commit

Permalink
fix(test): syncingTimeEstimator.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
gabaldon committed Feb 6, 2024
1 parent 3e3526d commit b544925
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 27 deletions.
5 changes: 3 additions & 2 deletions src/services/SyncingTimeEstimator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 32 additions & 25 deletions test/unit/src/services/SyncingTimeEstimator.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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()
Expand All @@ -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)
})
})

Expand Down
8 changes: 8 additions & 0 deletions test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ export const createMocks = ({
slots: { ...slots },
}
}

export const sleep = (timeout = 1000) => {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, timeout)
})
}

0 comments on commit b544925

Please sign in to comment.