Odysseus

Odysseus

Race condition when mocking concurrent API calls

Hello, I’m trying to test some code that fires off multiple external API calls in parallel using Task.async_stream(). I’m using Mox to mock these external api calls like you see below. They work 95% of the time, but sometimes the second item’s api call fires off first, which results in a failed test. It seems that Mox cares about the order it receives its calls. Is there a way around this?

code:

Task.async_stream(employee_ids, fn emp_id -> EmployeeClient.get(emp_id) end)

Mocks:

expect(Mock, :get, fn "/employees/1" ->
  {:ok,
   %HTTPoison.Response{
    status_code: 200
   }}
end)

expect(Mock, :get, fn "/employees/2" ->
  {:ok,
   %HTTPoison.Response{
    status_code: 200
   }}
end)

Error msg:

** (FunctionClauseError) no function clause matching in anonymous fn/1 in ----edited out file names----: anonymous fn("/employees/2") in ----edited out file names-----

Thanks!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Right as @hauleth notes, those definitions are clobbering each other. If you want to pattern match at the function level, do so at the function level:

expect(Mock, :get, fn
"/employees/1" ->
  {:ok,
   %HTTPoison.Response{
    status_code: 200
   }}

"/employees/2" ->
  {:ok,
   %HTTPoison.Response{
    status_code: 200
   }}
end)

Last Post!

Odysseus

Odysseus

I believe you can because I have 3 different routes mocked this way, and the others work without overwriting and pass 100% of the time. Its only this route that’s expecting concurrent calls that fails and its only failing about 5% of the time

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement