Skip to content

Commit

Permalink
fix: abstract-session cleanup and duplicate provider handling
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed May 17, 2024
1 parent 97d21cd commit 46e5e3c
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 106 deletions.
1 change: 1 addition & 0 deletions packages/block-brokers/src/trustless-gateway/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TrustlessGatewaySession extends AbstractSession<TrustlessGateway, Trustles
}

async * findNewProviders (cid: CID, options: AbortOptions = {}): AsyncGenerator<TrustlessGateway> {
this.log('findNewProviders called')
yield * findHttpGatewayProviders(cid, this.routing, this.logger, this.allowInsecure, this.allowLocal, options)
}

Expand Down
38 changes: 34 additions & 4 deletions packages/block-brokers/test/trustless-gateway-sessions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,43 @@ describe('trustless-gateway sessions', () => {
]
}

components.routing.findProviders.returns(async function * () {
yield prov
components.routing.findProviders.callsFake(async function * () {
yield prov
yield prov
}())
})

await expect(session.retrieve(cid)).to.eventually.deep.equal(block)
expect(queryProviderSpy.callCount).to.equal(1)
})

it('should ignore duplicate providers when unable to retrieve a block', async () => {
const session = createTrustlessGatewaySession(components, {
allowInsecure: true,
allowLocal: true
})

// changed the CID to end in `aa` instead of `aq`
const cid = CID.parse('bafkreiefnkxuhnq3536qo2i2w3tazvifek4mbbzb6zlq3ouhprjce5c3aa')

const queryProviderSpy = Sinon.spy(session, 'queryProvider')
const findNewProvidersSpy = Sinon.spy(session, 'findNewProviders')
const hasProviderSpy = Sinon.spy(session, 'hasProvider')

const prov = {
id: await createEd25519PeerId(),
multiaddrs: [
uriToMultiaddr(process.env.TRUSTLESS_GATEWAY ?? '')
]
}

components.routing.findProviders.callsFake(async function * () {
yield prov
})

await expect(session.retrieve(cid)).to.eventually.be.rejected()
expect(hasProviderSpy.callCount).to.be.greaterThanOrEqual(2)
expect(hasProviderSpy.getCall(0).returnValue).to.be.false()
expect(hasProviderSpy.getCall(1).returnValue).to.be.true()
expect(findNewProvidersSpy.callCount).to.be.greaterThanOrEqual(2)
expect(queryProviderSpy.callCount).to.equal(1)
})
})
Loading

0 comments on commit 46e5e3c

Please sign in to comment.