"""Tests for NetDB throttle components.""" from i2p_netdb.throttle import NegativeLookupCache, FloodThrottler, LookupThrottler def test_negative_cache_after_max_fails(): cache = NegativeLookupCache() key = b"\x01" * 32 assert not cache.should_skip(key) for _ in range(3): cache.lookup_failed(key) assert cache.should_skip(key) def test_negative_cache_clear_resets(): cache = NegativeLookupCache() key = b"\x02" * 32 for _ in range(3): cache.lookup_failed(key) cache.clear() assert not cache.should_skip(key) def test_bad_destination_permanent(): cache = NegativeLookupCache() key = b"\x03" * 32 cache.add_bad_destination(key) assert cache.is_bad_destination(key) cache.clear() assert cache.is_bad_destination(key) # survives clear def test_flood_throttle_limit(): ft = FloodThrottler() peer = b"\x04" * 32 for _ in range(3): assert not ft.should_throttle(peer) assert ft.should_throttle(peer) def test_flood_throttle_clear(): ft = FloodThrottler() peer = b"\x05" * 32 for _ in range(4): ft.should_throttle(peer) ft.clear() assert not ft.should_throttle(peer) def test_lookup_throttle_duplicate(): lt = LookupThrottler() key = b"\x06" * 32 gw = b"\x07" * 32 assert not lt.should_throttle(key, 100, gw) assert lt.should_throttle(key, 100, gw) # duplicate def test_lookup_throttle_different_tunnel(): lt = LookupThrottler() key = b"\x08" * 32 gw = b"\x09" * 32 assert not lt.should_throttle(key, 100, gw) assert not lt.should_throttle(key, 200, gw) # different tunnel