Skip to content

Commit

Permalink
fix: long failing backend test
Browse files Browse the repository at this point in the history
  • Loading branch information
EmiM committed Apr 10, 2024
1 parent a529c7f commit fcd8b2b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { CustomEvent } from '@libp2p/interfaces/events'
import { jest, beforeEach, describe, it, expect, afterEach } from '@jest/globals'
import { communities, getFactory, identity, prepareStore, Store } from '@quiet/state-manager'
import { createPeerId, createTmpDir, libp2pInstanceParams, removeFilesFromDir, tmpQuietDirPath } from '../common/utils'

import { NetworkStats, type Community, type Identity, type InitCommunityPayload } from '@quiet/types'
import { NetworkStats, type Community, type Identity } from '@quiet/types'
import { LazyModuleLoader } from '@nestjs/core'
import { TestingModule, Test } from '@nestjs/testing'
import { FactoryGirl } from 'factory-girl'
Expand Down Expand Up @@ -113,7 +112,6 @@ beforeEach(async () => {
})

afterEach(async () => {
await libp2pService?.libp2pInstance?.stop()
if (connectionsManagerService) {
await connectionsManagerService.closeAllServices()
}
Expand All @@ -122,6 +120,10 @@ afterEach(async () => {

describe('Connections manager', () => {
it('saves peer stats when peer has been disconnected', async () => {
// @ts-expect-error
libp2pService.processInChunksService.init = jest.fn()
// @ts-expect-error
libp2pService.processInChunksService.process = jest.fn()
class RemotePeerEventDetail {
peerId: string

Expand All @@ -137,6 +139,10 @@ describe('Connections manager', () => {

// Peer connected
await connectionsManagerService.init()
await connectionsManagerService.launchCommunity({
community,
network: { peerId: userIdentity.peerId, hiddenService: userIdentity.hiddenService },
})
libp2pService.connectedPeers.set(peerId.toString(), DateTime.utc().valueOf())

// Peer disconnected
Expand All @@ -145,11 +151,16 @@ describe('Connections manager', () => {
remotePeer: new RemotePeerEventDetail(peerId.toString()),
remoteAddr: new RemotePeerEventDetail(remoteAddr),
}
await waitForExpect(async () => {
expect(libp2pService.libp2pInstance).not.toBeUndefined()
}, 2_000)
libp2pService.libp2pInstance?.dispatchEvent(
new CustomEvent('peer:disconnect', { detail: peerDisconectEventDetail })
)
await waitForExpect(async () => {
expect(libp2pService.connectedPeers.size).toEqual(0)
}, 2000)

expect(libp2pService.connectedPeers.size).toEqual(0)
await waitForExpect(async () => {
expect(await localDbService.get(LocalDBKeys.PEERS)).not.toBeNull()
}, 2000)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class ProcessInChunksService<T> extends EventEmitter {
super()
}

public init(data: T[], processItem: (arg: T) => Promise<any>, chunkSize: number = DEFAULT_CHUNK_SIZE) {
public init(data: T[] = [], processItem: (arg: T) => Promise<any>, chunkSize: number = DEFAULT_CHUNK_SIZE) {
this.data = data
this.processItem = processItem
this.chunkSize = chunkSize
Expand Down

0 comments on commit fcd8b2b

Please sign in to comment.