-
Notifications
You must be signed in to change notification settings - Fork 5
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
chore: integration tests setup #116
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import path, { resolve } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import { runWranglerDev } from '../helpers/run-wrangler.js' | ||
|
||
// Get __dirname equivalent in ES module | ||
const __filename = fileURLToPath(import.meta.url) | ||
const __dirname = path.dirname(__filename) | ||
|
||
/** | ||
* Worker information object | ||
* @typedef {Object} WorkerInfo | ||
* @property {string | undefined} ip - The IP address of the test worker. | ||
* @property {number | undefined} port - The port of the test worker. | ||
* @property {() => Promise<void> | undefined} stop - Function to stop the test worker. | ||
* @property {() => string | undefined} getOutput - Function to get the output of the test worker. | ||
* @property {string} wranglerEnv - The wrangler environment to use for the test worker. | ||
*/ | ||
|
||
/** | ||
* Worker information object | ||
* @type {WorkerInfo} | ||
*/ | ||
const workerInfo = { | ||
ip: undefined, | ||
port: undefined, | ||
stop: undefined, | ||
getOutput: undefined, | ||
wranglerEnv: process.env.WRANGLER_ENV || 'integration' | ||
} | ||
|
||
/** | ||
* Sets up the test worker. | ||
* @returns {Promise<void>} | ||
*/ | ||
export const mochaGlobalSetup = async () => { | ||
try { | ||
const result = await runWranglerDev( | ||
resolve(__dirname, '../../'), // The directory of the worker with the wrangler.toml | ||
['--local'], | ||
process.env, | ||
workerInfo.wranglerEnv | ||
) | ||
Object.assign(workerInfo, result) | ||
console.log(`Output: ${await workerInfo.getOutput()}`) | ||
console.log('WorkerInfo:', workerInfo) | ||
console.log('Test worker started!') | ||
} catch (error) { | ||
console.error('Failed to start test worker:', error) | ||
throw error | ||
} | ||
} | ||
|
||
/** | ||
* Tears down the test worker. | ||
* @returns {Promise<void>} | ||
*/ | ||
export const mochaGlobalTeardown = async () => { | ||
try { | ||
const { stop } = workerInfo | ||
await stop?.() | ||
// console.log('getOutput', getOutput()) // uncomment for debugging | ||
console.log('Test worker stopped!') | ||
} catch (error) { | ||
console.error('Failed to stop test worker:', error) | ||
throw error | ||
} | ||
} | ||
|
||
/** | ||
* Gets the worker info. | ||
* @returns {WorkerInfo} | ||
*/ | ||
export function getWorkerInfo () { | ||
return workerInfo | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import assert from 'node:assert' | ||
import { fork } from 'node:child_process' | ||
import path from 'node:path' | ||
import treeKill from 'tree-kill' | ||
import { fileURLToPath } from 'node:url' | ||
|
||
// Get __dirname equivalent in ES module | ||
const __filename = fileURLToPath(import.meta.url) | ||
const __dirname = path.dirname(__filename) | ||
|
||
export const wranglerEntryPath = path.resolve( | ||
__dirname, | ||
'../../node_modules/wrangler/bin/wrangler.js' | ||
) | ||
|
||
/** | ||
* @typedef {Object} WranglerProcessInfo | ||
* @property {string} ip - The IP address of the test worker. | ||
* @property {number} port - The port of the test worker. | ||
* @property {() => Promise<void>} stop - Function to stop the test worker. | ||
* @property {() => string} getOutput - Function to get the output of the test worker. | ||
* @property {() => void} clearOutput - Function to clear the output buffer. | ||
*/ | ||
|
||
/** | ||
* Runs the command `wrangler dev` in a child process. | ||
* | ||
* Returns an object that gives you access to: | ||
* | ||
* - `ip` and `port` of the http-server hosting the pages project | ||
* - `stop()` function that will close down the server. | ||
* | ||
* @param {string} cwd - The current working directory. | ||
* @param {string[]} options - The options to pass to the wrangler command. | ||
* @param {NodeJS.ProcessEnv} [env] - The environment variables. | ||
* @param {string} [wranglerEnv] - The wrangler environment to use. | ||
* @returns {Promise<WranglerProcessInfo>} | ||
*/ | ||
export async function runWranglerDev ( | ||
cwd, | ||
options, | ||
env, | ||
wranglerEnv | ||
) { | ||
return runLongLivedWrangler( | ||
['dev', `--env=${wranglerEnv}`, ...options], | ||
cwd, | ||
env | ||
) | ||
} | ||
|
||
/** | ||
* Runs a long-lived wrangler command in a child process. | ||
* | ||
* @param {string[]} command - The wrangler command to run. | ||
* @param {string} cwd - The current working directory. | ||
* @param {NodeJS.ProcessEnv} [env] - The environment variables. | ||
* @returns {Promise<WranglerProcessInfo>} | ||
*/ | ||
async function runLongLivedWrangler ( | ||
command, | ||
cwd, | ||
env | ||
) { | ||
let settledReadyPromise = false | ||
/** @type {(value: { ip: string port: number }) => void} */ | ||
let resolveReadyPromise | ||
/** @type {(reason: unknown) => void} */ | ||
let rejectReadyPromise | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Let's avoid all these |
||
|
||
const ready = new Promise((resolve, reject) => { | ||
resolveReadyPromise = resolve | ||
rejectReadyPromise = reject | ||
}) | ||
|
||
const wranglerProcess = fork(wranglerEntryPath, command, { | ||
stdio: ['ignore', /* stdout */ 'pipe', /* stderr */ 'pipe', 'ipc'], | ||
cwd, | ||
env: { ...process.env, ...env, PWD: cwd } | ||
}).on('message', (message) => { | ||
if (settledReadyPromise) return | ||
settledReadyPromise = true | ||
clearTimeout(timeoutHandle) | ||
resolveReadyPromise(JSON.parse(message.toString())) | ||
}) | ||
|
||
const chunks = [] | ||
wranglerProcess.stdout?.on('data', (chunk) => { | ||
chunks.push(chunk) | ||
}) | ||
wranglerProcess.stderr?.on('data', (chunk) => { | ||
chunks.push(chunk) | ||
}) | ||
const getOutput = () => Buffer.concat(chunks).toString() | ||
const clearOutput = () => (chunks.length = 0) | ||
|
||
const timeoutHandle = setTimeout(() => { | ||
if (settledReadyPromise) return | ||
settledReadyPromise = true | ||
const separator = '='.repeat(80) | ||
const message = [ | ||
'Timed out starting long-lived Wrangler:', | ||
separator, | ||
getOutput(), | ||
separator | ||
].join('\n') | ||
rejectReadyPromise(new Error(message)) | ||
}, 50_000) | ||
|
||
async function stop () { | ||
return new Promise((resolve) => { | ||
assert( | ||
wranglerProcess.pid, | ||
`Command "${command.join(' ')}" had no process id` | ||
) | ||
treeKill(wranglerProcess.pid, (e) => { | ||
if (e) { | ||
console.error( | ||
'Failed to kill command: ' + command.join(' '), | ||
wranglerProcess.pid, | ||
e | ||
) | ||
fforbeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
resolve() | ||
}) | ||
}) | ||
} | ||
|
||
const { ip, port } = await ready | ||
return { ip, port, stop, getOutput, clearOutput } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { expect } from 'chai' | ||
import { describe, it } from 'mocha' | ||
import fetch from 'node-fetch' | ||
import { getWorkerInfo } from '../fixtures/worker-fixture.js' | ||
|
||
describe('Rate Limit Handler', () => { | ||
/** | ||
* This is a test CID that is known to be stored in the staging environment | ||
* See https://bafybeibv7vzycdcnydl5n5lbws6lul2omkm6a6b5wmqt77sicrwnhesy7y.ipfs.w3s.link | ||
*/ | ||
const cid = 'bafybeibv7vzycdcnydl5n5lbws6lul2omkm6a6b5wmqt77sicrwnhesy7y' | ||
|
||
it('should enforce rate limits', async () => { | ||
const { ip, port } = getWorkerInfo() | ||
const maxRequests = 130 | ||
let successCount = 0 | ||
let rateLimitCount = 0 | ||
|
||
const requests = Array.from({ length: maxRequests }, async () => { | ||
const response = await fetch(`http://${ip}:${port}/ipfs/${cid}`) | ||
if (response.status === 200) { | ||
successCount++ | ||
} else if (response.status === 429) { | ||
rateLimitCount++ | ||
} | ||
}) | ||
|
||
await Promise.all(requests) | ||
|
||
expect(successCount).to.be.lessThan(maxRequests) | ||
expect(rateLimitCount).to.be.greaterThan(0) | ||
fforbeck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}).timeout(30_000) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
praise: I like this move!