Skip to content

Commit 52a6d2d

Browse files
committed
Clarify usage of Timeout.timeout
1 parent 9f00684 commit 52a6d2d

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

spec/mysql2/client_spec.rb

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -498,23 +498,22 @@ def connect *args
498498
end
499499

500500
it "threaded queries should be supported" do
501-
threads, results = [], {}
502-
lock = Mutex.new
503-
connect = lambda{
504-
Mysql2::Client.new(DatabaseCredentials['root'])
505-
}
506-
Timeout.timeout(0.7) do
507-
5.times {
508-
threads << Thread.new do
509-
result = connect.call.query("SELECT sleep(0.5) as result")
510-
lock.synchronize do
511-
results[Thread.current.object_id] = result
512-
end
513-
end
514-
}
501+
sleep_time = 0.5
502+
503+
# Note that each thread opens its own database connection
504+
threads = 5.times.map do
505+
Thread.new do
506+
client = Mysql2::Client.new(DatabaseCredentials.fetch('root'))
507+
client.query("SELECT SLEEP(#{sleep_time})")
508+
Thread.current.object_id
509+
end
515510
end
516-
threads.each{|t| t.join }
517-
results.keys.sort.should eql(threads.map{|t| t.object_id }.sort)
511+
512+
# This timeout demonstrates that the threads are sleeping concurrently:
513+
# In the serial case, the timeout would fire and the test would fail
514+
values = Timeout.timeout(sleep_time * 1.1) { threads.map(&:value) }
515+
516+
values.should =~ threads.map(&:object_id)
518517
end
519518

520519
it "evented async queries should be supported" do

0 commit comments

Comments
 (0)