Skip to content

Commit

Permalink
chore: move test fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki committed Mar 21, 2024
1 parent 043490f commit d3dc777
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 42 deletions.
33 changes: 2 additions & 31 deletions packages/verified-fetch/test/abort-handling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,10 @@ import Sinon from 'sinon'
import { stubInterface, type StubbedInstance } from 'sinon-ts'
import { VerifiedFetch } from '../src/verified-fetch.js'
import { createHelia } from './fixtures/create-offline-helia.js'
import { getAbortablePromise } from './fixtures/get-abortable-promise.js'
import { makeAbortedRequest } from './fixtures/make-aborted-request.js'
import type { BlockRetriever, Helia } from '@helia/interface'

async function makeAbortedRequest (verifiedFetch: VerifiedFetch, [resource, options = {}]: Parameters<typeof verifiedFetch.fetch>, promise: Promise<any>): Promise<Response> {
const controller = new AbortController()
const resultPromise = verifiedFetch.fetch(resource, {
...options,
signal: controller.signal
})

void promise.then(() => {
controller.abort()
})
return resultPromise
}

/**
* we need to emulate signal handling (blockBrokers/dnsResolvers/etc should handle abort signals too)
* this is a simplified version of what libs we depend on should do, and the
* tests in this file verify how verified-fetch would handle the failure
*/
async function getAbortablePromise <T> (signal?: AbortSignal): Promise<T> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error('timeout while resolving'))
}, 5000)

signal?.addEventListener('abort', () => {
clearTimeout(timeoutId)
reject(new Error('aborted'))
})
})
}

describe('abort-handling', function () {
this.timeout(500) // these tests should all fail extremely quickly. if they don't, they're not aborting properly, or they're being ran on an extremely slow machine.
const sandbox = Sinon.createSandbox()
Expand Down
12 changes: 1 addition & 11 deletions packages/verified-fetch/test/cache-control-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,13 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
import { dns } from '@multiformats/dns'
import { expect } from 'aegir/chai'
import Sinon from 'sinon'
import { stubInterface } from 'sinon-ts'
import { VerifiedFetch } from '../src/verified-fetch.js'
import { createHelia } from './fixtures/create-offline-helia.js'
import { answerFake } from './fixtures/dns-answer-fake.js'
import type { Helia } from '@helia/interface'
import type { IPNS } from '@helia/ipns'
import type { DNSResponse } from '@multiformats/dns'

function answerFake (data: string, TTL: number, name: string, type: number): DNSResponse {
const fake = stubInterface<DNSResponse>()
fake.Answer = [{
data,
TTL,
name,
type
}]
return fake
}
describe('cache-control header', () => {
let helia: Helia
let name: IPNS
Expand Down
17 changes: 17 additions & 0 deletions packages/verified-fetch/test/fixtures/get-abortable-promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* we need to emulate signal handling (blockBrokers/dnsResolvers/etc should handle abort signals too)
* this is a simplified version of what libs we depend on should do, and the
* tests in this file verify how verified-fetch would handle the failure
*/
export async function getAbortablePromise <T> (signal?: AbortSignal): Promise<T> {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error('timeout while resolving'))
}, 5000)

signal?.addEventListener('abort', () => {
clearTimeout(timeoutId)
reject(new Error('aborted'))
})
})
}
14 changes: 14 additions & 0 deletions packages/verified-fetch/test/fixtures/make-aborted-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { VerifiedFetch } from '../../src/verified-fetch.js'

export async function makeAbortedRequest (verifiedFetch: VerifiedFetch, [resource, options = {}]: Parameters<typeof verifiedFetch.fetch>, promise: Promise<any>): Promise<Response> {
const controller = new AbortController()
const resultPromise = verifiedFetch.fetch(resource, {
...options,
signal: controller.signal
})

void promise.then(() => {
controller.abort()
})
return resultPromise
}

0 comments on commit d3dc777

Please sign in to comment.