ct:capture_start/get interferes with messages to process under test #6016
Labels
bug
Issue is reported as a bug
help wanted
Issue not worked on by OTP; help wanted from the community
team:PS
Assigned to OTP team PS
Describe the bug
https://erlang.org/doc/man/ct.html#capture_get-1 seems to work by redirecting
writes to stdout to be messages to the current pid. Those messages get queued up,
with the intent to read them back later to get stdout. Unfortunately, this doesn't
work when the test case itself is handling messages.
Here's an example of one situation where this crops up:
At various points, Dialyzer dequeues messages it receives from its worker child processes,
sometimes throwing them away knowing it doesn't need certain results from them.
Sadly, this interacts badly with
ct:capture_start/0
/ct:capture_get/1
and Dialyzer ends up dequeuing (and hence,deleting) some of what was written to stdout. The solution was to run Dialyzer in
a sub-process, so the messages containing stdout go to the parent process's mailbox
and Dialyzer is free to do what it wants with its own messages.
Fundamentally this seems to be an issue with the capture mechanism, since it seems to presume it's
fine to send messages to the test case's pid and they'll never be consumed by the code
under test!
Here was my workaround for capturing Dialyzer's stdout in a test:
In this case, the code that writes to stdout and receives messages is run as a new process, so that the test case which captures the stdout into its mailbox is not also the process which is reading from its mailbox. I suspect in general, you'd want to run the code-under-test in a sub-process to make sure its safe to redirect stdout to the current process's message queue.
To Reproduce
Use
ct:capture_start/0
in a test case, and within that test case, write to stdout, then consume the message from the mailbox. It will now no longer appear in the captured output fromct:capture_get/1
.Expected behavior
I'd expect
ct:capture_get/1
to work even if the test reads messages. The code in the description offers a way to do this by creating a new process.Affected versions
All recent versions I have used.
The text was updated successfully, but these errors were encountered: