Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a locking test case #99

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions packages/helia/test/storage.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<CID> {
for (let i = 0; i < count; i++) {
yield blocks[i].cid
await delay(10)
}
}

const getFirstEntry = async (asyncIterableInstance: AsyncIterable<Pair>) => {
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()
})
})
})