From: Dossy Date: 2002-05-10T04:57:16+09:00 Subject: Re: Thread#join doesn't accept a timeout? On 2002.05.10, Nathaniel Talbott wrote: > > Are the threads critical, or could you use the timeout extension? > > timeout_length = 3 # seconds > rd, wr = IO.pipe > begin > timeout(timeout_length) do > # do some stuff > wr.write "done" > wr.close > end > rescue TimeoutError > end > rd.close I did try using the timeout extension, but this just doesn't seem to work: timeout(@timeout) { send(@method_name) } I'm busy right now, so wrapping my mind around why it isn't working will have to wait until later. It's probably something simple, but I'm having a stupid moment right now. > > Also, is there any interest in my Test::Unit::TimedTestCase? > > Funny you should ask... I just ran in to a need to do some performance > tests, here's what I came up with: > > module Test > module Unit > module Assertions > def assert_takes(milliseconds) > start_time = Time.now > yield > end_time = Time.now > actual_time = end_time.to_f - > start_time.to_f > assert(actual_time < > milliseconds, "The operation took #{actual_time - milliseconds} ms > longer than #{milliseconds} ms") > end > end > end > end > > Perhaps it's solving a slightly different problem, but it was nice to > write my test without having to deal with a decorator. Of course, this > doesn't provide a timeout like yours does, although I don't think it > would be hard to add. I was/am considering adding something like this to > Test::Unit, so any comments/suggestions/pros/cons/etc. would be greatly > appreciated. I thought about writing a new assertion as well, but didn't know if that was "okay" or not. I'm too used to open sourced projects where getting changes to the "core" is a long, uphill battle, so I'm used to having to write extensions that don't touch the core. Of course, I just realized yet another reason why Ruby rules ... I can extend the core without having to actually touch any of its files -- aah! I'm going to write an assert_xpath assertion to make my REXML-using test cases easier to write! God, this is great! I guess we could implement #assert_takes_nowait that has a timeout and all of my code can be thrown away. :-) What do you think? Nathaniel, I've posted my code (and unit tests) to this URL: http://panoptic.com/ruby Check it out. It could probably use some work and will be much improved once the Thread#join patch makes it into stable and it can be used instead of my pipes/select work-around. I'll let others help refactor the code ... I'm sure something more elegant than what I threw together will pop out that I just can't see because I'm standing in the forest looking at trees ... -- Dossy -- Dossy Shiobara mail: dossy@panoptic.com Panoptic Computer Network web: http://www.panoptic.com/ "He realized the fastest way to change is to laugh at your own folly -- then you can let go and quickly move on." (p. 70)