如何测试(rspec)耗时太长的 http 请求?

发布于 2024-12-28 17:46:24 字数 707 浏览 0 评论 0原文

如果 rspec 请求花费的时间太长,我该如何测试行为?

我正在考虑使用线程来模拟这一点:

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

我想确保在首次发出请求后,另一个线程“启动”,在本例中只是休眠 xx 秒。然后我应该期望请求超时,因为执行时间太长

我认为有更好的方法来做到这一点。鉴于我请求的网址不相关。我只是想测试一下,如果执行时间太长,确实会超时。

我可以使用stub()、expect() 或任何rspec 功能来模拟这个吗?

有什么方法可以将“块”传递到存根方法中,

http_request_to_google.stub(:connection).executethisblock(sleep for xx seconds)
.and_throw error ?

感谢任何帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原谅我要高飞 2025-01-04 17:46:24

如果请求未在 20 秒内完成,以下测试将失败。如果 lambda 中的代码没有引发 Timeout::Error,它也会失败。

因此,成功的场景是 long_running_stuff 在 20 秒内引发异常。

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

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
挥剑断情 2025-01-04 17:46:24

如果你纯粹关心 Net::HTTP 引发 Timeout::Error,你总是可以强制它用模拟返回错误, 这里 是您可以与 RSpec 一起使用的各种内容的良好汇编。

这取决于您的确切 Net::HTTP 请求,但诸如 Net::HTTP.should_receive(:request_get).and_raise(Timeout::Error) 之类的内容会跳过任何网络调用,只引发立即出错。

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.

虐人心 2025-01-04 17:46:24

我意识到这个问题很古老,但这是使用 WebMock gem 的另一个解决方案:

stub_request(:any, 'www.google.com').to_timeout

另一个使用 Webmock 存根请求的一个好处是,即使您更换了 HTTP 客户端,这也将继续工作,这将您的测试与代码分离。

I realize this question is ancient, but here's another solution using the WebMock gem:

stub_request(:any, 'www.google.com').to_timeout

Another nice benefit to stubbing the request with Webmock is that this will continue to work even if you swap out your HTTP client, which decouples your tests from your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文