Skip to content
Browse files

Don't succeed the deferrable client raises exception

* If there is a MySQL error when using EM, don't succeed the deferrable.
* Thanks to: qqshfox/em-synchrony@3448059
  • Loading branch information...
1 parent 75ef9e6 commit 0e0b490058ddd5d71f0b341abfdb3a940d6774b8 @thoughtless committed Mar 15, 2012
Showing with 40 additions and 16 deletions.
  1. +2 −1 lib/mysql2/em.rb
  2. +38 −15 spec/em/em_spec.rb
View
3 lib/mysql2/em.rb
@@ -18,8 +18,9 @@ def notify_readable
result = @client.async_result
rescue Exception => e
@deferable.fail(e)
+ else
+ @deferable.succeed(result)
end
- @deferable.succeed(result)
end
end
View
53 spec/em/em_spec.rb
@@ -62,24 +62,47 @@
}.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
+ context 'when an exception is raised by the client' do
+ let(:client) { Mysql2::EM::Client.new }
+ let(:error) { StandardError.new('some error') }
+ before { client.stub(:async_result).and_raise(error) }
+
+ it "should swallow exceptions raised in by the client" do
+ errors = []
+ EM.run do
+ defer = client.query "SELECT sleep(0.1) as first_query"
+ defer.callback do |result|
+ # This _shouldn't_ be run, but it is 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
- defer.errback do |err|
- errors << err
- EM.stop_event_loop
+ errors.should == [error]
+ end
+
+ it "should fail the deferrable" do
+ callbacks_run = []
+ EM.run do
+ defer = client.query "SELECT sleep(0.025) as first_query"
+ EM.add_timer(0.1) do
+ defer.callback do |result|
+ callbacks_run << :callback
+ # This _shouldn't_ be run, but it is needed to prevent the specs from
+ # freezing if this test fails.
+ EM.stop_event_loop
+ end
+ defer.errback do |err|
+ callbacks_run << :errback
+ EM.stop_event_loop
+ end
+ end
end
+ callbacks_run.should == [:errback]
end
- errors.should == [error]
end
end
rescue LoadError

0 comments on commit 0e0b490

Please sign in to comment.
Something went wrong with that request. Please try again.