110 lines
2.8 KiB
Nim
Raw Normal View History

2025-02-11 16:31:23 +01:00
import pkg/chronos
import pkg/questionable/results
import pkg/asynctest/chronos/unittest
import ../../../codexcrawler/components/timetracker
import ../../../codexcrawler/components/nodestore
import ../../../codexcrawler/utils/asyncdataevent
import ../../../codexcrawler/types
import ../../../codexcrawler/state
2025-02-12 14:48:57 +01:00
import ../mocks/mockstate
import ../mocks/mocknodestore
import ../mocks/mockdht
2025-02-13 10:49:50 +01:00
import ../mocks/mockclock
2025-02-11 16:31:23 +01:00
import ../helpers
suite "TimeTracker":
2025-02-13 10:49:50 +01:00
let now = 123456789.uint64
2025-02-11 16:31:23 +01:00
var
nid: Nid
state: MockState
store: MockNodeStore
2025-02-13 10:49:50 +01:00
clock: MockClock
dht: MockDht
2025-02-11 16:31:23 +01:00
time: TimeTracker
expiredNodesReceived: seq[Nid]
sub: AsyncDataEventSubscription
setup:
nid = genNid()
state = createMockState()
store = createMockNodeStore()
2025-02-13 10:49:50 +01:00
clock = createMockClock()
dht = createMockDht()
2025-02-11 16:31:23 +01:00
2025-02-13 10:49:50 +01:00
clock.setNow = now
2025-02-11 16:31:23 +01:00
# Subscribe to nodesExpired event
expiredNodesReceived = newSeq[Nid]()
proc onExpired(nids: seq[Nid]): Future[?!void] {.async.} =
expiredNodesReceived = nids
return success()
sub = state.events.nodesExpired.subscribe(onExpired)
2025-02-13 12:35:19 +01:00
state.config.checkDelayMins = 11
state.config.expiryDelayMins = 22
2025-02-11 16:31:23 +01:00
2025-02-13 10:49:50 +01:00
time = TimeTracker.new(state, store, dht, clock)
2025-02-11 16:31:23 +01:00
(await time.start()).tryGet()
teardown:
(await time.stop()).tryGet()
await state.events.nodesExpired.unsubscribe(sub)
state.checkAllUnsubscribed()
2025-02-13 12:35:19 +01:00
proc onStepExpiry() {.async.} =
(await state.steppers[0]()).tryGet()
proc onStepRt() {.async.} =
(await state.steppers[1]()).tryGet()
2025-02-12 13:25:37 +01:00
2025-02-11 16:31:23 +01:00
proc createNodeInStore(lastVisit: uint64): Nid =
let entry = NodeEntry(id: genNid(), lastVisit: lastVisit)
store.nodesToIterate.add(entry)
return entry.id
2025-02-13 12:35:19 +01:00
test "start sets steppers for expiry and routingtable load":
check:
state.delays[0] == state.config.checkDelayMins.minutes
state.delays[1] == 30.minutes
2025-02-11 16:31:23 +01:00
test "onStep fires nodesExpired event for expired nodes":
let
2025-02-13 12:35:19 +01:00
expiredTimestamp = now - ((1 + state.config.expiryDelayMins) * 60).uint64
2025-02-11 16:31:23 +01:00
expiredNodeId = createNodeInStore(expiredTimestamp)
2025-02-13 12:35:19 +01:00
await onStepExpiry()
2025-02-11 16:31:23 +01:00
check:
expiredNodeId in expiredNodesReceived
test "onStep does not fire nodesExpired event for nodes that are recent":
let
2025-02-13 12:35:19 +01:00
recentTimestamp = now - ((state.config.expiryDelayMins - 1) * 60).uint64
2025-02-11 16:31:23 +01:00
recentNodeId = createNodeInStore(recentTimestamp)
2025-02-13 12:35:19 +01:00
await onStepExpiry()
2025-02-11 16:31:23 +01:00
check:
recentNodeId notin expiredNodesReceived
test "onStep raises routingTable nodes as nodesFound":
var nodesFound = newSeq[Nid]()
proc onNodesFound(nids: seq[Nid]): Future[?!void] {.async.} =
nodesFound = nids
return success()
let sub = state.events.nodesFound.subscribe(onNodesFound)
dht.routingTable.add(nid)
2025-02-13 12:35:19 +01:00
await onStepRt()
check:
nid in nodesFound
await state.events.nodesFound.unsubscribe(sub)