Stabilize rate limiter tests by using dependency injection
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
defmodule Req.RateLimiter.Clock do
|
||||
@moduledoc """
|
||||
Behaviour for time operations used by `Req.RateLimiter`.
|
||||
|
||||
Allows injecting a fake clock in tests for deterministic throttle assertions.
|
||||
"""
|
||||
|
||||
@callback now() :: integer()
|
||||
@callback sleep(non_neg_integer()) :: :ok
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
defmodule Req.RateLimiter.SystemClock do
|
||||
@moduledoc """
|
||||
Real clock implementation using `System.monotonic_time/1` and `Process.sleep/1`.
|
||||
"""
|
||||
|
||||
@behaviour Req.RateLimiter.Clock
|
||||
|
||||
@impl true
|
||||
def now, do: System.monotonic_time(:millisecond)
|
||||
|
||||
@impl true
|
||||
def sleep(ms), do: Process.sleep(ms)
|
||||
end
|
||||
Reference in New Issue
Block a user