Problem testing certain Thread methods with Test::Unit

Discussion in 'Ruby' started by Daniel Berger, Dec 7, 2007.

  1. 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
     
    Daniel Berger, Dec 7, 2007
    #1
    1. Advertising

  2. Daniel Berger

    MenTaLguY Guest

    On Sat, 8 Dec 2007 02:33:59 +0900, Daniel Berger <> 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
     
    MenTaLguY, Dec 7, 2007
    #2
    1. Advertising

  3. On Dec 7, 12:34 pm, MenTaLguY <> wrote:
    > On Sat, 8 Dec 2007 02:33:59 +0900, Daniel Berger <> 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 }


    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
     
    Daniel Berger, Dec 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edvard Majakari
    Replies:
    4
    Views:
    688
    Edvard Majakari
    Feb 25, 2005
  2. Ulrich Eckhardt

    unit-profiling, similar to unit-testing

    Ulrich Eckhardt, Nov 16, 2011, in forum: Python
    Replies:
    6
    Views:
    336
    Roy Smith
    Nov 18, 2011
  3. Hemant Kumar
    Replies:
    12
    Views:
    184
    Pit Capitain
    Jan 4, 2007
  4. Bill Mosteller
    Replies:
    0
    Views:
    230
    Bill Mosteller
    Oct 22, 2009
  5. timr
    Replies:
    2
    Views:
    168
Loading...

Share This Page