logos-delivery/tests/common/test_ratelimit_setting.nim
Prem Chaitanya Prathi ba39ee4a37
fix(tests): libp2p v2.0.0 API migrations across test suite
Three small classes of fix across 12 test files (uncovered by a local
`nim c` sweep of tests/all_tests_common.nim + tests/all_tests_waku.nim):

1. `rng` no longer auto-calls when used as an argument: libp2p v2.0.0
   exports its own `rng` symbol (SwitchBuilder field), so Nim's resolver
   sees the testlib `common.rng` template ambiguously and stops auto-
   calling it. Add explicit `()`:
   - tests/test_helpers.nim
   - tests/waku_filter_v2/waku_filter_utils.nim
   - tests/waku_lightpush/lightpush_utils.nim
   - tests/waku_lightpush_legacy/lightpush_utils.nim
   - tests/node/test_wakunode_filter.nim
   - tests/waku_store/store_utils.nim

2. `PeerId.random()` zero-arg form removed in v2.0.0; new signature
   takes `rng: Rng`. Add `newRng()` + `libp2p/crypto/crypto` import:
   - tests/common/test_requestratelimiter.nim
   - tests/common/test_ratelimit_setting.nim

3. `some()` ambiguity vs `Opt.some` — files that don't directly
   `import std/options` get `Opt.some` template only, breaking calls
   intended for `Option[T]`. Add `std/options` to imports:
   - tests/waku_store/test_wakunode_store.nim
   - tests/waku_relay/test_wakunode_relay.nim

4. Two more API removals:
   - tests/testlib/wakunode.nim — `builders.MaxConnections` was removed;
     use `DefaultMaxConnections` from `libp2p/connmanager`.
   - tests/waku_rln_relay/utils_onchain.nim — `keys.PrivateKey.random
     (rng[])` `rng` ambiguous between testlib/common and eth/keys
     exports; qualify as `common.rng()[]`.

There are still more test-compile errors past this point — this is
incremental progress, not a complete v2.0.0 test-suite migration.
2026-06-04 18:55:26 +05:30

145 lines
4.6 KiB
Nim

# Chronos Test Suite
# (c) Copyright 2022-Present
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)
{.used.}
import testutils/unittests
import chronos, libp2p/stream/connection, libp2p/crypto/crypto
import std/[options, tables]
import ../../waku/common/rate_limit/request_limiter
import ../../waku/common/rate_limit/timed_map
let proto = "ProtocolDescriptor"
let conn1 = Connection(peerId: PeerId.random(newRng()).tryGet())
let conn2 = Connection(peerId: PeerId.random(newRng()).tryGet())
let conn3 = Connection(peerId: PeerId.random(newRng()).tryGet())
suite "RateLimitSetting":
test "Parse rate limit setting - ok":
let test1 = "10/2m"
let test2 = " store : 10 /1h"
let test2b = "storeV3: 12 /1s"
let test3 = "LIGHTPUSH: 10/ 1m"
let test4 = "px:10/2 s "
let test5 = "filter:42/66ms"
let expU = UnlimitedRateLimit
let exp1: RateLimitSetting = (10, 2.minutes)
let exp2: RateLimitSetting = (10, 1.hours)
let exp2b: RateLimitSetting = (12, 1.seconds)
let exp3: RateLimitSetting = (10, 1.minutes)
let exp4: RateLimitSetting = (10, 2.seconds)
let exp5: RateLimitSetting = (42, 66.milliseconds)
let res1 = ProtocolRateLimitSettings.parse(@[test1])
let res2 = ProtocolRateLimitSettings.parse(@[test2])
let res2b = ProtocolRateLimitSettings.parse(@[test2b])
let res3 = ProtocolRateLimitSettings.parse(@[test3])
let res4 = ProtocolRateLimitSettings.parse(@[test4])
let res5 = ProtocolRateLimitSettings.parse(@[test5])
check:
res1.isOk()
res1.get() == {GLOBAL: exp1, FILTER: FilterDefaultPerPeerRateLimit}.toTable()
res2.isOk()
res2.get() ==
{GLOBAL: expU, FILTER: FilterDefaultPerPeerRateLimit, STOREV3: exp2}.toTable()
res2b.isOk()
res2b.get() ==
{GLOBAL: expU, FILTER: FilterDefaultPerPeerRateLimit, STOREV3: exp2b}.toTable()
res3.isOk()
res3.get() ==
{GLOBAL: expU, FILTER: FilterDefaultPerPeerRateLimit, LIGHTPUSH: exp3}.toTable()
res4.isOk()
res4.get() ==
{GLOBAL: expU, FILTER: FilterDefaultPerPeerRateLimit, PEEREXCHG: exp4}.toTable()
res5.isOk()
res5.get() == {GLOBAL: expU, FILTER: exp5}.toTable()
test "Parse rate limit setting - err":
let test1 = "10/2d"
let test2 = " stre : 10 /1h"
let test2b = "storev3: 12 1s"
let test3 = "somethingelse: 10/ 1m"
let test4 = ":px:10/2 s "
let test5 = "filter:p42/66ms"
let res1 = ProtocolRateLimitSettings.parse(@[test1])
let res2 = ProtocolRateLimitSettings.parse(@[test2])
let res2b = ProtocolRateLimitSettings.parse(@[test2b])
let res3 = ProtocolRateLimitSettings.parse(@[test3])
let res4 = ProtocolRateLimitSettings.parse(@[test4])
let res5 = ProtocolRateLimitSettings.parse(@[test5])
check:
res1.isErr()
res2.isErr()
res2b.isErr()
res3.isErr()
res4.isErr()
res5.isErr()
test "Parse rate limit setting - complex":
let expU = UnlimitedRateLimit
let test1 = @["lightpush:2/2ms", "10/2m", " store: 3/3s"]
let exp1 = {
GLOBAL: (10, 2.minutes),
FILTER: FilterDefaultPerPeerRateLimit,
LIGHTPUSH: (2, 2.milliseconds),
STOREV3: (3, 3.seconds),
}.toTable()
let res1 = ProtocolRateLimitSettings.parse(test1)
check:
res1.isOk()
res1.get() == exp1
res1.get().getSetting(PEEREXCHG) == (10, 2.minutes)
res1.get().getSetting(STOREV3) == (3, 3.seconds)
res1.get().getSetting(LIGHTPUSH) == (2, 2.milliseconds)
let test2 = @["lightpush:2/2ms", " store: 3/3s", "px:10/10h", "filter:4/42ms"]
let exp2 = {
GLOBAL: expU,
LIGHTPUSH: (2, 2.milliseconds),
STOREV3: (3, 3.seconds),
FILTER: (4, 42.milliseconds),
PEEREXCHG: (10, 10.hours),
}.toTable()
let res2 = ProtocolRateLimitSettings.parse(test2)
check:
res2.isOk()
res2.get() == exp2
let test3 = @["store:3/3s", "storev3:4/42ms", "storev3:5/5s", "storev3:6/6s"]
let exp3 = {
GLOBAL: expU, FILTER: FilterDefaultPerPeerRateLimit, STOREV3: (6, 6.seconds)
}.toTable()
let res3 = ProtocolRateLimitSettings.parse(test3)
check:
res3.isOk()
res3.get() == exp3
res3.get().getSetting(LIGHTPUSH) == expU
let test4 = newSeq[string](0)
let exp4 = {GLOBAL: expU, FILTER: FilterDefaultPerPeerRateLimit}.toTable()
let res4 = ProtocolRateLimitSettings.parse(test4)
check:
res4.isOk()
res4.get() == exp4
res3.get().getSetting(LIGHTPUSH) == expU