代码之家  ›  专栏  ›  技术社区  ›  vinibrsl

如何使用rspec控制器规范触发并发请求?

  •  0
  • vinibrsl  · 技术社区  · 6 年前

    我试图使用线程和rspec同时触发5个请求,但是这给了我一个 AbstractController::DoubleRenderError 错误。我认为rspec正在为请求共享相同的“上下文”。

    context 'when the request is duplicated' do
      it 'blocks duplicate requests' do
        expect{
    
          threads = 5.times.map do
            Thread.new { post :checkout }
          end
          threads.map(&:join)
    
        }.to change{
          PaymentTransaction.count
        }.by(1)
      end
    end
    

    是否有任何方法可以使用rspec控制器测试来测试并发请求,而不引发此类异常,也不共享相同的“环境”?

    1 回复  |  直到 6 年前
        1
  •  0
  •   vinibrsl    6 年前

    似乎rspec没有一个官方的解决方案。为了解决这个问题,我救了 AbstractController::DoubleRenderError 线程内部出现异常。这解决了它,因此它不是最优雅的解决方案。

    context 'when the request is duplicated' do
      it 'blocks duplicate requests' do
        expect{
    
          threads = 5.times.map do
            Thread.new do
              post :checkout
            rescue AbstractController::DoubleRenderError
            end
          end
          threads.map(&:join)
    
        }.to change{
          PaymentTransaction.count
        }.by(1)
      end
    end