Skip to content

Commit

Permalink
fix bigfiles test
Browse files Browse the repository at this point in the history
  • Loading branch information
adrastaea committed Dec 16, 2024
1 parent 4b1dd3f commit e0f02d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
17 changes: 9 additions & 8 deletions packages/backend/src/nest/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,23 @@ export const libp2pInstanceParams = async (): Promise<Libp2pNodeParams> => {
export const createTmpDir = (prefix = 'quietTestTmp_'): tmp.DirResult => {
return tmp.dirSync({ mode: 0o750, prefix, unsafeCleanup: true })
}

export const tmpQuietDirPath = (name: string): string => {
return path.join(name, TestConfig.QUIET_DIR)
}

export function createFile(filePath: string, size: number) {
const stream = fs.createWriteStream(filePath)
const maxChunkSize = 1048576 // 1MB
if (size < maxChunkSize) {
stream.write(crypto.randomBytes(size))
} else {
const chunks = Math.floor(size / maxChunkSize)
for (let i = 0; i < chunks; i++) {
stream.write(crypto.randomBytes(Math.min(size, maxChunkSize)))
size -= maxChunkSize
}

let remainingSize = size

while (remainingSize > 0) {
const chunkSize = Math.min(maxChunkSize, remainingSize)
stream.write(crypto.randomBytes(chunkSize))
remainingSize -= chunkSize
}

stream.end()
}

Expand Down
23 changes: 15 additions & 8 deletions packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import fs from 'fs'
import { createLogger } from '../common/logger'

const logger = createLogger('bigFiles:test')
const BIG_FILE_SIZE = 2147483000

jest.setTimeout(200_000)
describe('IpfsFileManagerService', () => {
let module: TestingModule
let ipfsFileManagerService: IpfsFileManagerService
Expand All @@ -30,12 +30,11 @@ describe('IpfsFileManagerService', () => {
let tmpDir: DirResult
let filePath: string

beforeEach(async () => {
beforeAll(async () => {
tmpDir = createTmpDir()
filePath = new URL('./testUtils/large-file.txt', import.meta.url).pathname
// Generate 2.1GB file
createFile(filePath, 2147483000)
sleep(5000)
createFile(filePath, BIG_FILE_SIZE)
module = await Test.createTestingModule({
imports: [TestModule, IpfsFileManagerModule, IpfsModule, SocketModule, Libp2pModule],
}).compile()
Expand Down Expand Up @@ -63,9 +62,7 @@ describe('IpfsFileManagerService', () => {
await ipfsService.stop()
await ipfsFileManagerService.stop()
await module.close()
sleep(1000)
})

it('uploads large files', async () => {
// Uploading
const eventSpy = jest.spyOn(ipfsFileManagerService, 'emit')
Expand All @@ -80,6 +77,18 @@ describe('IpfsFileManagerService', () => {
channelId: 'channelId',
},
}
await waitForExpect(
() => {
expect(fs.statSync(filePath).size).toBe(BIG_FILE_SIZE)
},
100000,
100
)
if (metadata.path) {
logger.info(`Uploading file ${metadata.path} of size ${fs.statSync(metadata.path).size}`)
} else {
logger.error('File path is null')
}

await ipfsFileManagerService.uploadFile(metadata)
expect(copyFileSpy).toHaveBeenCalled()
Expand Down Expand Up @@ -128,8 +137,6 @@ describe('IpfsFileManagerService', () => {
)
})

await sleep(20_000)

await ipfsFileManagerService.stop()
logger.time('Stopping ipfs')
await ipfsService.ipfsInstance?.stop()
Expand Down

0 comments on commit e0f02d8

Please sign in to comment.