[Proporsal] Add argument "count times" to assert_receive

45 views
Skip to first unread message

Anastasiya Dyachenko

unread,
Dec 6, 2018, 8:25:40 AM12/6/18
to elixir-lang-core
Hi,
In tests often face with need to write code like
```
assert_receive {"msg", _}
assert_receive {"msg", _}
assert_receive {"msg", _}
```
where same messages or same matching messages expected to receive multiple times.
It would be match better to have ability to set number of times message must be received, like

```
assert_receive {"msg", _}, 5_000, "message", 3
```
or
```
assert_receive {"msg", _}, times: 3, timeout: 5_000, message: "message"
```

Thanks

Sven Gehring

unread,
Dec 6, 2018, 8:31:57 AM12/6/18
to elixir-l...@googlegroups.com
Hi,

Assuming you want to receive the same message, wouldn't this only be slightly shorter than using
```
for _ <- 1..3, do: assert_receive({"msg", _})
```

- Sven

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/1ab2968a-9a6c-444f-b9e2-ab164c9673ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anastasiya Dyachenko

unread,
Dec 6, 2018, 10:24:55 AM12/6/18
to elixir-lang-core
Looks nice, but if there will be an error, then line number in error message will say nothing. 
But if it would be native function, then it might have special error message, like
"Message ... received only 2 times, expected 3"

четверг, 6 декабря 2018 г., 16:31:57 UTC+3 пользователь Sven Gehring написал:

Fernando Tapia Rico

unread,
Dec 6, 2018, 11:46:27 AM12/6/18
to elixir-lang-core
I think you could use the "failure_message" argument for that:

for times <- 0..2, do: assert_receive({"msg", _}, 100, "message received only #{times}, expected 3")

Anastasiya Dyachenko

unread,
Dec 6, 2018, 12:32:46 PM12/6/18
to elixir-lang-core
Then in failure message would be no output from process mailbox which in future will show diff with expected message https://github.com/elixir-lang/elixir/pull/8440

четверг, 6 декабря 2018 г., 19:46:27 UTC+3 пользователь Fernando Tapia Rico написал:
Reply all
Reply to author
Forward
0 new messages