2023-05-18 08:24:17 +00:00
|
|
|
{.used.}
|
2022-05-11 08:38:43 +00:00
|
|
|
|
2023-05-18 08:24:17 +00:00
|
|
|
# Nim-Libp2p
|
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2022-05-11 08:38:43 +00:00
|
|
|
|
|
|
|
# MacOs has some nasty jitter when sleeping
|
|
|
|
# (up to 7 ms), so we skip test there
|
|
|
|
when not defined(macosx):
|
2023-05-18 08:24:17 +00:00
|
|
|
import chronos
|
|
|
|
|
|
|
|
import ../libp2p/utils/heartbeat
|
|
|
|
import ./helpers
|
|
|
|
|
2022-05-11 08:38:43 +00:00
|
|
|
suite "Heartbeat":
|
|
|
|
asyncTest "simple heartbeat":
|
|
|
|
var i = 0
|
|
|
|
proc t() {.async.} =
|
2024-03-01 13:06:42 +00:00
|
|
|
heartbeat "shouldn't see this", 50.milliseconds:
|
2022-05-11 08:38:43 +00:00
|
|
|
i.inc()
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2022-05-11 08:38:43 +00:00
|
|
|
let hb = t()
|
2024-03-01 13:06:42 +00:00
|
|
|
await sleepAsync(500.milliseconds)
|
2022-05-11 08:38:43 +00:00
|
|
|
await hb.cancelAndWait()
|
|
|
|
check:
|
2024-03-05 15:05:21 +00:00
|
|
|
i in 9 .. 12
|
2022-05-11 08:38:43 +00:00
|
|
|
|
|
|
|
asyncTest "change heartbeat period on the fly":
|
|
|
|
var i = 0
|
|
|
|
proc t() {.async.} =
|
|
|
|
var period = 30.milliseconds
|
|
|
|
heartbeat "shouldn't see this", period:
|
|
|
|
i.inc()
|
|
|
|
if i >= 4:
|
|
|
|
period = 75.milliseconds
|
2024-06-11 15:18:06 +00:00
|
|
|
|
2022-05-11 08:38:43 +00:00
|
|
|
let hb = t()
|
|
|
|
await sleepAsync(500.milliseconds)
|
|
|
|
await hb.cancelAndWait()
|
|
|
|
|
|
|
|
# 4x 30 ms heartbeat = 120ms
|
|
|
|
# (500 ms - 120 ms) / 75ms = 5x 75ms
|
|
|
|
# total 9
|
|
|
|
check:
|
2024-03-05 15:05:21 +00:00
|
|
|
i in 8 .. 11
|
2022-05-11 08:38:43 +00:00
|
|
|
|
|
|
|
asyncTest "catch up on slow heartbeat":
|
|
|
|
var i = 0
|
|
|
|
proc t() {.async.} =
|
|
|
|
heartbeat "this is normal", 30.milliseconds:
|
|
|
|
if i < 3:
|
|
|
|
await sleepAsync(150.milliseconds)
|
|
|
|
i.inc()
|
|
|
|
|
|
|
|
let hb = t()
|
|
|
|
await sleepAsync(900.milliseconds)
|
|
|
|
await hb.cancelAndWait()
|
|
|
|
# 3x (150ms heartbeat + 30ms interval) = 540ms
|
|
|
|
# 360ms remaining, / 30ms = 12x
|
|
|
|
# total 15
|
|
|
|
check:
|
2024-03-05 15:05:21 +00:00
|
|
|
i in 14 .. 17
|