From 687487d5a5cc675d9a91f2998d4470a633052a65 Mon Sep 17 00:00:00 2001 From: Anton Mironov Date: Mon, 27 Sep 2010 21:46:30 +0800 Subject: [PATCH] Detach before executing callbacks. This allows to make queries in callbacks. --- lib/mysql2/em.rb | 2 +- spec/em/em_spec.rb | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/mysql2/em.rb b/lib/mysql2/em.rb index 6d95549..98da1e6 100644 --- a/lib/mysql2/em.rb +++ b/lib/mysql2/em.rb @@ -13,12 +13,12 @@ module Mysql2 end def notify_readable + detach begin @deferable.succeed(@client.async_result) rescue Exception => e @deferable.fail(e) end - detach end end diff --git a/spec/em/em_spec.rb b/spec/em/em_spec.rb index 2213cff..1254c4c 100644 --- a/spec/em/em_spec.rb +++ b/spec/em/em_spec.rb @@ -19,8 +19,27 @@ describe Mysql2::EM::Client do results << result.first end end - + results[0].keys.should include("second_query") results[1].keys.should include("first_query") end + + it "should support queries in callbacks" do + results = [] + EM.run do + client = Mysql2::EM::Client.new + defer1 = client.query "SELECT sleep(0.025) as first_query" + defer1.callback do |result| + results << result.first + defer2 = client.query "SELECT sleep(0.025) as second_query" + defer2.callback do |result| + results << result.first + EM.stop_event_loop + end + end + end + + results[0].keys.should include("first_query") + results[1].keys.should include("second_query") + end end