# Problem testing certain Thread methods with Test::Unit

**URL:** <https://www.ruby-forum.com/t/problem-testing-certain-thread-methods-with-test-unit/122455>\
**Category:** Ruby\
**Created:** [December 7, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/problem-testing-certain-thread-methods-with-test-unit/122455 "2007-12-07T18:45:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 7, 2007, 6:45pm UTC](https://www.ruby-forum.com/t/problem-testing-certain-thread-methods-with-test-unit/122455/1 "2007-12-07T18:45:23Z")

</div>

Hi all,

Ruby 1.8.6

I’m trying to do write some basic tests for Thread#raise.  
Unfortunately, because of the way Test::Unit implements assert\_raise  
(I think) I can’t actually verify that Thread#raise actually works.  
For example:

# Assume @thread created in setup

assert\_raise(FooError){ @thread.raise(FooError) }

The above test won’t work - no error is raised. Any suggestions on how  
I should approach this?

Also, if anyone has any idea how to test that threads abort properly  
with Thread.abort\_on\_exception = true, I’m all ears.

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 7, 2007, 8:35pm UTC](https://www.ruby-forum.com/t/problem-testing-certain-thread-methods-with-test-unit/122455/2 "2007-12-07T20:35:41Z")

</div>

On Sat, 8 Dec 2007 02:33:59 +0900, Daniel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> # Assume @thread created in setup
> 
> assert\_raise(FooError){ @thread.raise(FooError) }
> 
> The above test won’t work - no error is raised. Any suggestions on how  
> I should approach this?

This should be reasonably robust:

t = Thread.new { sleep }  
Thread.pass until t.status == “sleep”  
t.raise FooError  
assert\_raise(FooError) { t.join }

> Also, if anyone has any idea how to test that threads abort properly  
> with Thread.abort\_on\_exception = true, I’m all ears.

I don’t think that’s possible without starting a child Ruby process.

-mental

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 12, 2007, 9:27pm UTC](https://www.ruby-forum.com/t/problem-testing-certain-thread-methods-with-test-unit/122455/3 "2007-12-12T21:27:57Z")

</div>

On Sat, 2007-12-08 at 07:21 +0900, Daniel B. wrote:

> > > Also, if anyone has any idea how to test that threads abort properly  
> > > with Thread.abort\_on\_exception = true, I’m all ears.
> > 
> > I don’t think that’s possible without starting a child Ruby process.
> 
> If that’s what it takes, that’s what I’ll do. Suggestions?

I’m not aware of a portable way to do that offhand, although there was  
an RCR for that floating around a while back.

-mental

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 7, 2007, 11:22pm UTC](https://www.ruby-forum.com/t/problem-testing-certain-thread-methods-with-test-unit/122455/4 "2007-12-07T23:22:13Z")

</div>

On Dec 7, 12:34 pm, MenTaLguY [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thread.pass until t.status == “sleep”  
> t.raise FooError  
> assert\_raise(FooError) { t.join }

That works nicely, thank you.

> > Also, if anyone has any idea how to test that threads abort properly  
> > with Thread.abort\_on\_exception = true, I’m all ears.
> 
> I don’t think that’s possible without starting a child Ruby process.

If that’s what it takes, that’s what I’ll do. Suggestions?

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [December 12, 2007, 9:40pm UTC](https://www.ruby-forum.com/t/problem-testing-certain-thread-methods-with-test-unit/122455/5 "2007-12-12T21:40:23Z")

</div>

On Wed, 12 Dec 2007 12:18:43 +0900, MenTaLguY [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Sat, 2007-12-08 at 07:21 +0900, Daniel B. wrote:
> 
> > > > Also, if anyone has any idea how to test that threads abort properly  
> > > > with Thread.abort\_on\_exception = true, I’m all ears.
> > > 
> > > I don’t think that’s possible without starting a child Ruby process.
> > 
> > If that’s what it takes, that’s what I’ll do. Suggestions?
> 
> I’m not aware of a portable way to do that offhand, although there was  
> an RCR for that floating around a while back.

Absent a more portable way, fork should work.

-mental
