Skip to content

Instantly share code, notes, and snippets.

@dbenhur
Created April 12, 2012 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbenhur/2363790 to your computer and use it in GitHub Desktop.
Save dbenhur/2363790 to your computer and use it in GitHub Desktop.
RSpec #should_recieve can't count correctly with threads
require 'rspec'
T = 10000
N = 200
class ConcurrentSender
def initialize(collaborator,message,threads,sends)
@collaborator,@message,@threads,@sends = collaborator,message,threads,sends
@tasks = []
end
def run
@threads.times do
@tasks << Thread.new do
@sends.times { @collaborator.send @message }
end
end
@tasks.each{|t| t.join }
@tasks = []
end
end
describe "should_recieve expectation counter" do
it "should correctly count messages in concurrent execution" do
dummy = Object.new
dummy.should_receive(:foo).exactly(N*T).times
cs = ConcurrentSender.new(dummy,:foo,T,N)
cs.run
end
end
@dbenhur
Copy link
Author

dbenhur commented Apr 12, 2012

$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
$ rspec
F

Failures:

  1) should_recieve expectation counter should correctly count messages in concurrent execution
     Failure/Error: dummy.should_receive(:foo).exactly(N*T).times
       (#<Object:0x7fba905a24e8>).foo(any args)
           expected: 2000000 times
           received: 1999800 times
     # ./spec/thread_safe_expectation_spec.rb:26

Finished in 43.46 seconds
1 example, 1 failure

$ rbenv global jruby-1.5.6 
$ jruby -v
jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2010-12-03 9cf97c3) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_26) [amd64-java]
$ jruby -S rspec
F

Failures:

  1) should_recieve expectation counter should correctly count messages in concurrent execution
     Failure/Error: dummy.should_receive(:foo).exactly(N*T).times
       (#<Object:0x4556a792>).foo(any args)
           expected: 2000000 times
           received: 1923606 times
     # ./spec/thread_safe_expectation_spec.rb:26
     # :1

Finished in 12.45 seconds
1 example, 1 failure

$ rbenv global ruby-1.9.2 
$ ruby -v
ruby 1.9.2p318 (2012-02-14 revision 34678) [x86_64-linux]
$ rspec
.

Finished in 19.45 seconds
1 example, 0 failures

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