File tree Expand file tree Collapse file tree 1 file changed +15
-16
lines changed Expand file tree Collapse file tree 1 file changed +15
-16
lines changed Original file line number Diff line number Diff line change @@ -498,23 +498,22 @@ def connect *args
498
498
end
499
499
500
500
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
515
510
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 )
518
517
end
519
518
520
519
it "evented async queries should be supported" do
You can’t perform that action at this time.
0 commit comments