diff --git a/packages/backend/src/nest/common/utils.ts b/packages/backend/src/nest/common/utils.ts index 3f11c53c77..801e35d614 100644 --- a/packages/backend/src/nest/common/utils.ts +++ b/packages/backend/src/nest/common/utils.ts @@ -250,6 +250,7 @@ export const libp2pInstanceParams = async (): Promise => { 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) } @@ -257,15 +258,15 @@ export const tmpQuietDirPath = (name: string): string => { 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() } diff --git a/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts b/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts index 7bb658e9ea..0802c14d38 100644 --- a/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts +++ b/packages/backend/src/nest/ipfs-file-manager/big-files.long.spec.ts @@ -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 @@ -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() @@ -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') @@ -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() @@ -128,8 +137,6 @@ describe('IpfsFileManagerService', () => { ) }) - await sleep(20_000) - await ipfsFileManagerService.stop() logger.time('Stopping ipfs') await ipfsService.ipfsInstance?.stop()