AlejandroHuerta

AlejandroHuerta

ExUnit CaptureIO and c.flush

Hey everyone,

I have test that check if messages have been sent to a process and to make sure I test against the one I want, I clear out the others. Previously I was using lib.flush_receive/0 but that has since been removed. It looks like c.flush/0 is the replacement. One thing that I dislike about c.flush/0 is that it outputs the messages which makes looking at test failures very tedious. I tried combining the call with ExUnit.CaptureIO.capture_io/1 but I’m getting ArgumentErrors, from seemingly c.flush/0 itself?

Here’s the code I’m using:

   try do
      capture_io(&:c.flush/0)
    rescue
      ArgumentError ->
        IO.puts "Error flushing mailbox!"
        IO.inspect __STACKTRACE__
    end

and the output looks like

Error flushing mailbox!
[
{:lists, :keyfind, [:encoding, 1, {:ok, [binary: true, encoding: :unicode]}],
},
{:c, :flush, 0, [file: ‘c.erl’, line: 819]},
{ExUnit.CaptureIO, :do_capture_io, 2,
[file: ‘lib/ex_unit/capture_io.ex’, line: 151]},
{ExUnit.CaptureIO, :do_capture_io, 3,
[file: ‘lib/ex_unit/capture_io.ex’, line: 121]},
{PlexServer.EmpireChannelUtils, :flush_mailbox, 1,
[file: ‘test/channels/empire_channel_utils.exs’, line: 54]},
{PlexServer.EmpireChannelTest, :“test succesful action: spend invade moves”,
1, [file: ‘test/channels/empire_channel_test.exs’, line: 567]},
{ExUnit.Runner, :exec_test, 1, [file: ‘lib/ex_unit/runner.ex’, line: 355]},
{:timer, :tc, 1, [file: ‘timer.erl’, line: 166]},
{ExUnit.Runner, :“-spawn_test_monitor/4-fun-1-”, 4,
[file: ‘lib/ex_unit/runner.ex’, line: 306]}
]

Digging into the c.flush/0 source, it seems that it calls io.getopts/0 and that’s returning a tuple rather than just an array? Why would this work without the capture_io/1?

Not really sure how to proceed. Any ideas?

Marked As Solved

NobbZ

NobbZ

Probably not, look at this remark from the modules documentation:

These functions are intended for interactive use in the Erlang shell only. The module prefix can be omitted.

Also :c.flush/0 shouldn’t return a tuple or a list (I assume thats what you are talking about when saying “array”), it should just return the atom :ok.

If you really want to flush the mailbox, you can implement this really easily:

def flush() do
  receive do
    _ -> flush()
  after
    0 -> :ok
  end
end

You can easily extend it to actually print or return the “flushed” messages if you need to.

Where Next?

Popular in Questions Top

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
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New

We're in Beta

About us Mission Statement