Skip to content

Commit

Permalink
tests: fix msw not being able to stall connections
Browse files Browse the repository at this point in the history
  • Loading branch information
guanzo committed Jan 26, 2024
1 parent d10af80 commit ff26b98
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 35 deletions.
75 changes: 41 additions & 34 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import { Saturn } from '#src/index.js'

const TEST_CID = 'QmXjYBY478Cno4jzdCcPy4NcJYFrwHZ51xaCP8vUwN9MGm'
const HELLO_CID = 'bafkreifjjcie6lypi6ny7amxnfftagclbuxndqonfipmb64f2km2devei4'
const TEST_AUTH =
'https://fz3dyeyxmebszwhuiky7vggmsu0rlkoy.lambda-url.us-west-2.on.aws/'
const TEST_AUTH = 'https://fz3dyeyxmebszwhuiky7vggmsu0rlkoy.lambda-url.us-west-2.on.aws/'
const TEST_ORIGIN_DOMAIN = 'l1s.saturn.test'
const clientKey = 'abc123'

Expand Down Expand Up @@ -89,8 +88,42 @@ describe('Saturn client', () => {
assert.strictEqual(actualContent.length, 5)
assert.strictEqual(actualContent, expectedContent)
})

it('should create a log on fetch success', async () => {
client.reportingLogs = true
for await (const _ of client.fetchContent(HELLO_CID)) {} // eslint-disable-line

const log = client.logs.pop()

assert(Number.isFinite(log.ttfbMs) && log.ttfbMs > 0)
assert.strictEqual(log.httpStatusCode, 200)
assert(Number.isFinite(log.numBytesSent) && log.numBytesSent > 0)
assert(Number.isFinite(log.requestDurationSec) && log.requestDurationSec > 0)
assert(!log.ifNetworkError)
})
})

describe('Fetch CID error cases', () => {
const client = new Saturn({
clientKey,
authURL: TEST_AUTH
})

// Doesn't use L1 origin mock, not sure how to force msw to stall a connection
// to test connection timeouts.
const handlers = [
mockJWT(TEST_AUTH)
]

const server = getMockServer(handlers)

before(() => {
server.listen(MSW_SERVER_OPTS)
})
after(() => {
server.close()
})

it('should fail to fetch non CID', async () => {
await assert.rejects(client.fetchCID('a'))
})
Expand All @@ -112,6 +145,12 @@ describe('Saturn client', () => {
)
})

it('should create a log on fetch network error', async () => {
await assert.rejects(client.fetchContentBuffer(HELLO_CID, { connectTimeout: 1 }))
const log = client.logs.pop()
assert.strictEqual(log.error, 'This operation was aborted')
})

it.skip('should fail when exceeding download timeout', async () => {
await assert.rejects(client.fetchCID(`${TEST_CID}/blah`, { downloadTimeout: 1 }))
})
Expand All @@ -128,36 +167,4 @@ describe('Saturn client', () => {
assert.strictEqual(client.createRequestURL('bafy...', { range: { rangeEnd: 20 } }).searchParams.get('entity-bytes'), '0:20')
})
})

describe('Logging', () => {
const handlers = [
mockJWT(TEST_AUTH)
]
const server = getMockServer(handlers)
const client = new Saturn({ clientKey, clientId: 'tesd', authURL: TEST_AUTH })
before(() => {
server.listen(MSW_SERVER_OPTS)
})
after(() => {
server.close()
})
it('should create a log on fetch success', async () => {
client.reportingLogs = true
for await (const _ of client.fetchContent(TEST_CID)) {} // eslint-disable-line

const log = client.logs.pop()

assert(Number.isFinite(log.ttfbMs) && log.ttfbMs > 0)
assert.strictEqual(log.httpStatusCode, 200)
assert(Number.isFinite(log.numBytesSent) && log.numBytesSent > 0)
assert(Number.isFinite(log.requestDurationSec) && log.requestDurationSec > 0)
assert(!log.ifNetworkError)
})

it('should create a log on fetch network error', async () => {
await assert.rejects(client.fetchContentBuffer(TEST_CID, { connectTimeout: 1 }))
const log = client.logs.pop()
assert.strictEqual(log.error, 'This operation was aborted')
})
})
})
2 changes: 1 addition & 1 deletion test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function generateNodes (count, originDomain) {
}

/**
* Generates a mock handler to mimick Saturn's orchestrator /nodes endpoint.
* Generates a mock handler to mimick an L1 node.
*
* @param {string} originUrl - originUrl
* @param {number} delay - request delay in ms
Expand Down

0 comments on commit ff26b98

Please sign in to comment.