# Unit testing with webrick

**URL:** <https://www.ruby-forum.com/t/unit-testing-with-webrick/55189>\
**Category:** Ruby\
**Created:** [March 17, 2006, 7:03pm UTC](https://www.ruby-forum.com/t/unit-testing-with-webrick/55189 "2006-03-17T19:03:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaronpowell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/7bcc69/32.png) [@aaronpowell](https://www.ruby-forum.com/u/aaronpowell)\
**Post date:** [March 17, 2006, 7:03pm UTC](https://www.ruby-forum.com/t/unit-testing-with-webrick/55189/1 "2006-03-17T19:03:10Z")

</div>

Hi. I’ve written a unit test that starts up WEBrick in a new thread in  
the setup method. I was wondering what the best way would be to  
guarantee that WEBrick is ready to accept connections before leaving the  
setup method.

I was thinking after creating the thread I would attempt to make a  
connection and if the connection didn’t work, do a retry. Here is my  
(not completely implemented) code so far:

class WebTest \< Test::Unit::TestCase  
def setup  
@server = Thread.new {  
s = WEBrick::HTTPServer.new(  
:Port =\> 0,  
:DocumentRoot =\> Dir::pwd + “/htdocs”  
)  
@port = s.config[:Port]

```
  s.start
}

begin
  # Test to see if the server is up
rescue
  # Sleep, then retry
end

```

end  
end

Does anyone have a more elegant way of doing this? Thanks!

–Aaron
