Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
feat: handle retrying on the client, not service (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahnu authored Aug 22, 2022
1 parent fc5e303 commit ae3f699
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
41 changes: 37 additions & 4 deletions packages/client/src/runTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,44 @@ async function runTest({

const startTime = process.hrtime.bigint()
logger.verbose(`[${invokeBackend.BackendName}] [${executionId}] Running: '${filename}'`)
let status: TestStatus | null = null
let status: TestStatus | undefined
let result: TestRunResult | undefined
let lastError: Error | undefined
try {
const result = await invokeBackend.invoke({ config, filename, code, executionId })
status = parseStatus(result)
return result
for (let attempt = 0; attempt < config.retryCount + 1; attempt++) {
if (attempt > 0) {
// TODO: Change this to an exponential backoff?
await new Promise((r) => setTimeout(r, 5000 + Math.round(Math.random() * 1000)))
}

status = undefined
result = undefined
lastError = undefined

try {
result = await invokeBackend.invoke({
config: {
...config,
// client will take care of retrying, so we disable retrying in the service
retryCount: 0,
},
filename,
code,
executionId,
})
status = parseStatus(result)
} catch (err) {
result = undefined
status = TestStatus.Failed
lastError = err instanceof Error ? err : new Error(String(err))
continue
}

if (status === TestStatus.Passed || status === TestStatus.Skipped) {
break
}
}
return result ? result : { filename, error: lastError }
} finally {
const duration = Number(process.hrtime.bigint() - startTime) / 1e9
logger.verbose(
Expand Down
24 changes: 9 additions & 15 deletions packages/service/src/core/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function runTest(runTestContext: RunTestContext): Promise<InvokeRes
'sanity_runner.max_retry_count': runTestContext.maxRetryCount,
})

let retryCount = 0
const retryCount = 0
try {
await runner.writeTestCodeToDisk({ testCode: runTestContext.testCode })

Expand All @@ -46,18 +46,15 @@ export async function runTest(runTestContext: RunTestContext): Promise<InvokeRes
}

const results = await retry(
async () => {
logger.info(
`Starting test run (${retryCount + 1}/${runTestContext.maxRetryCount + 1}):`,
{
run_id: runTestContext.runId,
execution_id: runTestContext.executionId,
test_file: runTestContext.testFilename,
},
)
async (_, attempt) => {
logger.info(`Starting test run (${attempt}/${runTestContext.maxRetryCount + 1}):`, {
run_id: runTestContext.runId,
execution_id: runTestContext.executionId,
test_file: runTestContext.testFilename,
})
const result = await trace('Test Run', async (span) => {
span?.addTags({
'sanity_runner.attempt': retryCount + 1,
'sanity_runner.attempt': attempt,
'sanity_runner.runner': runner.name,
})
return await runner.run()
Expand All @@ -66,7 +63,7 @@ export async function runTest(runTestContext: RunTestContext): Promise<InvokeRes
// force retry if test was unsuccesfull
// if last retry, return as normal
if (!result.success) {
if (retryCount !== runTestContext.maxRetryCount) {
if (attempt <= runTestContext.maxRetryCount) {
throw new Error('Test Failed!')
}

Expand All @@ -80,9 +77,6 @@ export async function runTest(runTestContext: RunTestContext): Promise<InvokeRes
},
{
retries: runTestContext.maxRetryCount,
onRetry: function () {
retryCount++
},
},
)

Expand Down

0 comments on commit ae3f699

Please sign in to comment.