diff --git a/tests/integration/hardhat.nim b/tests/integration/hardhat.nim index 3d5a0822..12ba57d8 100644 --- a/tests/integration/hardhat.nim +++ b/tests/integration/hardhat.nim @@ -28,7 +28,7 @@ type HardhatProcess* = ref object of NodeProcess logFile: ?IoHandle started: Future[void] - trackedFutures: TrackedFutures + # trackedFutures: TrackedFutures proc captureOutput*(node: HardhatProcess, logFilePath: string) {.async.} = let logFileHandle = openFile( @@ -44,27 +44,42 @@ proc captureOutput*(node: HardhatProcess, logFilePath: string) {.async.} = node.logFile = some fileHandle node.started = newFuture[void]("hardhat.started") try: - for line in node.process.outputStream.lines: + while true: + while(let line = await node.process.stdOutStream.readLine(); line != ""): + echo "got line: ", line + if line.contains(startedOutput): + node.started.complete() - if line.contains(startedOutput): - node.started.complete() - - if error =? fileHandle.writeFile(line & "\n").errorOption: - error "failed to write to hardhat file", errorCode = error - discard fileHandle.closeFile() - return + if error =? fileHandle.writeFile(line & "\n").errorOption: + error "failed to write to hardhat file", errorCode = error + discard fileHandle.closeFile() + return + await sleepAsync(1.millis) await sleepAsync(1.millis) + # for line in node.process.outputStream.lines: + + # if line.contains(startedOutput): + # node.started.complete() + + # if error =? fileHandle.writeFile(line & "\n").errorOption: + # error "failed to write to hardhat file", errorCode = error + # discard fileHandle.closeFile() + # return + + # await sleepAsync(1.millis) + except CancelledError: discard -proc start(node: HardhatProcess) = - node.process = osproc.startProcess( +proc start(node: HardhatProcess) {.async.} = + node.process = await startProcess( "npm start", workingDir, # node.arguments, - options={poEvalCommand}) + options={AsyncProcessOption.EvalCommand} + ) for arg in node.arguments: if arg.contains "--log-file=": @@ -72,41 +87,41 @@ proc start(node: HardhatProcess) = discard node.captureOutput(logFilePath).track(node) break -proc waitUntilOutput*(node: HardhatProcess, output: string) = +proc waitUntilOutput*(node: HardhatProcess, output: string) {.async.} = if not node.started.isNil: try: - waitFor node.started.wait(5000.milliseconds) + await node.started.wait(5000.milliseconds) return except AsyncTimeoutError: discard # should raiseAssert below - else: - for line in node.process.outputStream.lines: - if line.contains(output): - return + # else: + # for line in node.process.outputStream.lines: + # if line.contains(output): + # return raiseAssert "node did not output '" & output & "'" -proc waitUntilStarted*(node: HardhatProcess) = - node.waitUntilOutput(startedOutput) +proc waitUntilStarted*(node: HardhatProcess) {.async.} = + await node.waitUntilOutput(startedOutput) -proc startHardhatProcess*(args: openArray[string]): HardhatProcess = +proc startHardhatProcess*(args: seq[string]): Future[HardhatProcess] {.async.} = ## Starts a Hardhat Node with the specified arguments. let node = HardhatProcess(arguments: @args, trackedFutures: TrackedFutures.new()) - node.start() + await node.start() node -method stop*(node: HardhatProcess) = +method stop*(node: HardhatProcess) {.async.} = # terminate the process procCall NodeProcess(node).stop() - waitFor node.trackedFutures.cancelTracked() + await node.trackedFutures.cancelTracked() if logFile =? node.logFile: discard logFile.closeFile() -proc restart*(node: HardhatProcess) = - node.stop() - node.start() - node.waitUntilStarted() +proc restart*(node: HardhatProcess) {.async.} = + await node.stop() + await node.start() + await node.waitUntilStarted() proc removeDataDir*(node: HardhatProcess) = discard diff --git a/tests/integration/multinodes.nim b/tests/integration/multinodes.nim index 9fa76a7a..854400d5 100644 --- a/tests/integration/multinodes.nim +++ b/tests/integration/multinodes.nim @@ -144,14 +144,18 @@ template multinodesuite*(name: string, body: untyped) = let fileName = logDir / fn return fileName - proc newHardhatProcess(config: HardhatConfig, role: Role): NodeProcess = + proc newHardhatProcess( + config: HardhatConfig, + role: Role + ): Future[NodeProcess] {.async.} = + var options: seq[string] = @[] if config.logFile: let updatedLogFile = getLogFile(role, none int) options.add "--log-file=" & updatedLogFile - let node = startHardhatProcess(options) - node.waitUntilStarted() + let node = await startHardhatProcess(options) + await node.waitUntilStarted() debug "started new hardhat node" return node @@ -159,7 +163,7 @@ template multinodesuite*(name: string, body: untyped) = proc newNodeProcess(roleIdx: int, config1: NodeConfig, role: Role - ): NodeProcess = + ): Future[NodeProcess] {.async.} = let nodeIdx = running.len var config = config1 @@ -189,8 +193,10 @@ template multinodesuite*(name: string, body: untyped) = "--disc-port=" & $(8090 + nodeIdx), "--eth-account=" & $accounts[nodeIdx]]) - let node = startNode(options, config.debugEnabled) - node.waitUntilStarted() + let node = await startNode(options, config.debugEnabled) + echo "[multinodes.newNodeProcess] waiting until ", role, " node started" + await node.waitUntilStarted() + echo "[multinodes.newNodeProcess] ", role, " NODE STARTED" return node @@ -203,17 +209,17 @@ template multinodesuite*(name: string, body: untyped) = proc validators(): seq[RunningNode] {.used.} = running.filter(proc(r: RunningNode): bool = r.role == Role.Validator) - proc startHardhatNode(): NodeProcess = + proc startHardhatNode(): Future[NodeProcess] {.async.} = var config = nodeConfigs.hardhat - return newHardhatProcess(config, Role.Hardhat) + return await newHardhatProcess(config, Role.Hardhat) - proc startClientNode(): NodeProcess = + proc startClientNode(): Future[NodeProcess] {.async.} = let clientIdx = clients().len var config = nodeConfigs.clients config.cliOptions.add CliOption(key: "--persistence") - return newNodeProcess(clientIdx, config, Role.Client) + return await newNodeProcess(clientIdx, config, Role.Client) - proc startProviderNode(): NodeProcess = + proc startProviderNode(): Future[NodeProcess] {.async.} = let providerIdx = providers().len var config = nodeConfigs.providers config.cliOptions.add CliOption(key: "--bootstrap-node", value: bootstrap) @@ -224,50 +230,57 @@ template multinodesuite*(name: string, body: untyped) = o => (let idx = o.nodeIdx |? providerIdx; idx == providerIdx) ) - return newNodeProcess(providerIdx, config, Role.Provider) + return await newNodeProcess(providerIdx, config, Role.Provider) - proc startValidatorNode(): NodeProcess = + proc startValidatorNode(): Future[NodeProcess] {.async.} = let validatorIdx = validators().len var config = nodeConfigs.validators config.cliOptions.add CliOption(key: "--bootstrap-node", value: bootstrap) config.cliOptions.add CliOption(key: "--validator") - return newNodeProcess(validatorIdx, config, Role.Validator) + return await newNodeProcess(validatorIdx, config, Role.Validator) setup: if not nodeConfigs.hardhat.isNil: - let node = startHardhatNode() + let node = await startHardhatNode() running.add RunningNode(role: Role.Hardhat, node: node) for i in 0..