Make a fd which will force block on read of expected bytes.
(self)
| 124 | self._test_wait_single(lambda pid: os.wait4(pid, 0)) |
| 125 | |
| 126 | def _interrupted_reads(self): |
| 127 | """Make a fd which will force block on read of expected bytes.""" |
| 128 | rd, wr = os.pipe() |
| 129 | self.addCleanup(os.close, rd) |
| 130 | # wr closed explicitly by parent |
| 131 | |
| 132 | # the payload below are smaller than PIPE_BUF, hence the writes will be |
| 133 | # atomic |
| 134 | data = [b"hello", b"world", b"spam"] |
| 135 | |
| 136 | code = '\n'.join(( |
| 137 | 'import os, sys, time', |
| 138 | '', |
| 139 | 'wr = int(sys.argv[1])', |
| 140 | f'data = {data!r}', |
| 141 | f'sleep_time = {self.sleep_time!r}', |
| 142 | '', |
| 143 | 'for item in data:', |
| 144 | ' # let the parent block on read()', |
| 145 | ' time.sleep(sleep_time)', |
| 146 | ' os.write(wr, item)', |
| 147 | )) |
| 148 | |
| 149 | proc = self.subprocess(code, str(wr), pass_fds=[wr]) |
| 150 | with kill_on_error(proc): |
| 151 | os.close(wr) |
| 152 | for datum in data: |
| 153 | yield rd, datum |
| 154 | self.assertEqual(proc.wait(), 0) |
| 155 | |
| 156 | def test_read(self): |
| 157 | for fd, expected in self._interrupted_reads(): |
no test coverage detected