Created
March 13, 2024 22:55
-
-
Save oliveigah/cbe7fa1ecb711a6006af5dd05c6803e7 to your computer and use it in GitHub Desktop.
finch metrics test with idle connections
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test "get pool status with not reusable connections", %{bypass: bypass, finch_name: finch_name} do | |
start_supervised!( | |
{Finch, | |
name: finch_name, | |
pools: %{ | |
default: [ | |
protocols: [:http1], | |
start_pool_metrics?: true, | |
conn_max_idle_time: 1, | |
size: 10 | |
] | |
}} | |
) | |
shp = shp_from_bypass(bypass) | |
parent = self() | |
Bypass.expect(bypass, "GET", "/", fn conn -> | |
["number", number] = String.split(conn.query_string, "=") | |
send(parent, {:ping_bypass, number}) | |
Process.sleep(:timer.seconds(1)) | |
Plug.Conn.send_resp(conn, 200, "OK") | |
end) | |
refs = | |
Enum.map(1..8, fn i -> | |
ref = | |
Finch.build(:get, endpoint(bypass, "?number=#{i}")) | |
|> Finch.async_request(finch_name) | |
{ref, i} | |
end) | |
assert_receive {:ping_bypass, "8"}, 500 | |
assert {:ok, | |
[ | |
%PoolMetrics{ | |
pool_index: 1, | |
pool_size: 10, | |
available_connections: 2, | |
in_use_connections: 8 | |
} | |
]} = Finch.get_pool_status(finch_name, shp) | |
Enum.each(refs, fn {req_ref, _number} -> assert_receive({^req_ref, {:status, 200}}, 2000) end) | |
wait_connection_checkin() | |
assert {:ok, | |
[ | |
%PoolMetrics{ | |
pool_index: 1, | |
pool_size: 10, | |
available_connections: 10, | |
in_use_connections: 0 | |
} | |
]} = Finch.get_pool_status(finch_name, shp) | |
# For now on the connections on the pool will be closed due to idle timeout | |
refs = | |
Enum.map(1..8, fn i -> | |
ref = | |
Finch.build(:get, endpoint(bypass, "?number=#{i}")) | |
|> Finch.async_request(finch_name) | |
{ref, i} | |
end) | |
assert_receive {:ping_bypass, "8"}, 500 | |
assert {:ok, | |
[ | |
%PoolMetrics{ | |
pool_index: 1, | |
pool_size: 10, | |
available_connections: 2, | |
in_use_connections: 8 | |
} | |
]} = Finch.get_pool_status(finch_name, shp) | |
Enum.each(refs, fn {req_ref, _number} -> assert_receive({^req_ref, {:status, 200}}, 2000) end) | |
wait_connection_checkin() | |
assert {:ok, | |
[ | |
%PoolMetrics{ | |
pool_index: 1, | |
pool_size: 10, | |
available_connections: 10, | |
in_use_connections: 0 | |
} | |
]} = Finch.get_pool_status(finch_name, shp) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment