Dossy [mailto:dossy / panoptic.com] wrote:

> In trying to port JUnitPerf (which supplies nice test 
> decorators for TimedTest and such) to Test::Unit for Ruby, I 
> realized that Thread#join takes no args, unlike Java where 
> you can optionally pass a timeout.
> 
> I hacked around it like so:
> 
>   timeout = 3 # seconds
>   rd, wr = IO.pipe
>   t = Thread.new do
>     # do some stuff
>     wr.write "done"
>     wr.close
>   end
>   selected = select([rd], nil, nil, timeout)
>   if selected.nil? or selected[0].size == 0
>     t.kill
>   end
>   rd.close
>   t.join

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


> 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.


Nathaniel

<:((><
+ - -
| RoleModel Software, Inc.
| EQUIP VI