Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
islathehut committed Apr 12, 2024
1 parent 39ddf35 commit 740c288
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import waitForExpect from 'wait-for-expect'
import { Libp2pEvents } from '../libp2p/libp2p.types'
import { sleep } from '../common/sleep'
import { createLibp2pAddress } from '@quiet/common'
import { lib } from 'crypto-js'

jest.setTimeout(100_000)

Expand Down Expand Up @@ -121,7 +122,7 @@ afterEach(async () => {
})

describe('Connections manager', () => {
it('saves peer stats when peer has been disconnected', async () => {
it.only('saves peer stats when peer has been disconnected', async () => {
class RemotePeerEventDetail {
peerId: string

Expand Down Expand Up @@ -205,8 +206,8 @@ describe('Connections manager', () => {
await sleep(5000)
// It looks LibP2P dials peers initially when it's started and
// then IPFS service dials peers again when started, thus
// peersCount * 2
expect(spyOnDial).toHaveBeenCalledTimes(peersCount * 2)
// peersCount-1 * 2 because we don't dial ourself (the first peer in the list)
expect(spyOnDial).toHaveBeenCalledTimes((peersCount - 1) * 2)
// Temporary fix for hanging test - websocketOverTor doesn't have abortController
await sleep(5000)
})
Expand Down
8 changes: 5 additions & 3 deletions packages/backend/src/nest/libp2p/libp2p.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Libp2pEvents, Libp2pNodeParams } from './libp2p.types'
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
import validator from 'validator'
import waitForExpect from 'wait-for-expect'
import { ProcessInChunksService } from './process-in-chunks.service'
import { DEFAULT_NUM_TRIES, ProcessInChunksService } from './process-in-chunks.service'

describe('Libp2pService', () => {
let module: TestingModule
Expand Down Expand Up @@ -93,10 +93,12 @@ describe('Libp2pService', () => {
await libp2pService.createInstance(params)
expect(libp2pService.libp2pInstance).not.toBeNull()
// @ts-expect-error processItem is private
const dialPeerSpy = jest.spyOn(processInChunks, 'processItem')
const processItemSpy = jest.spyOn(processInChunks, 'processItem')
const dialSpy = jest.spyOn(libp2pService.libp2pInstance!, 'dial')
libp2pService.emit(Libp2pEvents.DIAL_PEERS, addresses)
await waitForExpect(async () => {
expect(dialPeerSpy).toBeCalledTimes(1)
expect(processItemSpy).toBeCalledTimes(2 * DEFAULT_NUM_TRIES)
expect(dialSpy).toBeCalledTimes(1)
})
})
})
11 changes: 4 additions & 7 deletions packages/backend/src/nest/libp2p/process-in-chunks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { queue, done } from 'fastq'
import Logger from '../common/logger'

const DEFAULT_CHUNK_SIZE = 10
const DEFAULT_NUM_TRIES = 2
export const DEFAULT_NUM_TRIES = 2

type ProcessTask<T> = {
data: T
Expand Down Expand Up @@ -40,16 +40,13 @@ export class ProcessInChunksService<T> extends EventEmitter {
}

private addToTaskQueue() {
const maxChunkSize = Math.min(this.data.size, this.chunkSize)
let count = 0
this.logger(`Adding ${maxChunkSize} items to the task queue`)
this.logger(`Adding ${this.data.size} items to the task queue`)
for (const item of this.data) {
if (item && count < maxChunkSize) {
if (item) {
this.logger(`Adding data ${item} to the task queue`)
this.data.delete(item)
try {
this.taskQueue.push({ data: item, tries: 0 } as ProcessTask<T>)
count++
} catch (e) {
this.logger.error(`Error occurred while adding new task for item ${item} to the queue`, e)
this.data.add(item)
Expand All @@ -63,7 +60,7 @@ export class ProcessInChunksService<T> extends EventEmitter {
this.logger(`Processing task with data ${task.data}`)
await this.processItem(task.data)
} catch (e) {
this.logger(`Processing task with data ${task.data} failed, message:`, e.message)
this.logger.error(`Processing task with data ${task.data} failed`, e)
if (task.tries + 1 < DEFAULT_NUM_TRIES) {
this.logger(`Will try to re-attempt task with data ${task.data}`)
this.taskQueue.push({ ...task, tries: task.tries + 1 })
Expand Down
6 changes: 3 additions & 3 deletions packages/backend/src/nest/libp2p/process-in-chunks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('ProcessInChunks', () => {
processInChunks.init(['a', 'b', 'c', 'd'], mockProcessItem)
await processInChunks.process()
await waitForExpect(() => {
expect(mockProcessItem).toBeCalledTimes(4)
expect(mockProcessItem).toBeCalledTimes(6)
})
})

Expand All @@ -43,7 +43,7 @@ describe('ProcessInChunks', () => {
processInChunks.updateData(['e', 'f'])
await processInChunks.process()
await waitForExpect(() => {
expect(mockProcessItem).toBeCalledTimes(4)
expect(mockProcessItem).toBeCalledTimes(5)
})
})

Expand All @@ -60,7 +60,7 @@ describe('ProcessInChunks', () => {
processInChunks.init(['a', 'b', 'c', 'd'], mockProcessItem, chunkSize)
await processInChunks.process()
await waitForExpect(() => {
expect(mockProcessItem).toBeCalledTimes(4)
expect(mockProcessItem).toBeCalledTimes(2)
})
})

Expand Down

0 comments on commit 740c288

Please sign in to comment.