From 5bd35720b56e2bed6103c2b2e1bf74d68557f24d Mon Sep 17 00:00:00 2001 From: thatben Date: Mon, 10 Mar 2025 14:09:41 +0100 Subject: [PATCH] implements dhtEnable flag --- codexcrawler/components/crawler.nim | 3 ++- tests/codexcrawler/components/testcrawler.nim | 14 ++++++++++++++ tests/codexcrawler/mocks/mockstate.nim | 4 +++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/codexcrawler/components/crawler.nim b/codexcrawler/components/crawler.nim index c32f5ce..57602db 100644 --- a/codexcrawler/components/crawler.nim +++ b/codexcrawler/components/crawler.nim @@ -52,7 +52,8 @@ method start*(c: Crawler): Future[?!void] {.async.} = proc onStep(): Future[?!void] {.async: (raises: []), gcsafe.} = await c.step() - await c.state.whileRunning(onStep, c.state.config.stepDelayMs.milliseconds) + if c.state.config.dhtEnable: + await c.state.whileRunning(onStep, c.state.config.stepDelayMs.milliseconds) return success() diff --git a/tests/codexcrawler/components/testcrawler.nim b/tests/codexcrawler/components/testcrawler.nim index cbe9d75..6a51721 100644 --- a/tests/codexcrawler/components/testcrawler.nim +++ b/tests/codexcrawler/components/testcrawler.nim @@ -55,6 +55,20 @@ suite "Crawler": check: !(dht.getNeighborsArg) == nid1 + test "onStep is not activated when config.dhtEnable is false": + # Recreate crawler, reset mockstate: + (await crawler.stop()).tryGet() + state.steppers = @[] + # disable DHT: + state.config.dhtEnable = false + (await crawler.start()).tryGet() + + todo.popReturn = success(nid1) + dht.getNeighborsReturn = success(responsive(nid1)) + + check: + state.steppers.len == 0 + test "nodes returned by getNeighbors are raised as nodesFound": var nodesFound = newSeq[Nid]() proc onNodesFound(nids: seq[Nid]): Future[?!void] {.async.} = diff --git a/tests/codexcrawler/mocks/mockstate.nim b/tests/codexcrawler/mocks/mockstate.nim index 00e1198..67b45a3 100644 --- a/tests/codexcrawler/mocks/mockstate.nim +++ b/tests/codexcrawler/mocks/mockstate.nim @@ -22,7 +22,9 @@ method whileRunning*(s: MockState, step: OnStep, delay: Duration) {.async.} = proc createMockState*(): MockState = MockState( status: ApplicationStatus.Running, - config: Config(), + config: Config( + dhtEnable: true + ), events: Events( nodesFound: newAsyncDataEvent[seq[Nid]](), newNodesDiscovered: newAsyncDataEvent[seq[Nid]](),