diff --git a/packages/helia/test/storage.spec.ts b/packages/helia/test/storage.spec.ts index 4247080a..4b3223dd 100644 --- a/packages/helia/test/storage.spec.ts +++ b/packages/helia/test/storage.spec.ts @@ -1,21 +1,22 @@ /* eslint-env mocha */ - +import type { Helia } from '@helia/interface' +import type { Pins } from '@helia/interface/pins' import { expect } from 'aegir/chai' import { MemoryBlockstore } from 'blockstore-core' import { MemoryDatastore } from 'datastore-core' import delay from 'delay' +import type { Blockstore, Pair } from 'interface-blockstore' +import type { Bitswap } from 'ipfs-bitswap' import all from 'it-all' import drain from 'it-drain' +import type { CID } from 'multiformats/cid' import * as raw from 'multiformats/codecs/raw' -import Sinon from 'sinon' -import { type StubbedInstance, stubInterface } from 'sinon-ts' +import * as Sinon from 'sinon' +import { stubInterface, type StubbedInstance } from 'sinon-ts' +import { createHelia } from '../src/index.js' import { PinsImpl } from '../src/pins.js' import { BlockStorage } from '../src/storage.js' import { createBlock } from './fixtures/create-block.js' -import type { Pins } from '@helia/interface/pins' -import type { Blockstore } from 'interface-blockstore' -import type { Bitswap } from 'ipfs-bitswap' -import type { CID } from 'multiformats/cid' describe('storage', () => { let storage: BlockStorage @@ -175,4 +176,52 @@ describe('storage', () => { expect(await blockstore.has(blocks[i].cid)).to.be.true() } }) + + describe('blocking stores', () => { + let helia: Helia + + beforeEach(async () => { + helia = await createHelia({ + blockstore + }) + }) + + afterEach(async () => { + if (helia != null) { + await helia.stop() + } + }) + + it.only('gets many blocks from bitswap when they are not in the blockstore and does not block.', async () => { + bitswap.isStarted.returns(true) + + const count = 5 + + for (let i = 0; i < count; i++) { + const { cid, block } = blocks[i] + bitswap.want.withArgs(cid).resolves(block) + + expect(await blockstore.has(cid)).to.be.false() + } + + const cidsGenerator = async function* (): AsyncGenerator { + for (let i = 0; i < count; i++) { + yield blocks[i].cid + await delay(10) + } + } + + const getFirstEntry = async (asyncIterableInstance: AsyncIterable) => { + for await (const entry of asyncIterableInstance) { + await helia.gc({ signal: AbortSignal.timeout(100) }) + return entry; + } + } + + setTimeout(() => Promise.reject(), 10000) + const retrieved = await storage.getMany(cidsGenerator()) + const firstEntryFromRetrieved = await getFirstEntry(retrieved) + expect(firstEntryFromRetrieved?.block).to.not.be.undefined() + }) + }) })