Merge branch 'fix_erratic_tests' into 'develop'
Fix flaky rate_limiter_test.exs test "it restricts based on config values" See merge request pleroma/pleroma!3688
This commit is contained in:
commit
84a573877a
1 changed files with 28 additions and 16 deletions
|
@ -48,38 +48,42 @@ defmodule Pleroma.Web.Plugs.RateLimiterTest do
|
||||||
refute RateLimiter.disabled?(build_conn())
|
refute RateLimiter.disabled?(build_conn())
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :erratic
|
|
||||||
test "it restricts based on config values" do
|
test "it restricts based on config values" do
|
||||||
limiter_name = :test_plug_opts
|
limiter_name = :test_plug_opts
|
||||||
scale = 80
|
scale = 80
|
||||||
limit = 5
|
limit = 5
|
||||||
|
|
||||||
clear_config([Pleroma.Web.Endpoint, :http, :ip], {8, 8, 8, 8})
|
clear_config([Pleroma.Web.Endpoint, :http, :ip], {127, 0, 0, 1})
|
||||||
clear_config([:rate_limit, limiter_name], {scale, limit})
|
clear_config([:rate_limit, limiter_name], {scale, limit})
|
||||||
|
|
||||||
plug_opts = RateLimiter.init(name: limiter_name)
|
plug_opts = RateLimiter.init(name: limiter_name)
|
||||||
conn = build_conn(:get, "/")
|
conn = build_conn(:get, "/")
|
||||||
|
|
||||||
for i <- 1..5 do
|
for _ <- 1..5 do
|
||||||
conn = RateLimiter.call(conn, plug_opts)
|
conn_limited = RateLimiter.call(conn, plug_opts)
|
||||||
assert {^i, _} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
|
||||||
Process.sleep(10)
|
refute conn_limited.status == Conn.Status.code(:too_many_requests)
|
||||||
|
refute conn_limited.resp_body
|
||||||
|
refute conn_limited.halted
|
||||||
end
|
end
|
||||||
|
|
||||||
conn = RateLimiter.call(conn, plug_opts)
|
conn_limited = RateLimiter.call(conn, plug_opts)
|
||||||
assert %{"error" => "Throttled"} = ConnTest.json_response(conn, :too_many_requests)
|
assert %{"error" => "Throttled"} = ConnTest.json_response(conn_limited, :too_many_requests)
|
||||||
assert conn.halted
|
assert conn_limited.halted
|
||||||
|
|
||||||
Process.sleep(50)
|
expire_ttl(conn, limiter_name)
|
||||||
|
|
||||||
conn = build_conn(:get, "/")
|
for _ <- 1..5 do
|
||||||
|
conn_limited = RateLimiter.call(conn, plug_opts)
|
||||||
|
|
||||||
conn = RateLimiter.call(conn, plug_opts)
|
refute conn_limited.status == Conn.Status.code(:too_many_requests)
|
||||||
assert {1, 4} = RateLimiter.inspect_bucket(conn, limiter_name, plug_opts)
|
refute conn_limited.resp_body
|
||||||
|
refute conn_limited.halted
|
||||||
|
end
|
||||||
|
|
||||||
refute conn.status == Conn.Status.code(:too_many_requests)
|
conn_limited = RateLimiter.call(conn, plug_opts)
|
||||||
refute conn.resp_body
|
assert %{"error" => "Throttled"} = ConnTest.json_response(conn_limited, :too_many_requests)
|
||||||
refute conn.halted
|
assert conn_limited.halted
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "options" do
|
describe "options" do
|
||||||
|
@ -263,4 +267,12 @@ defmodule Pleroma.Web.Plugs.RateLimiterTest do
|
||||||
|
|
||||||
refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)
|
refute {:err, :not_found} == RateLimiter.inspect_bucket(conn, limiter_name, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def expire_ttl(%{remote_ip: remote_ip} = _conn, bucket_name_root) do
|
||||||
|
bucket_name = "anon:#{bucket_name_root}" |> String.to_atom()
|
||||||
|
key_name = "ip::#{remote_ip |> Tuple.to_list() |> Enum.join(".")}"
|
||||||
|
|
||||||
|
{:ok, bucket_value} = Cachex.get(bucket_name, key_name)
|
||||||
|
Cachex.put(bucket_name, key_name, bucket_value, ttl: -1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue