hang manager off IntegrationTest, cleanup

- add a TestManager property to IntegrationTest, so manager does not need to be passed into all functions
- cleanup:
  - remove unneeded stopHardhat function
  - add hardhat instance to manager outside of startHardhat
This commit is contained in:
Eric 2025-01-15 10:38:33 +11:00
parent db7f19fba9
commit 84fb99189e
No known key found for this signature in database

View File

@ -46,6 +46,7 @@ type
Error # Test file did not launch correctly. Indicates an error occurred running the tests (usually an error in the harness). Error # Test file did not launch correctly. Indicates an error occurred running the tests (usually an error in the harness).
IntegrationTest = ref object IntegrationTest = ref object
manager: TestManager
config: IntegrationTestConfig config: IntegrationTestConfig
process: Future[CommandExResponse].Raising([AsyncProcessError, AsyncProcessTimeoutError, CancelledError]) process: Future[CommandExResponse].Raising([AsyncProcessError, AsyncProcessTimeoutError, CancelledError])
timeStart: Moment timeStart: Moment
@ -155,8 +156,7 @@ proc duration(test: IntegrationTest): Duration =
test.timeEnd - test.timeStart test.timeEnd - test.timeStart
proc startHardhat( proc startHardhat(
manager: TestManager, test: IntegrationTest): Future[Hardhat] {.async: (raises: [CancelledError, TestManagerError]).} =
config: IntegrationTestConfig): Future[Hardhat] {.async: (raises: [CancelledError, TestManagerError]).} =
var args: seq[string] = @[] var args: seq[string] = @[]
var port: int var port: int
@ -167,9 +167,9 @@ proc startHardhat(
proc onOutputLineCaptured(line: string) {.raises: [].} = proc onOutputLineCaptured(line: string) {.raises: [].} =
hardhat.output.add line hardhat.output.add line
withLock(manager.hardhatPortLock): withLock(test.manager.hardhatPortLock):
port = await nextFreePort(manager.lastHardhatPort + 10) port = await nextFreePort(test.manager.lastHardhatPort + 10)
manager.lastHardhatPort = port test.manager.lastHardhatPort = port
args.add("--port") args.add("--port")
args.add($port) args.add($port)
@ -190,16 +190,6 @@ proc startHardhat(
except CatchableError as e: except CatchableError as e:
raiseTestManagerError "hardhat node failed to start: " & e.msg, e raiseTestManagerError "hardhat node failed to start: " & e.msg, e
proc stopHardhat(
manager: TestManager,
test: IntegrationTest,
hardhat: Hardhat) {.async: (raises: [CancelledError, TestManagerError]).} =
try:
await hardhat.process.stop()
except CatchableError as parent:
raiseTestManagerError("failed to stop hardhat node", parent)
proc printResult( proc printResult(
test: IntegrationTest, test: IntegrationTest,
colour: ForegroundColor) = colour: ForegroundColor) =
@ -272,22 +262,21 @@ proc printStart(test: IntegrationTest) =
echoStyled styleBright, fgMagenta, &"[Integration test started] ", resetStyle, test.config.name echoStyled styleBright, fgMagenta, &"[Integration test started] ", resetStyle, test.config.name
proc buildCommand( proc buildCommand(
manager: TestManager,
test: IntegrationTest, test: IntegrationTest,
hardhatPort: ?int): Future[string] {.async: (raises:[CancelledError, TestManagerError]).} = hardhatPort: ?int): Future[string] {.async: (raises:[CancelledError, TestManagerError]).} =
var apiPort, discPort: int var apiPort, discPort: int
withLock(manager.codexPortLock): withLock(test.manager.codexPortLock):
# TODO: needed? nextFreePort should take care of this # TODO: needed? nextFreePort should take care of this
# inc by 20 to allow each test to run 20 codex nodes (clients, SPs, # inc by 20 to allow each test to run 20 codex nodes (clients, SPs,
# validators) giving a good chance the port will be free # validators) giving a good chance the port will be free
apiPort = await nextFreePort(manager.lastCodexApiPort + 20) apiPort = await nextFreePort(test.manager.lastCodexApiPort + 20)
manager.lastCodexApiPort = apiPort test.manager.lastCodexApiPort = apiPort
discPort = await nextFreePort(manager.lastCodexDiscPort + 20) discPort = await nextFreePort(test.manager.lastCodexDiscPort + 20)
manager.lastCodexDiscPort = discPort test.manager.lastCodexDiscPort = discPort
var logging = "" var logging = ""
if manager.debugTestHarness: if test.manager.debugTestHarness:
logging = "-d:chronicles_log_level=TRACE " & logging = "-d:chronicles_log_level=TRACE " &
"-d:chronicles_disabled_topics=websock " & "-d:chronicles_disabled_topics=websock " &
"-d:chronicles_default_output_device=stdout " & "-d:chronicles_default_output_device=stdout " &
@ -309,7 +298,7 @@ proc buildCommand(
raiseTestManagerError "bad file name, testFile: " & test.config.testFile, parent raiseTestManagerError "bad file name, testFile: " & test.config.testFile, parent
var command: string var command: string
withLock(manager.hardhatPortLock): withLock(test.manager.hardhatPortLock):
try: try:
return "nim c " & return "nim c " &
&"-d:CodexApiPort={apiPort} " & &"-d:CodexApiPort={apiPort} " &
@ -341,6 +330,7 @@ proc runTest(
trace "Running test" trace "Running test"
var test = IntegrationTest( var test = IntegrationTest(
manager: manager,
config: config, config: config,
testId: $ uint16.example testId: $ uint16.example
) )
@ -353,9 +343,10 @@ proc runTest(
var command: string var command: string
try: try:
if config.startHardhat: if config.startHardhat:
hardhat = await manager.startHardhat(config) hardhat = await test.startHardhat()
hardhatPort = hardhat.port.some hardhatPort = hardhat.port.some
command = await manager.buildCommand(test, hardhatPort) manager.hardhats.add hardhat
command = await test.buildCommand(hardhatPort)
except TestManagerError as e: except TestManagerError as e:
error "Failed to start hardhat and build command", error = e.msg error "Failed to start hardhat and build command", error = e.msg
test.timeEnd = Moment.now() test.timeEnd = Moment.now()
@ -404,8 +395,8 @@ proc runTest(
if config.startHardhat and not hardhat.isNil: if config.startHardhat and not hardhat.isNil:
try: try:
trace "Stopping hardhat", name = config.name trace "Stopping hardhat", name = config.name
await manager.stopHardhat(test, hardhat) await hardhat.process.stop()
except TestManagerError as e: except CatchableError as e:
warn "Failed to stop hardhat node, continuing", warn "Failed to stop hardhat node, continuing",
error = e.msg, test = test.config.name error = e.msg, test = test.config.name