Skip to content

Commit 60bcf1c

Browse files
committed
Print process mailbox on failing assert_receive
1 parent d86ca58 commit 60bcf1c

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

lib/ex_unit/lib/ex_unit/assertions.ex

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,13 +281,30 @@ defmodule ExUnit.Assertions do
281281
unquote(expected) = received -> received
282282
after
283283
unquote(timeout) ->
284-
flunk unquote(message)
284+
flunk(unquote(message) <> mailbox(self))
285285
end
286286
end
287287

288288
{:receive, [{:export_head, true}|meta], args}
289289
end
290290

291+
@max_mailbox_length 10
292+
293+
@doc false
294+
def mailbox(pid) do
295+
{:messages, messages} = Process.info(pid, :messages)
296+
length = length(messages)
297+
mailbox = Enum.take(messages, @max_mailbox_length)
298+
|> Enum.map_join("\n", &inspect/1)
299+
". Process mailbox (#{length}):" <> mailbox_message(length, mailbox)
300+
end
301+
302+
defp mailbox_message(0, _mailbox), do: " []"
303+
defp mailbox_message(length, mailbox) when length > 10 do
304+
"\n" <> mailbox <> "\nShowing only #{@max_mailbox_length} first messages."
305+
end
306+
defp mailbox_message(_length, mailbox), do: "\n" <> mailbox
307+
291308
@doc """
292309
Asserts the `exception` is raised during `function` execution with
293310
the `expected_message`. Returns the rescued exception, fails otherwise.

lib/ex_unit/test/ex_unit/assertions_test.exs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,32 @@ defmodule ExUnit.AssertionsTest do
8686
:hello = assert_received :hello
8787
end
8888

89-
test "assert received when different" do
89+
test "assert received when empty mailbox" do
9090
try do
9191
"This should never be tested" = assert_received :hello
9292
rescue
9393
error in [ExUnit.AssertionError] ->
94-
"No message matching :hello" = error.message
94+
"No message matching :hello. Process mailbox (0): []" = error.message
95+
end
96+
end
97+
98+
test "assert received when different message" do
99+
send self, {:message, :not_expected, :at_all}
100+
try do
101+
"This should never be tested" = assert_receive :hello, 5_000
102+
rescue
103+
error in [ExUnit.AssertionError] ->
104+
"No message matching :hello. Process mailbox (1):\n{:message, :not_expected, :at_all}" = error.message
105+
end
106+
end
107+
108+
test "assert received when different message having more than 10 on mailbox" do
109+
for i <- 1..11, do: send(self, {:message, i})
110+
try do
111+
"This should never be tested" = assert_receive :hello, 5_000
112+
rescue
113+
error in [ExUnit.AssertionError] ->
114+
"No message matching :hello. Process mailbox (11):\n{:message, 1}\n{:message, 2}\n{:message, 3}\n{:message, 4}\n{:message, 5}\n{:message, 6}\n{:message, 7}\n{:message, 8}\n{:message, 9}\n{:message, 10}\nShowing only 10 first messages." = error.message
95115
end
96116
end
97117

0 commit comments

Comments
 (0)