Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7169863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:05:48+00:00 2026-05-28T15:05:48+00:00

How do I test the behavior if a request takes too long with rspec?

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T15:05:49+00:00Added an answer on May 28, 2026 at 3:05 pm

    If you purely care about Net::HTTP raising a Timeout::Error, you could always just force it to return the error with a mock, here is a good compilation of various things you can use with RSpec.

    It would depend on your exact Net::HTTP request, but something such as Net::HTTP.should_receive(:request_get).and_raise(Timeout::Error) would skip any networking calls and just raise the error immediately.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
I'm really confused here, can someone explain this to me? request: http://example.com/test.php?var=String's $a =
'mvn release:perform' takes too long to deploy artifacts to Archiva. The logs shows that
I'm using OCMock trying to test the behavior of NSURLConnection. Here's the incomplete test:
I'm experiencing a really bizarre behavior when using the test client in django. I'm
Since I started to develop in a test/behavior driven style, I appreciated the ability
I want to test the behavior of a certain piece of .NET code in
I just wrote some code to test the behavior of std::equal, and came away
Both are BDD (Behavior Driven Development) capable unit test frameworks for Scala written in
If you want to move your development process from Test-Driven Development to Behavior-Driven Development

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.