require "rails_helper"
require Rails.root.join("app/models/route")

RSpec.describe RoutesController, type: :controller do
  before do
    authenticate_as_stub_user
    FactoryBot.create(:backend, backend_id: "a-backend")
  end

  let(:data) do
    {
      route: {
        incoming_path: "/foo/bar", route_type: "prefix", handler: "backend", backend_id: "a-backend"
      },
    }.to_json
  end

  it "should not fail on multiple simultaneous requests" do
    bypass_rescue
    failed = false

    threads = 4.times.map do
      Thread.new do
        put :update, body: data, format: :json
      rescue Mongo::Error::OperationFailure
        failed = true
      rescue AbstractController::DoubleRenderError
        # this error will happen if both threads succeed, so this is fine.
      end
    end
    threads.each(&:join)

    expect(failed).to be false
  end
end

require "rails_helper"
require Rails.root.join("app/models/route")

RSpec.describe RoutesController, type: :controller do
  before do
    authenticate_as_stub_user
    FactoryBot.create(:backend, backend_id: "a-backend")
  end

  let(:data) do
    {
      route: {
        incoming_path: "/foo/bar", route_type: "prefix", handler: "backend", backend_id: "a-backend"
      },
    }.to_json
  end

  it "should retry on multiple simultaneous requests" do
    allow(Route).to receive(:find_or_initialize_by)
      .and_raise(Mongo::Error::OperationFailure, Route::DUPLICATE_KEY_ERROR)

    expect(Route).to receive(:find_or_initialize_by).exactly(3).times

    expect { put :update, body: data, format: :json }
      .to raise_error(Mongo::Error::OperationFailure)
  end
end

Fix flakey test that depended on threads
Fix flakey test that depended on threads

This rewrites the test to check the underlying behaviour, which is
that we retry a request for a specific kind of error.
