title: Start and call Ruby HTTP server in the same script
body: <p>I wonder how I could start a Ruby Rack application (such as Sinatra) and call it with Net::HTTP or similar in the same script. Of couse I could do something like...</p>
  
  <pre><code>require 'sinatra/base'
  require 'net/http'
  
  t = Thread.new do
      class App &lt; Sinatra::Base
          get '/' do
              'Hi!'
          end
      end
  
      App.run! :host =&gt; 'localhost', :port =&gt; 1234
  end
  
  sleep 2
  
  puts Net::HTTP.start('localhost', 1234) { |http| http.get('/') }.body
  
  t.join
  
  puts 'Bye!'
  </code></pre>
  
  <p>...but it doesn't feel optimal to sleep for two seconds, waiting for Thin to start. What I need is some kind of callback when the server has started or does anybody have any other suggestions?</p>