本文介绍了在Ruby中测试线程代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为DataMapper写一个delayed_job克隆.除了工作进程中的线程之外,我已经拥有了我认为可以正常工作并经过测试的代码.我在delayed_job上寻找了如何进行测试的方法,但是现在对该部分代码进行了测试.下面是我需要测试的代码.想法? (我正在使用rspec BTW)

I'm writing a delayed_job 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 delayed_job 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)

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

另请参见此线程

推荐答案

在测试时,您可以将worker作为子进程启动,等待其完全启动,然后检查输出/向其发送信号.

You could start the worker as a subprocess when testing, waiting for it to fully start, and then check the output / send signals to it.

我怀疑您可以在 Unicorn 项目中获得很多具体的测试思路.

I suspect you can pick up quite a few concrete testing ideas in this area from the Unicorn project.

这篇关于在Ruby中测试线程代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 22:38