Checking the thread code in ruby

I am writing a delayed_job clone for a DataMapper. I have what I think works and is checked by code, with the exception of the thread in the workflow. I looked at delayed_job how to check this, but now there are tests for this part of the code. Below is the code I need to check. ideas? (I am using rspec BTW)

 def start say "*** Starting job worker #{@name}" t = Thread.new do loop do delay = Update.work_off(self) #this method well tested break if $exit sleep delay break if $exit end clear_locks end trap('TERM') { terminate_with t } trap('INT') { terminate_with t } trap('USR1') do say "Wakeup Signal Caught" t.run end 

see also this thread

+4
source share
4 answers

You can start an employee as a subprocess during testing, waiting for it to fully run, and then check output / send signals for it.

I suspect that you can find many specific testing ideas in this area from the Unicorn project.

+3
source

The best approach, I believe, is to drown out the Thread.new method and make sure that any β€œcomplex” material has its own method that can be tested individually. So you will have something like this:

 class Foo def start Thread.new do do_something end end def do_something loop do foo.bar(bar.foo) end end end 

Then you check as follows:

 describe Foo it "starts thread running do_something" do f = Foo.new expect(Thread).to receive(:new).and_yield expect(f).to receive(:do_something) f.start end it "do_something loops with and calls foo.bar with bar.foo" do f = Foo.new expect(f).to receive(:loop).and_yield #for multiple yields: receive(:loop).and_yield.and_yield.and_yield... expect(foo).to receive(:bar).with(bar.foo) f.do_something end end 

Thus, you do not need to go around so much to get the desired result.

+8
source

It is not possible to fully test streams. Best of all you can use mocks.

(something like) object.should_recieve (: trap) .with ('TERM') and yield object.start

0
source

How about getting the stream correctly in your test.

 Thread.stub(:new).and_yield start # assertions... 
0
source

All Articles