parroty / exvcr

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

Async tests using Bypass interfere with ExVcr tests #192

Open maxmarcon opened 1 year ago

maxmarcon commented 1 year ago

First of all, thanks for creating this library! I really want to use it as I feel it would allow me to write tests that interact with external APIs more efficiently than with my current approach.

What is my current approach? I use Bypass.

So right now I have a bunch of legacy tests for different REST APIs that use Bypass. My HTTP client is Tesla + Finch.

I wrote a test for a new API using ExVcr. When I run the ExVcr test asynchronously together with the other async Bypass-based tests, some of the Bypass tests always fail. Which tests exactly fail is random and depends on the order of execution I guess. If I run the ExVcr test with async: false everything is fine.

What could be the reason behind this issue? My understanding is that ExVcr mocks the functions in the Finch module. Are these mocks global? If that's the case then it's no surprise that the other async Bypass-based tests occasionally fail, because they need to hit the real Finch implementation to talk to the Bypass server.

Am I right in this assumption? If so, is there a way to restrict the mocks to the current test only, so that other tests running in parallel will use the real implementation?

this is the preamble of my test file:

  use ExVCR.Mock, adapter: ExVCR.Adapter.Finch

  setup do
    ExVCR.Config.cassette_library_dir("test/fixtures/vcr_cassettes/onit")
    ExVCR.Config.filter_request_headers("authorization")

    :ok
  end

I have no global config for ExVCR.

For Tesla I have this in my config:

config :tesla, :adapter, {Tesla.Adapter.Finch, name: DataIngestion.Finch}

And I start the Finch process with this child spec in my application.ex:

{Finch, name: DataIngestion.Finch},

(All very standard as you can see)

ghost commented 1 year ago

This is a wild guess, but maybe try "clear mock" as mentioned in this issue: https://github.com/parroty/exvcr/issues/136 ?

maxmarcon commented 1 year ago

Thanks. I tried but it doesn't work. And it makes sense: the tests are running concurrently, so clearing the mocks after a test doesn't guarantee in any way that another test running concurrently won't use the mocks

ghost commented 1 year ago

Discussed in elixirforum - https://elixirforum.com/t/approach-for-numerous-edge-cases-http-get-exvcr/50291/15?u=kanishka - probably resolved.