Skip to content

Commit

Permalink
Start using await/async
Browse files Browse the repository at this point in the history
  • Loading branch information
mnot committed Feb 28, 2024
1 parent 0e9c37f commit 2c28d98
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 99 deletions.
30 changes: 15 additions & 15 deletions test-engine/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ if (testId !== '') {
testsToRun = tests
}

runTests(testsToRun, fetch, false, baseUrl)
.then(() => {
const results = getResults()
if (testId !== '') {
console.log(`${GREEN}==== Results${NC}`)
const resultSymbol = determineTestResult(tests, testId, results, false)
const resultDetails = results[testId][1] || ''
console.log(`${resultSymbol[2]} - ${resultDetails}`)
} else {
console.log(JSON.stringify(results, null, 2))
}
})
.catch(err => {
console.error(err)
})
await runTests(testsToRun, fetch, false, baseUrl).catch(err => {
console.error(err)
process.exit(1);
})

const results = getResults()

if (testId !== '') {
console.log(`${GREEN}==== Results${NC}`)
const resultSymbol = determineTestResult(tests, testId, results, false)
const resultDetails = results[testId][1] || ''
console.log(`${resultSymbol[2]} - ${resultDetails}`)
} else {
console.log(JSON.stringify(results, null, 2))
}
24 changes: 10 additions & 14 deletions test-engine/client/runner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { makeTest, testResults } from './test.mjs'

const testArray = []

export function runTests (tests, myFetch, browserCache, base, chunkSize = 50) {
export async function runTests (tests, myFetch, browserCache, base, chunkSize = 50) {
config.setFetch(myFetch)
config.setBaseUrl(base)
config.setUseBrowserCache(browserCache)
Expand All @@ -28,18 +28,14 @@ export function getResults () {
return ordered
}

function runSome (tests, chunkSize) {
return new Promise((resolve, reject) => {
let index = 0
function next () {
if (index < tests.length) {
const these = tests.slice(index, index + chunkSize).map(makeTest)
index += chunkSize
Promise.all(these).then(next)
} else {
resolve()
}
async function runSome (tests, chunkSize) {
let index = 0
function next () {
if (index < tests.length) {
const these = tests.slice(index, index + chunkSize).map(makeTest)
index += chunkSize
return Promise.all(these).then(next)
}
next()
})
}
return next()
}
134 changes: 66 additions & 68 deletions test-engine/client/test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,76 @@ const setupCheck = clientUtils.setupCheck
export const testUUIDs = {}
export const testResults = {}

export function makeTest (test) {
return new Promise((resolve, reject) => {
const uuid = utils.token()
testUUIDs[test.id] = uuid
const requests = fetching.inflateRequests(test)
const responses = []
const fetchFunctions = []
for (let i = 0; i < requests.length; ++i) {
fetchFunctions.push({
code: idx => {
const reqConfig = requests[idx]
const reqNum = idx + 1
const url = clientUtils.makeTestUrl(uuid, reqConfig)
let prevRes
if (i > 0) {
prevRes = Object.fromEntries(responses[i - 1].headers)
}
const init = fetching.init(idx, reqConfig, prevRes)
const controller = new AbortController()
const timeout = setTimeout(() => {
controller.abort()
}, config.requestTimeout * 1000)
init.signal = controller.signal
if (test.dump === true) clientUtils.logRequest(url, init, reqNum)
return config.fetch(url, init)
.then(response => {
responses.push(response)
return checkResponse(test, requests, idx, response)
})
.finally(() => {
clearTimeout(timeout)
})
},
pauseAfter: 'pause_after' in requests[i]
})
}

let idx = 0
function runNextStep () {
if (fetchFunctions.length) {
const nextFetchFunction = fetchFunctions.shift()
if (nextFetchFunction.pauseAfter === true) {
return nextFetchFunction.code(idx++)
.then(clientUtils.pause)
.then(runNextStep)
} else {
return nextFetchFunction.code(idx++)
.then(runNextStep)
export async function makeTest (test) {
const uuid = utils.token()
testUUIDs[test.id] = uuid
const requests = fetching.inflateRequests(test)
const responses = []
const fetchFunctions = []
for (let i = 0; i < requests.length; ++i) {
fetchFunctions.push({
code: idx => {
const reqConfig = requests[idx]
const reqNum = idx + 1
const url = clientUtils.makeTestUrl(uuid, reqConfig)
let prevRes
if (i > 0) {
prevRes = Object.fromEntries(responses[i - 1].headers)
}
const init = fetching.init(idx, reqConfig, prevRes)
const controller = new AbortController()
const timeout = setTimeout(() => {
controller.abort()
}, config.requestTimeout * 1000)
init.signal = controller.signal
if (test.dump === true) clientUtils.logRequest(url, init, reqNum)
return config.fetch(url, init)
.then(response => {
responses.push(response)
return checkResponse(test, requests, idx, response)
})
.finally(() => {
clearTimeout(timeout)
})
},
pauseAfter: 'pause_after' in requests[i]
})
}

let idx = 0
function runNextStep () {
if (fetchFunctions.length) {
const nextFetchFunction = fetchFunctions.shift()
if (nextFetchFunction.pauseAfter === true) {
return nextFetchFunction.code(idx++)
.then(clientUtils.pause)
.then(runNextStep)
} else {
return nextFetchFunction.code(idx++)
.then(runNextStep)
}
}
}

return clientUtils.putTestConfig(uuid, requests)
.catch(handleError)
.then(runNextStep)
.then(() => {
return clientUtils.getServerState(uuid)
})
.then(serverState => {
checkServerRequests(requests, responses, serverState)
})
.then(() => { // pass
if (test.id in testResults) throw new Error(`Duplicate test ${test.id}`)
testResults[test.id] = true
resolve()
})
.catch(err => { // fail
if (test.id in testResults) throw new Error(`Duplicate test ${test.id}`)
testResults[test.id] = [(err.name || 'unknown'), err.message]
resolve()
})
})
return clientUtils.putTestConfig(uuid, requests)
.catch(handleError)
.then(runNextStep)
.then(() => {
return clientUtils.getServerState(uuid)
})
.then(serverState => {
checkServerRequests(requests, responses, serverState)
})
.then(() => { // pass
if (test.id in testResults) throw new Error(`Duplicate test ${test.id}`)
testResults[test.id] = true
return
})
.catch(err => { // fail
if (test.id in testResults) throw new Error(`Duplicate test ${test.id}`)
testResults[test.id] = [(err.name || 'unknown'), err.message]
return
})
}

function checkResponse (test, requests, idx, response) {
Expand Down
4 changes: 2 additions & 2 deletions test-engine/client/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function makeTestUrl (uuid, reqConfig) {

const uninterestingHeaders = new Set(['date', 'expires', 'last-modified', 'content-length', 'content-type', 'connection', 'content-language', 'vary', 'mime-version'])

export function putTestConfig (uuid, requests) {
export async function putTestConfig (uuid, requests) {
const init = {
method: 'PUT',
headers: [['content-type', 'application/json']],
Expand All @@ -43,7 +43,7 @@ export function putTestConfig (uuid, requests) {
})
}

export function getServerState (uuid) {
export async function getServerState (uuid) {
return config.fetch(`${config.baseUrl}/state/${uuid}`)
.then(response => {
if (response.status === 200) {
Expand Down

0 comments on commit 2c28d98

Please sign in to comment.