From: Hemant Kumar Date: 2007-01-03T22:51:31+09:00 Subject: Re: Problem in Unit Testing Methods that start new threads On Wed, 2007-01-03 at 22:30 +0900, Hemant Kumar wrote: > I have a bit of doubt, in Unit Testing Programs that start new threads. > Please have a look at the code below: > > > class Foobar > > def hello_world > p "Hello World" > @thread_status = false > end > > def new_thread_start > Thread.new do > sleep(100) > @thread_status = true > end > end > > end > > # here goes the lame test case > require "test/unit" > > module Test::Unit::Assertions > def assert_false(t_object,message=nil) > boolean = !t_object > full_message = build_message(message,' Object is not > false',boolean) > assert_block(full_message) { boolean} > end > end > > class TestFoobar < Test::Unit::TestCase > def setup > @foo = Foobar.new > class << @foo > def ivar var > instance_variable_get(:"@#{var}") > end > end > end > > def test_hello_world > @foo.hello_world > assert_false @foo.ivar(:thread_status) > end > > > def test_new_thread > # sorry for a bit of not so DRY thingy > @foo.hello_world > assert_false @foo.ivar(:thread_status) > @foo.new_thread_start > > # next assert is true because method was started in a new thread > # and control came back immediately, what i would probably want is > # to wait here so that i can have proper check on the method and > # state of the program, but i am not sure, if that's exactly > # approach i should take. > > assert_false @foo.ivar(:thread_status) > end > end > > > Now, as someone suggested on IRC, I can do a join and wait for the > thread to finish. But the problem is, I don't exactly have an instance > to the thread, because its managed by a plugin and i am just using the > plugin to do stuff. > > Any ideas/suggestions are more than welcome. Or is it this a good idea? Thread.list.each { |t| t.join }