Fl4m3Ph03n1x

Fl4m3Ph03n1x

--breakpoint flag changes the behaviour of mix test using bypass

Background

I am trying to do an integration test for a simple terminal application that I am writting.
This application is basically a Supervisor, which supervises other smaller libraries.

The communication within the project is mostly asynchronous, with processes sending messages to callers.

Problem

One of the tests I make requires an HTTP request, so I decided to use bypass for this. However, the problem is rather surprising to me. Here is the test:

    setup do
      bypass = Bypass.open(port: 8082)
      credentials = %{email: "an_email", pass: "a_password"}

      %{
        credentials: credentials,
        bypass: bypass
      }
    end


  test "logs in user correctly", %{bypass: bypass, credentials: credentials} do

      Bypass.expect_once(bypass, fn conn ->
        IO.inspect(conn.request_path, label: "REQ PATH")
        IO.inspect(conn.method, label: "METHOD")
        Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
      end)

     # start the application
      _manager_pid = start_link_supervised!(ManagerSupervisor)
      
       # this should make a GET request to bypass but ...
       Manager.login(credentials, false)
    end

If I simply run mix test this simple test fails as Bypass received no requests.

1) test login logs in user correctly (Manager.WorkerTest)
     test/integration/manager_test.exs:83
     No HTTP request arrived at Bypass

Confusion

However, confunsingly enough, if I use the --breakpoints feature by running iex -S mix test --breakpoints I can make the test pass by doing some simple steps:

  • Press n line by line.
  • When reaching the last line (Manager.login(credentials, false), I do not execute it with n or c. Instead I manually type the command Manager.login(credentials, false).
  • The call to bypass is made
  • Then I press c and the test passes

This is beyond confusing to me. It appears that the test process in not invoking Manager.login like it should.

I cannot explain this.

Questions

  1. Why is the bypass being called when I invoke Manager.login manually using the --breakpoint option and not when mix test runs?
  2. How can I fix my test?

Marked As Solved

RudManusachi

RudManusachi

Is it possible that when ran without breakpoints the test finishes before the process makes the call. While with breakpoints, by the time you press c, Manage.login/2 makes its way to send the message?

I’d try to synchronize the test and Bypass with something like:

test "logs in user correctly", %{bypass: bypass, credentials: credentials} do
  test_pid = self()

  Bypass.expect_once(bypass, fn conn ->
    IO.inspect(conn.request_path, label: "REQ PATH")
    IO.inspect(conn.method, label: "METHOD")
  
    send(test_pid, :logging_in)
    
    Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
  end)
  
  _manager_pid = start_link_supervised!(ManagerSupervisor)
  Manager.login(credentials, false)

  # this ensures that the test will not exit until the message is received
  # (or until the default `assert_receive` timeout 100ms)
  assert_receive :logging_in
end

Where Next?

Popular in Questions Top

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement