Skip to content

Instantly share code, notes, and snippets.

@tombh
Created June 12, 2014 10:42
Show Gist options
  • Save tombh/ffa163e8c05beb3c8317 to your computer and use it in GitHub Desktop.
Save tombh/ffa163e8c05beb3c8317 to your computer and use it in GitHub Desktop.
Rspec exception getting caught by async method
require 'celluloid'
# It wasn't until I commented out this line that I realised what was actually going on below
# Celluloid.logger = nil
class Foo
include Celluloid
def do
bar
end
def bar; end
end
describe Foo do
let(:instance){Foo.new}
it 'should detect a message being sent' do
expect(instance.wrapped_object).to receive(:bar)
instance.async.do
sleep 0.1
end
it 'THIS TEST SHOULD FAIL' do
expect(instance.wrapped_object).to_not receive(:bar)
instance.async.do
sleep 0.1
end
end
@tombh
Copy link
Author

tombh commented Jun 12, 2014

Output of above spec:

Foo
  should detect a message being sent
E, [2014-06-12T11:45:47.513569 #28415] ERROR -- : Foo crashed!
RSpec::Mocks::MockExpectationError: (#<Foo:0x007f0066a70b08>).bar(no args)
    expected: 0 times with any arguments
    received: 1 time
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-mocks-3.0.0/lib/rspec/mocks/error_generator.rb:206:in `__raise'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-mocks-3.0.0/lib/rspec/mocks/error_generator.rb:64:in `raise_expectation_error'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-mocks-3.0.0/lib/rspec/mocks/message_expectation.rb:477:in `invoke_incrementing_actual_calls_by'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-mocks-3.0.0/lib/rspec/mocks/message_expectation.rb:213:in `invoke'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-mocks-3.0.0/lib/rspec/mocks/proxy.rb:165:in `message_received'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-mocks-3.0.0/lib/rspec/mocks/proxy.rb:301:in `message_received'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-mocks-3.0.0/lib/rspec/mocks/method_double.rb:73:in `proxy_method_invoked'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/rspec-mocks-3.0.0/lib/rspec/mocks/method_double.rb:60:in `block (2 levels) in define_proxy_method'
        /home/tombh/Workspace/peas/to_not_receive_spec.rb:9:in `do'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/celluloid-0.15.2/lib/celluloid/calls.rb:25:in `public_send'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/celluloid-0.15.2/lib/celluloid/calls.rb:25:in `dispatch'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/celluloid-0.15.2/lib/celluloid/calls.rb:122:in `dispatch'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/celluloid-0.15.2/lib/celluloid/actor.rb:322:in `block in handle_message'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/celluloid-0.15.2/lib/celluloid/actor.rb:416:in `block in task'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/celluloid-0.15.2/lib/celluloid/tasks.rb:55:in `block in initialize'
        /home/tombh/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/celluloid-0.15.2/lib/celluloid/tasks/task_fiber.rb:13:in `block in create'
  THIS TEST SHOULD FAIL

Finished in 0.20842 seconds (files took 0.11631 seconds to load)
2 examples, 0 failures
D, [2014-06-12T11:45:47.613333 #28415] DEBUG -- : Terminating 1 actor...

@tarcieri
Copy link

While this may very well be a bug, the test is inherently racy

@tombh
Copy link
Author

tombh commented Jul 12, 2014

Sorry, I never got a notification about your response. But thanks. I seem to remember I fixed it by taking a completely different approach. I do still sometimes find myself using sleeps in Celluloid specs, which can't be good. I just need to be more imaginative I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment