Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Nov 28, 2021
1 parent 83b35f3 commit 5bbda66
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
57 changes: 17 additions & 40 deletions src/run/measure/events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { toInputsObject } from '../../combination/inputs.js'
import { safeFinally } from '../../utils/finally.js'
import { updateDescription, END_DESCRIPTION } from '../preview/description.js'
import { sendAndReceive } from '../process/ipc.js'

Expand All @@ -12,10 +13,15 @@ import { getMinLoopDuration } from './min_loop_duration.js'
// We still perform each combination ends, for cleanup.
// `end` is always called, for runner-specific cleanup, providing `start`
// completed.
// `after` and `end` are called on exceptions.
// If an exception happens inside those themselves, it is ignored because it
// might be due to the runner being in a bad state due to the first exception.
export const runEvents = async function ({ combination, ...args }) {
const taskIds = await startCombination(combination, args.server)
const stats = await runMainEvents(args)
await endCombination(args.server)
const stats = await safeFinally(
runMainEvents.bind(undefined, args),
endCombination.bind(undefined, args.server),
)
return { stats, taskIds }
}

Expand Down Expand Up @@ -47,15 +53,6 @@ const startCombination = async function (
return taskIds
}

// `after` and `end` are called on exceptions.
// If an exception happens inside those themselves, it is ignored because it
// might be due to the runner being in a bad state due to the first exception.
const silentEndCombination = async function (server) {
try {
await endCombination(server)
} catch {}
}

// Run the runner-defined end logic
const endCombination = async function (server) {
await sendAndReceive({ event: 'end' }, server)
Expand All @@ -67,30 +64,16 @@ const runMainEvents = async function (args) {
return
}

try {
const minLoopDuration = await getMinLoopDuration(args)
const startStat = startRunDuration()
await beforeCombination(args.server)
const stats = await getCombinationStats({
...args,
startStat,
minLoopDuration,
})
await afterCombination(args)
return endRunDuration(startStat, stats)
} catch (error) {
await silentEndCombination(args.server)
throw error
}
}
const minLoopDuration = await getMinLoopDuration(args)

const getCombinationStats = async function (args) {
try {
return await performMeasureLoop(args)
} catch (error) {
await silentAfterCombination(args)
throw error
}
const startStat = startRunDuration()
await beforeCombination(args.server)
const stats = await safeFinally(
performMeasureLoop.bind(undefined, { ...args, startStat, minLoopDuration }),
afterCombination.bind(undefined, args),
)
const statsA = endRunDuration(startStat, stats)
return statsA
}

// Run the user-defined `before` hooks
Expand All @@ -115,12 +98,6 @@ const beforeCombination = async function (server) {
await sendAndReceive({ event: 'before' }, server)
}

const silentAfterCombination = async function (args) {
try {
await afterCombination(args)
} catch {}
}

// Run the user-defined `after` hooks
// `after` is always called, for cleanup, providing `before` completed.
const afterCombination = async function ({ server, previewState }) {
Expand Down
23 changes: 23 additions & 0 deletions src/utils/finally.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Like `try {} finally {}` except:
// - if both blocks throw, the exception from the `try` block has priority.
// - the finally block cannot return a value.
export const safeFinally = async function (tryFunc, finallyFunc) {
const returnValue = await callTry(tryFunc, finallyFunc)
await finallyFunc()
return returnValue
}

const callTry = async function (tryFunc, finallyFunc) {
try {
return await tryFunc()
} catch (error) {
await callSilentFinally(finallyFunc)
throw error
}
}

const callSilentFinally = async function (finallyFunc) {
try {
await finallyFunc()
} catch {}
}

0 comments on commit 5bbda66

Please sign in to comment.