How do I test the behavior if a request takes too long with rspec?
I am thinking of using thread to mock this:
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
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
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.
Can I use stub(), expect() or any rspec features to simulate this?
Is there any way that I can pass in a 'block' into stub method
http_request_to_google.stub(:connection).executethisblock(sleep for xx seconds)
.and_throw error ?
any help is appreciated
The below test fails if request doesn't finish in 20 seconds. It also fails if the code in the lambda doesn't raise Timeout::Error.
So, successful scenario is when long_running_stuff raises exception in less than 20 seconds.
require 'timeout'
describe "Test" do
it "should timeout if the request takes too long" do
Timeout::timeout(20) do # 20 seconds
lambda {
long_running_stuff(:timeout => 10.seconds)
}.should raise_error(Timeout::Error)
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With