Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Don't fail deferrables when callbacks raise exceptions

* Only fail the deferrable if the exception came from the client.
  • Loading branch information...
commit 75ef9e6e7e1ca0b364c08b291f0765f299b1da0e 1 parent d81ba95
@thoughtless authored
Showing with 40 additions and 2 deletions.
  1. +3 −2 lib/mysql2/em.rb
  2. +37 −0 spec/em/em_spec.rb
View
5 lib/mysql2/em.rb
@@ -15,10 +15,11 @@ def initialize(client, deferable)
def notify_readable
detach
begin
- @deferable.succeed(@client.async_result)
+ result = @client.async_result
rescue Exception => e
@deferable.fail(e)
end
+ @deferable.succeed(result)
end
end
@@ -34,4 +35,4 @@ def query(sql, opts={})
end
end
end
-end
+end
View
37 spec/em/em_spec.rb
@@ -44,6 +44,43 @@
results[0].keys.should include("first_query")
results[1].keys.should include("second_query")
end
+
+ it "should not swallow exceptions raised in callbacks" do
+ lambda {
+ EM.run do
+ client = Mysql2::EM::Client.new
+ defer = client.query "SELECT sleep(0.1) as first_query"
+ defer.callback do |result|
+ raise 'some error'
+ end
+ defer.errback do |err|
+ # This _shouldn't_ be run, but it needed to prevent the specs from
+ # freezing if this test fails.
+ EM.stop_event_loop
+ end
+ end
+ }.should raise_error
+ end
+
+ it "should swallow exceptions raised in by the client" do
+ errors = []
+ error = StandardError.new('some error')
+ EM.run do
+ client = Mysql2::EM::Client.new
+ defer = client.query "SELECT sleep(0.1) as first_query"
+ client.stub(:async_result).and_raise(error)
+ defer.callback do |result|
+ # This _shouldn't_ be run, but it needed to prevent the specs from
+ # freezing if this test fails.
+ EM.stop_event_loop
+ end
+ defer.errback do |err|
+ errors << err
+ EM.stop_event_loop
+ end
+ end
+ errors.should == [error]
+ end
end
rescue LoadError
puts "EventMachine not installed, skipping the specs that use it"
Please sign in to comment.
Something went wrong with that request. Please try again.