Problem testing certain Thread methods with Test::Unit

D

Daniel Berger

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
 
M

MenTaLguY

# 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
 
D

Daniel Berger

This should be reasonably robust:

t = Thread.new { sleep }
Thread.pass until t.status == "sleep"
t.raise FooError
assert_raise(FooError) { t.join }

That works nicely, thank you.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top