'A common way to cleanup and fail with useful debug output.
Kills the process if it is still running, collects remaining output
and fails the test with an error message including the output.
Args:
why: Text to go after "Error from IO process" in the message.
stdout, stderr: standard output and error from the process so
far to include in the error message.
communicate: bool, when True we call communicate() on the process
after killing it to gather additional output.'

def fail_with_process_info(self, why, stdout='', stderr='', communicate=True):

    if (self._process.poll() is None):
        time.sleep(0.1)
        try:
            self._process.terminate()
        except OSError:
            pass
    if communicate:
        (stdout_end, stderr_end) = self._process.communicate()
        stdout += stdout_end
        stderr += stderr_end
    self.fail(('Error    from    IO    process    %s:\nSTDOUT:\n%sSTDERR:\n%s\n' % (why, stdout.decode(), stderr.decode())))
