title: how do I test (rspec) a http request that takes too long?
body: <p>How do I test the behavior if a request takes too long with rspec?</p>
  
  <p>I am thinking of using thread to mock this: </p>
  
  <pre><code>describe "Test" do 
    it "should timeout if the request takes too long" do 
      lambda {
        thread1 = Thread.new { #net::http request to google.com }
        thread2 = Thread.new { sleep(xx seconds) }
        thread1.join 
        thread2.join
      }.should raise_error
    end 
  end
  </code></pre>
  
  <p>I want to make sure that after the request is first made, another thread "kicks in" which in this case is just a sleep for xx seconds. Then I should expect the request to timeout because it takes too long to execute</p>
  
  <p>I think that there are better ways to do this. Given the fact that the url I am requesting is not relevant. I just want to test that it will indeed timeout if it takes too long to execute.</p>
  
  <p>Can I use stub(), expect() or any rspec features to simulate this? </p>
  
  <p>Is there any way that I can pass in a 'block' into stub method</p>
  
  <pre><code>http_request_to_google.stub(:connection).executethisblock(sleep for xx seconds)
  .and_throw error ?
  </code></pre>
  
  <p>any help is appreciated</p>