# Testing Thread and sleep

**URL:** <https://www.ruby-forum.com/t/testing-thread-and-sleep/103228>\
**Category:** Ruby\
**Created:** [May 3, 2007, 4:54am UTC](https://www.ruby-forum.com/t/testing-thread-and-sleep/103228 "2007-05-03T04:54:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [May 3, 2007, 4:54am UTC](https://www.ruby-forum.com/t/testing-thread-and-sleep/103228/1 "2007-05-03T04:54:35Z")

</div>

Greetings All:

I’m writing a script that calls an external program using popen. The  
program has the potential to run forever, but I what to kill it if it  
has not found a solution in 10 seconds. I have therefore set up a  
Thread that 'sleep’s for 10 seconds and then calls Process.kill on the  
PID. I’ve been successful in mocking out the popen call and Process,  
but I am having trouble writing a test that ensures the thread will be  
killed. Test::Unit seems to ignore/override threads.

Can anyone give me some advice on how to test threads? It would be  
really nice if I didn’t have to actually wait the full 10 seconds  
either.

John M.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [May 3, 2007, 9:30am UTC](https://www.ruby-forum.com/t/testing-thread-and-sleep/103228/2 "2007-05-03T09:30:11Z")

</div>

On Thu, May 03, 2007 at 11:54:37AM +0900, John M. wrote:

> either.  
> I suggest you post your code; modify it so the process it calls via  
> popen is  
> “sleep 15” or something like that.

I’ve never had problems testing threads; but I _have_ had problems when  
testing code which forks another process (which may include popen).

The problem was that the child process inherited all the Test::Unit  
stuff,  
including its atexit handler. I think the solution was to call exit! at  
the  
end of the child, but I can’t remember exactly.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [May 3, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/testing-thread-and-sleep/103228/3 "2007-05-03T18:11:38Z")

</div>

Brian C. wrote:

> On Thu, May 03, 2007 at 11:54:37AM +0900, John M. wrote:
> 
> > either.  
> > I suggest you post your code; modify it so the process it calls via  
> > popen is  
> > “sleep 15” or something like that.
> 
> I’ve never had problems testing threads; but I _have_ had problems when  
> testing code which forks another process (which may include popen).
> 
> The problem was that the child process inherited all the Test::Unit  
> stuff,  
> including its atexit handler. I think the solution was to call exit! at  
> the  
> end of the child, but I can’t remember exactly.

Actually popen is easy to get around. I rewrote it for the test simply  
return a StringIO like object instead of a new process. I can test the  
process that is called separately.

Per request here is the code in question

This is the method I’m testing:

#Attempt to kill the process ‘pid’ after  
#‘time’ seconds.  
def terminate\_process\_after(pid,time)  
Thread.new do  
sleep time  
begin  
cross\_platform\_end\_process(pid)  
rescue  
end  
end  
end

John

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [May 3, 2007, 9:21pm UTC](https://www.ruby-forum.com/t/testing-thread-and-sleep/103228/4 "2007-05-03T21:21:00Z")

</div>

2007/5/3, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Can anyone give me some advice on how to test threads? It would be  
> really nice if I didn’t have to actually wait the full 10 seconds  
> either.
> 
> John M.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

How about using the timeout module?

require ‘timeout’  
begin  
Timeout::timeout(10) do  
#do stuff…  
end  
rescue Timeout::Error  
puts “Cut short”  
else  
puts “Ran until the end”  
end

Regards,  
Raf

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [May 4, 2007, 9:43pm UTC](https://www.ruby-forum.com/t/testing-thread-and-sleep/103228/5 "2007-05-04T21:43:35Z")

</div>

Brian C. wrote:

> On Fri, May 04, 2007 at 01:11:39AM +0900, John M. wrote:
> 
> > > The problem was that the child process inherited all the Test::Unit
> > 
> > ```
> > end
> > end
> > 
> > ```
> > 
> > end
> 
> Hmm, that’s not what I meant. I meant the full code, complete with test,  
> so  
> it could be run and seen to fail.
> 
> It’s hard for me to replicate exactly what you’re talking about, since  
> IO.popen doesn’t return a pid.

io = IO.popen(’-’, ‘w+’)  
pid = io.pid

Note that ‘fork’ is not supported in windows, only popen.

I believe I have found where my problem was, and a solution. The  
problem is that my main process was ending after a couple of seconds  
worth of tests with the mocked out process and terminate\_process threads  
still running. I needed to Thread.join my mock\_process thread at the  
end of my test.

John M.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [May 3, 2007, 10:01pm UTC](https://www.ruby-forum.com/t/testing-thread-and-sleep/103228/6 "2007-05-03T22:01:06Z")

</div>

On Fri, May 04, 2007 at 01:11:39AM +0900, John M. wrote:

> > The problem was that the child process inherited all the Test::Unit
> 
> ```
> end
> end
> 
> ```
> 
> end

Hmm, that’s not what I meant. I meant the full code, complete with test,  
so  
it could be run and seen to fail.

It’s hard for me to replicate exactly what you’re talking about, since  
IO.popen doesn’t return a pid.

But writing an example using fork and exec works perfectly for me - see  
below. And as you can see, it uses a thread in just the way you seem to  
be.

Regards,

Brian.

## — 8\<

require ‘test/unit’

class Foo  
def terminate\_process\_after(pid,time)  
Thread.new do  
sleep time  
Process.kill ‘TERM’, pid  
end  
end

def doit(cmd)  
fork { exec(cmd) } # returns pid  
end  
end

class TestFoo \< Test::Unit::TestCase  
def setup  
@foo = Foo.new  
end

def test\_single  
pid = @foo.doit(“sleep 4”)  
Process.waitpid(pid)  
end

def test\_short  
pid = @foo.doit(“sleep 4”)  
@foo.terminate\_process\_after(pid, 2)  
Process.waitpid(pid)  
end

def test\_long  
pid = @foo.doit(“sleep 4”)  
@foo.terminate\_process\_after(pid, 6)  
Process.waitpid(pid)  
end  
end
