|
1 | 1 | import fcntl |
2 | 2 | import functools |
3 | 3 | import operator |
| 4 | +import sys |
4 | 5 |
|
5 | 6 | import mitogen.core |
6 | 7 | import mitogen.parent |
@@ -210,3 +211,35 @@ def test_eof_too_early(self): |
210 | 211 | finally: |
211 | 212 | proc.stdout.close() |
212 | 213 | proc.stderr.close() |
| 214 | + |
| 215 | + def test_timeout_error(self): |
| 216 | + """ |
| 217 | + The boot command should write an ECO marker to stdout, read the |
| 218 | + preamble from stdin, then execute it. |
| 219 | +
|
| 220 | + This test attaches closes stdin to create a specific failure |
| 221 | + 1. Fork child tries to read from STDIN, but fails as it is closed |
| 222 | + 2. Fork child raises TimeoutError |
| 223 | + 3. Fork child's file descriptors (write pipes) are closed by the OS |
| 224 | + 4. Fork parent does `dup(<read pipe>, <stdin>)` and `exec(<python>)` |
| 225 | + 5. Python reads `b''` (i.e. EOF) from stdin (a closed pipe) |
| 226 | + 6. Python runs `''` (a valid script) and exits with success |
| 227 | + """ |
| 228 | + |
| 229 | + proc = testlib.subprocess.Popen( |
| 230 | + args=self.args, |
| 231 | + stdout=testlib.subprocess.PIPE, |
| 232 | + stderr=testlib.subprocess.PIPE, |
| 233 | + preexec_fn=sys.stdin.close, |
| 234 | + ) |
| 235 | + try: |
| 236 | + stdout, stderr = proc.communicate(timeout=12) |
| 237 | + except testlib.subprocess.TimeoutExpired: |
| 238 | + proc.kill() |
| 239 | + self.fail("Timeout situation was not recognized") |
| 240 | + self.assertEqual(0, proc.returncode) |
| 241 | + self.assertEqual(stdout, mitogen.parent.BootstrapProtocol.EC0_MARKER + b("\n")) |
| 242 | + self.assertIn( |
| 243 | + b("TimeoutError"), |
| 244 | + stderr, |
| 245 | + ) |
0 commit comments