Skip to content

Commit

Permalink
Add spec about connection pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Yacine Petitprez committed Dec 24, 2018
1 parent 240623e commit 52e6aa9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions spec/sql/connection_pool_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require "../spec_helper"

module ConnectionPoolSpec
extend self

@@count = 0_i64

def self.reinit
reinit_migration_manager
end

describe "Clear::SQL" do
describe "ConnectionPool" do
it "can handle multiple fibers" do

begin
Clear::SQL.execute("CREATE TABLE tests (id serial PRIMARY KEY)")

init = true
spawn do
Clear::SQL.transaction do
Clear::SQL.insert("tests", {id: 1}).execute
sleep 0.2 #< The transaction is not yet commited
end
end

@@count = 0

spawn do
# Not inside the transaction so count must be zero since the transaction is not finished:
sleep 0.1
@@count = Clear::SQL.select.from("tests").count
end

sleep 0.3 # Let the 2 spawn finish...

@@count.should eq 0 #< If one, the connection pool got wrong with the fiber.

# Now the transaction is over, count should be 1
count = Clear::SQL.select.from("tests").count
count.should eq 1
ensure
Clear::SQL.execute("DROP TABLE tests;") unless init
end

end
end
end


end
2 changes: 1 addition & 1 deletion src/clear/sql/logger.cr
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module Clear::SQL::Logger
o = yield
elapsed_time = Time.monotonic - start_time

Clear.logger.debug(("[" + Clear::SQL::Logger.display_time(elapsed_time).colorize.bold.white.to_s + "] #{SQL::Logger.colorize_query(sql)}"))
Clear.logger.debug(("[" + Clear::SQL::Logger.display_time(elapsed_time.to_f).colorize.bold.white.to_s + "] #{SQL::Logger.colorize_query(sql)}"))

return o
rescue e
Expand Down

0 comments on commit 52e6aa9

Please sign in to comment.