title: testing threaded code in ruby
body: <p>I'm writing a <code>delayed_job</code> clone for DataMapper.  I've got what I think is working and tested code except for the thread in the worker process.  I looked to <code>delayed_job</code> for how to test this but there are now tests for that portion of the code.  Below is the code I need to test.  ideas?  (I'm using rspec BTW)</p>
  
  <pre><code>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
  </code></pre>
  
  <p>see also <a href="https://stackoverflow.com/questions/12159/how-should-i-unit-test-threaded-code">this thread</a></p>