def test_partial_output(): pi = Pipe() proc = Process( runpy(r'print("foo", flush=True); input(); print("bar", flush=True)'), stdout=pi.side_in, ) proc.start() data = pi.side_out.readline() proc.terminate() proc.join() assert data in (b'foo\n', b'foo\r\n')
def test_terminated_output(): # NOTE: POSIX clears the pipe when a process has non-zero return pi = Pipe() proc = Process( runpy(r'print("foo", flush=True); input(); print("bar", flush=True)'), stdout=pi.side_in, ) proc.start() pi.side_in.close( ) # Remove our reference on this end of the pipe, now that the child has one proc.terminate() proc.join() data = pi.side_out.read() assert data == b''
def test_pause_unpause(): import time pi = Pipe() proc = Process( runpy('import time\nwhile True: print(time.time(), flush=True)'), stdout=pi.side_in, ) proc.start() pi.side_in.close( ) # Remove our reference on this end of the pipe, now that the child has one below = [ float(ts.decode('utf-8').rstrip('\r\n')) for ts in iter_for_time(1, pi.side_out) if ts ] proc.pause() time.sleep(1) proc.unpause() above = [ float(ts.decode('utf-8').rstrip('\r\n')) for ts in iter_for_time(1, pi.side_out) if ts ] proc.terminate() # We don't have a non-blocking way to flush, so we're just hoping that 1s is # enough time to get what was stashed. timestamps = below + above # Calculate the mean, assume its in the paused area, and calculate the gap # in the timestamps from that pause. mean = sum(timestamps) / len(timestamps) below = [ts for ts in timestamps if ts < mean] above = [ts for ts in timestamps if ts > mean] pause_begin = max(below) pause_end = min(above) gap = pause_end - pause_begin assert gap > 0.9 # 0.9 for leeway
def test_terminate_process(): proc = Process(runpy('input()')) proc.start() proc.terminate() proc.join() assert proc.return_code != 0