parroty / exvcr

HTTP request/response recording library for elixir, inspired by VCR.
MIT License
714 stars 133 forks source link

Wrong responses when HTTPoison is used from async Task #191

Closed dluksza closed 1 year ago

dluksza commented 1 year ago

ExVCR will return wrong response when HTTPoison requests are executed from async tasks. It all works fine when cassette is recorded, and the recording is correct, but on replay one response is returned for each call. Here's an example code

def get_pages(article, pages) do
  pages
    |> Enum.map(fn page -> Task.async(fn -> {page, HTTPoison.get!("/articles/#{article}", %{"page" => page})} end)
    |> Task.wait_many() 
    |> Enum.into(%{})
end

test code

test "fetch multiple pages"
  use_cassette "multiple_pages" do
    actual = get_pages("example", [2, 4])

     assert length(actual) == 2
     assert actual[2].page == 2
     assert actual[4].page == 4
  end
end

This will pass on the first execution (when cassette is recorded) but fail on subsequent sections.

When Task.async and Task.wait_many is removed test will always pass.

kacorvus commented 1 year ago

Are you matching on query params?

dluksza commented 1 year ago

I'm using the default configuration. How can I check and set query params match?

dluksza commented 1 year ago

Adding match_requests_on: [:query] to the test case did solve the problem. Thanks @kacorvus

parroty commented 1 year ago

Thank you @kacorvus for the help 🙇