From c5d9b7ff65dde5164a1f88394f1aadce792c3759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 7 Aug 2010 21:52:46 +0100 Subject: [PATCH] Introduce test case for Mysql2::Client in multi-threaded environments --- spec/mysql2/client_spec.rb | 14 ++++++++++++++ spec/spec_helper.rb | 1 + 2 files changed, 15 insertions(+) diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index a6ff3c7..7c5c8ce 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -176,6 +176,20 @@ describe Mysql2::Client do }.should_not raise_error(Mysql2::Error) end + it "threaded queries should be supported" do + threads, results = [], {} + connect = lambda{ Mysql2::Client.new(:host => "localhost", :username => "root") } + Timeout.timeout(0.7) do + 5.times { + threads << Thread.new do + results[Thread.current.object_id] = connect.call.query("SELECT sleep(0.5) as result") + end + } + end + threads.each{|t| t.join } + results.keys.sort.should eql(threads.map{|t| t.object_id }.sort) + end + it "evented async queries should be supported" do # should immediately return nil @client.query("SELECT sleep(0.1)", :async => true).should eql(nil) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b50e6cc..0063bb7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,7 @@ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/..') $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib') require 'mysql2' +require 'timeout' Spec::Runner.configure do |config| config.before(:all) do