From 96d94fa72ae70ec96147af5190ac962162198212 Mon Sep 17 00:00:00 2001 From: "W. Andrew Loe III" Date: Wed, 15 Jun 2011 13:50:45 -0700 Subject: [PATCH 1/4] Spec demonstrating issue #99 --- spec/mysql2/client_spec.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index e8280bc19..bd0863610 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -165,6 +165,19 @@ def connect *args end end + it "should handle Timeouts without leaving the connection hanging" do + begin + Timeout.timeout(1) do + @client.query("SELECT sleep(2)") + end + rescue Timeout::Error + end + + lambda { + @client.query("SELECT 1") + }.should_not raise_error(Mysql2::Error) + end + it "#socket should return a Fixnum (file descriptor from C)" do @client.socket.class.should eql(Fixnum) @client.socket.should_not eql(0) From e941873101d73c2db131c4366675dbab512e5e99 Mon Sep 17 00:00:00 2001 From: "W. Andrew Loe III" Date: Wed, 15 Jun 2011 17:16:16 -0700 Subject: [PATCH 2/4] Use a local client that has reconnect true on. (Common in Rails). Conflicts: spec/mysql2/client_spec.rb --- spec/mysql2/client_spec.rb | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index bd0863610..f3cdc4715 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -165,19 +165,6 @@ def connect *args end end - it "should handle Timeouts without leaving the connection hanging" do - begin - Timeout.timeout(1) do - @client.query("SELECT sleep(2)") - end - rescue Timeout::Error - end - - lambda { - @client.query("SELECT 1") - }.should_not raise_error(Mysql2::Error) - end - it "#socket should return a Fixnum (file descriptor from C)" do @client.socket.class.should eql(Fixnum) @client.socket.should_not eql(0) @@ -190,6 +177,20 @@ def connect *args }.should raise_error(Mysql2::Error) end + it "should handle Timeouts without leaving the connection hanging if reconnect is true" do + client = Mysql2::Client.new(:reconnect => true) + begin + Timeout.timeout(1) do + client.query("SELECT sleep(2)") + end + rescue Timeout::Error + end + + lambda { + client.query("SELECT 1") + }.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") } From 27fe0dafe7b1645602ec88422e59c283b46cb2dd Mon Sep 17 00:00:00 2001 From: "W. Andrew Loe III" Date: Wed, 15 Jun 2011 17:42:46 -0700 Subject: [PATCH 3/4] Refactor this test to check for a closed connection. --- spec/mysql2/client_spec.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index f3cdc4715..a2ddd94aa 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -177,18 +177,17 @@ def connect *args }.should raise_error(Mysql2::Error) end - it "should handle Timeouts without leaving the connection hanging if reconnect is true" do - client = Mysql2::Client.new(:reconnect => true) + it "should close the connection when an exception is raised" do begin Timeout.timeout(1) do - client.query("SELECT sleep(2)") + @client.query("SELECT sleep(2)") end rescue Timeout::Error end - + lambda { - client.query("SELECT 1") - }.should_not raise_error(Mysql2::Error) + @client.query("SELECT 1") + }.should raise_error(Mysql2::Error, 'closed MySQL connection') end it "threaded queries should be supported" do From bf6bccd91ac9d29324fe7a55d1a55e3c1adad454 Mon Sep 17 00:00:00 2001 From: "W. Andrew Loe III" Date: Wed, 15 Jun 2011 17:52:56 -0700 Subject: [PATCH 4/4] Test with reconnect => true, common in Rails. --- spec/mysql2/client_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/mysql2/client_spec.rb b/spec/mysql2/client_spec.rb index a2ddd94aa..8a0bbcd8f 100644 --- a/spec/mysql2/client_spec.rb +++ b/spec/mysql2/client_spec.rb @@ -190,6 +190,20 @@ def connect *args }.should raise_error(Mysql2::Error, 'closed MySQL connection') end + it "should handle Timeouts without leaving the connection hanging if reconnect is true" do + client = Mysql2::Client.new(:reconnect => true) + begin + Timeout.timeout(1) do + client.query("SELECT sleep(2)") + end + rescue Timeout::Error + end + + lambda { + client.query("SELECT 1") + }.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") }