mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
291b65b853
* Allow DB::Pool to be a generic connection pool * Use fully qualified class name for consistency Co-authored-by: Brian J. Cardiff <bcardiff@gmail.com> * Wrap only the necessary code in an `ensure` * Add spec for http client pool * Fix ICE in crystal-sqlite3 Co-authored-by: Brian J. Cardiff <bcardiff@gmail.com>
52 lines
1.4 KiB
Crystal
52 lines
1.4 KiB
Crystal
require "./spec_helper"
|
|
require "./support/http"
|
|
|
|
describe DB::Pool do
|
|
it "distributes evenly the requests" do
|
|
mutex = Mutex.new
|
|
requests_per_connection = Hash(Socket::Address, Int32).new
|
|
|
|
server = HTTP::Server.new do |context|
|
|
remote_address = context.request.remote_address.not_nil!
|
|
mutex.synchronize do
|
|
requests_per_connection[remote_address] ||= 0
|
|
requests_per_connection[remote_address] += 1
|
|
end
|
|
sleep context.request.query_params["delay"].to_f
|
|
context.response.print "ok"
|
|
end
|
|
address = server.bind_unused_port "127.0.0.1"
|
|
|
|
run_server(server) do
|
|
fixed_pool_size = 5
|
|
expected_per_connection = 5
|
|
requests = fixed_pool_size * expected_per_connection
|
|
|
|
pool = DB::Pool.new(
|
|
initial_pool_size: fixed_pool_size,
|
|
max_pool_size: fixed_pool_size,
|
|
max_idle_pool_size: fixed_pool_size) {
|
|
HTTP::Client.new(URI.parse("http://127.0.0.1:#{address.port}/"))
|
|
}
|
|
|
|
done = Channel(Nil).new
|
|
|
|
requests.times do
|
|
spawn do
|
|
pool.checkout do |http|
|
|
http.get("/?delay=0.1")
|
|
end
|
|
done.send(nil)
|
|
end
|
|
end
|
|
|
|
spawn do
|
|
requests.times { done.receive }
|
|
done.close
|
|
end
|
|
wait_for { done.closed? }
|
|
|
|
requests_per_connection.values.should eq([expected_per_connection] * fixed_pool_size)
|
|
end
|
|
end
|
|
end
|