From ac588b99cead54a52d22e9a8a87488c8416cb651 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Fri, 21 Mar 2025 11:23:21 +1100 Subject: [PATCH] write to stdout/stderr as the stream is read, rearrange node stop logging --- tests/integration/codexprocess.nim | 2 +- tests/integration/nodeprocess.nim | 2 +- tests/integration/testmanager.nim | 40 ++++++++++-------------------- 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/tests/integration/codexprocess.nim b/tests/integration/codexprocess.nim index 1a41a658..4a0e1c6b 100644 --- a/tests/integration/codexprocess.nim +++ b/tests/integration/codexprocess.nim @@ -113,9 +113,9 @@ method stop*(node: CodexProcess) {.async: (raises: []).} = logScope: nodeName = node.name + trace "stopping codex client" await procCall NodeProcess(node).stop() - trace "stopping codex client" if client =? node.client: await client.close() node.client = none CodexClient diff --git a/tests/integration/nodeprocess.nim b/tests/integration/nodeprocess.nim index 0dabff77..fed42a0d 100644 --- a/tests/integration/nodeprocess.nim +++ b/tests/integration/nodeprocess.nim @@ -127,7 +127,6 @@ method stop*( if exitCode > 0 and exitCode != 143 and # 143 = SIGTERM (initiated above) exitCode != expectedErrCode: warn "process exited with a non-zero exit code", exitCode - trace "node stopped", exitCode except CatchableError: try: let forcedExitCode = await noCancel node.process.killAndWaitForExit(3.seconds) @@ -150,6 +149,7 @@ method stop*( asyncSpawn closeProcessStreams() else: await closeProcessStreams() + trace "node stopped", exitCode proc waitUntilOutput*( node: NodeProcess, output: string diff --git a/tests/integration/testmanager.nim b/tests/integration/testmanager.nim index 5a9a0f46..764a25de 100644 --- a/tests/integration/testmanager.nim +++ b/tests/integration/testmanager.nim @@ -415,6 +415,7 @@ proc teardownTest(test: IntegrationTest) {.async: (raises: []).} = if not test.process.isNil: var output = test.output.expect("should have output value") if test.process.running |? false: + trace "Test process still running, terminating..." try: output.exitCode = some (await noCancel test.process.terminateAndWaitForExit(1.seconds)) @@ -423,25 +424,6 @@ proc teardownTest(test: IntegrationTest) {.async: (raises: []).} = let e = getCurrentException() warn "Test process failed to terminate, check for zombies", error = e.msg - try: - trace "Writing stdout and/or stderr streams to file", - stdoutFile = test.logFile("stdout.log"), - stdoutLines = output.stdOut.len, - stderrFile = test.logFile("stderr.log"), - stderrLines = output.stdErr.len - test.logFile("stdout.log").appendFile(output.stdOut.join("\n").stripAnsi) - if test.status == IntegrationTestStatus.Error or - (test.status == IntegrationTestStatus.Failed and test.output.isErrorLike): - test.logFile("stderr.log").appendFile(output.stdErr.join("\n").stripAnsi) - except AsyncTimeoutError: - error "Timeout waiting for stdout or stderr stream contents, nothing will be written to file" - except IOError as e: - warn "Failed to write test stdout and/or stderr to file", error = e.msg - except AsyncStreamError as e: - error "Failed to read test process output stream", error = e.msg - test.output = TestOutput.failure(e) - test.status = IntegrationTestStatus.Error - await test.process.closeWait() trace "Test process output streams closed" @@ -479,14 +461,18 @@ proc untilTimeout( raise newException(AsyncTimeoutError, "Timed out") proc captureOutput( - process: AsyncProcessRef, stream: AsyncStreamReader + process: AsyncProcessRef, stream: AsyncStreamReader, filePath: string ): Future[seq[string]] {.async: (raises: [CancelledError]).} = var output: seq[string] = @[] try: while process.running.option == some true: while (let line = await stream.readLine(0, "\n"); line != ""): - output.add line - await sleepAsync(1.nanos) + try: + output.add line + filePath.appendFile(line.stripAnsi) + await sleepAsync(1.nanos) + except IOError as e: + warn "Failed to write test stdout and/or stderr to file", error = e.msg await sleepAsync(1.nanos) return output except CancelledError as e: @@ -502,8 +488,10 @@ proc captureProcessOutput( trace "Reading stdout and stderr streams from test process" - let futStdOut = test.process.captureOutput(test.process.stdoutStream) - let futStdErr = test.process.captureOutput(test.process.stderrStream) + let futStdOut = + test.process.captureOutput(test.process.stdoutStream, test.logFile("stdout.log")) + let futStdErr = + test.process.captureOutput(test.process.stderrStream, test.logFile("stderr.log")) await allFutures(futStdOut, futStdErr) return (await futStdOut, await futStdErr) @@ -751,9 +739,7 @@ proc printResult(manager: TestManager) = timeSavedEst = &"{relativeTimeSaved}%", passing = &"{successes} / {manager.tests.len}" -proc start*( - manager: TestManager -) {.async: (raises: [CancelledError]).} = +proc start*(manager: TestManager) {.async: (raises: [CancelledError]).} = if manager.config.showContinuousStatusUpdates: let fut = manager.continuallyShowUpdates() manager.trackedFutures.track fut