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 490b9c7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 30 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
61 changes: 35 additions & 26 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,53 @@ 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]
console.log(oldestSample)
estimator.addSample({ currentBlock: 101 * 50, lastBlock: 101 * 50 + 50 })
console.log(estimator.window[0])
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 })

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 average = estimator.calculate()

expect(average).toBe(0)
})
expect(average).toBeTruthy()
}, 30000)

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
4 changes: 2 additions & 2 deletions test/unit/src/services/birthDate.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildImportWalletBirthdate } from '@/services/birthDate'
import { describe, expect, test } from 'vitest'

describe.only('birthDate', () => {
describe('birthDate', () => {
test('return 30 days less than the provided date', () => {
const date = new Date('2020-12-31')

Expand All @@ -20,7 +20,7 @@ describe.only('birthDate', () => {
expect(birthDate).toBe(null)
})

it.only('return null if the date is in the future', () => {
test('return null if the date is in the future', () => {
// date 3 days in the future
const date = new Date(Date.now() + 3 * 24 * 60 * 60 * 1000)

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 490b9c7

Please sign in to comment.