| 1 | n/a | """ |
|---|
| 2 | n/a | This test suite exercises some system calls subject to interruption with EINTR, |
|---|
| 3 | n/a | to check that it is actually handled transparently. |
|---|
| 4 | n/a | It is intended to be run by the main test suite within a child process, to |
|---|
| 5 | n/a | ensure there is no background thread running (so that signals are delivered to |
|---|
| 6 | n/a | the correct thread). |
|---|
| 7 | n/a | Signals are generated in-process using setitimer(ITIMER_REAL), which allows |
|---|
| 8 | n/a | sub-second periodicity (contrarily to signal()). |
|---|
| 9 | n/a | """ |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | import contextlib |
|---|
| 12 | n/a | import faulthandler |
|---|
| 13 | n/a | import os |
|---|
| 14 | n/a | import select |
|---|
| 15 | n/a | import signal |
|---|
| 16 | n/a | import socket |
|---|
| 17 | n/a | import subprocess |
|---|
| 18 | n/a | import sys |
|---|
| 19 | n/a | import time |
|---|
| 20 | n/a | import unittest |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | from test import support |
|---|
| 23 | n/a | android_not_root = support.android_not_root |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | @contextlib.contextmanager |
|---|
| 26 | n/a | def kill_on_error(proc): |
|---|
| 27 | n/a | """Context manager killing the subprocess if a Python exception is raised.""" |
|---|
| 28 | n/a | with proc: |
|---|
| 29 | n/a | try: |
|---|
| 30 | n/a | yield proc |
|---|
| 31 | n/a | except: |
|---|
| 32 | n/a | proc.kill() |
|---|
| 33 | n/a | raise |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()") |
|---|
| 37 | n/a | class EINTRBaseTest(unittest.TestCase): |
|---|
| 38 | n/a | """ Base class for EINTR tests. """ |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | # delay for initial signal delivery |
|---|
| 41 | n/a | signal_delay = 0.1 |
|---|
| 42 | n/a | # signal delivery periodicity |
|---|
| 43 | n/a | signal_period = 0.1 |
|---|
| 44 | n/a | # default sleep time for tests - should obviously have: |
|---|
| 45 | n/a | # sleep_time > signal_period |
|---|
| 46 | n/a | sleep_time = 0.2 |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | @classmethod |
|---|
| 49 | n/a | def setUpClass(cls): |
|---|
| 50 | n/a | cls.orig_handler = signal.signal(signal.SIGALRM, lambda *args: None) |
|---|
| 51 | n/a | signal.setitimer(signal.ITIMER_REAL, cls.signal_delay, |
|---|
| 52 | n/a | cls.signal_period) |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | # Issue #25277: Use faulthandler to try to debug a hang on FreeBSD |
|---|
| 55 | n/a | if hasattr(faulthandler, 'dump_traceback_later'): |
|---|
| 56 | n/a | faulthandler.dump_traceback_later(10 * 60, exit=True) |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | @classmethod |
|---|
| 59 | n/a | def stop_alarm(cls): |
|---|
| 60 | n/a | signal.setitimer(signal.ITIMER_REAL, 0, 0) |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | @classmethod |
|---|
| 63 | n/a | def tearDownClass(cls): |
|---|
| 64 | n/a | cls.stop_alarm() |
|---|
| 65 | n/a | signal.signal(signal.SIGALRM, cls.orig_handler) |
|---|
| 66 | n/a | if hasattr(faulthandler, 'cancel_dump_traceback_later'): |
|---|
| 67 | n/a | faulthandler.cancel_dump_traceback_later() |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | def subprocess(self, *args, **kw): |
|---|
| 70 | n/a | cmd_args = (sys.executable, '-c') + args |
|---|
| 71 | n/a | return subprocess.Popen(cmd_args, **kw) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()") |
|---|
| 75 | n/a | class OSEINTRTest(EINTRBaseTest): |
|---|
| 76 | n/a | """ EINTR tests for the os module. """ |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | def new_sleep_process(self): |
|---|
| 79 | n/a | code = 'import time; time.sleep(%r)' % self.sleep_time |
|---|
| 80 | n/a | return self.subprocess(code) |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | def _test_wait_multiple(self, wait_func): |
|---|
| 83 | n/a | num = 3 |
|---|
| 84 | n/a | processes = [self.new_sleep_process() for _ in range(num)] |
|---|
| 85 | n/a | for _ in range(num): |
|---|
| 86 | n/a | wait_func() |
|---|
| 87 | n/a | # Call the Popen method to avoid a ResourceWarning |
|---|
| 88 | n/a | for proc in processes: |
|---|
| 89 | n/a | proc.wait() |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | def test_wait(self): |
|---|
| 92 | n/a | self._test_wait_multiple(os.wait) |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | @unittest.skipUnless(hasattr(os, 'wait3'), 'requires wait3()') |
|---|
| 95 | n/a | def test_wait3(self): |
|---|
| 96 | n/a | self._test_wait_multiple(lambda: os.wait3(0)) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | def _test_wait_single(self, wait_func): |
|---|
| 99 | n/a | proc = self.new_sleep_process() |
|---|
| 100 | n/a | wait_func(proc.pid) |
|---|
| 101 | n/a | # Call the Popen method to avoid a ResourceWarning |
|---|
| 102 | n/a | proc.wait() |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | def test_waitpid(self): |
|---|
| 105 | n/a | self._test_wait_single(lambda pid: os.waitpid(pid, 0)) |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | @unittest.skipUnless(hasattr(os, 'wait4'), 'requires wait4()') |
|---|
| 108 | n/a | def test_wait4(self): |
|---|
| 109 | n/a | self._test_wait_single(lambda pid: os.wait4(pid, 0)) |
|---|
| 110 | n/a | |
|---|
| 111 | n/a | def test_read(self): |
|---|
| 112 | n/a | rd, wr = os.pipe() |
|---|
| 113 | n/a | self.addCleanup(os.close, rd) |
|---|
| 114 | n/a | # wr closed explicitly by parent |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | # the payload below are smaller than PIPE_BUF, hence the writes will be |
|---|
| 117 | n/a | # atomic |
|---|
| 118 | n/a | datas = [b"hello", b"world", b"spam"] |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | code = '\n'.join(( |
|---|
| 121 | n/a | 'import os, sys, time', |
|---|
| 122 | n/a | '', |
|---|
| 123 | n/a | 'wr = int(sys.argv[1])', |
|---|
| 124 | n/a | 'datas = %r' % datas, |
|---|
| 125 | n/a | 'sleep_time = %r' % self.sleep_time, |
|---|
| 126 | n/a | '', |
|---|
| 127 | n/a | 'for data in datas:', |
|---|
| 128 | n/a | ' # let the parent block on read()', |
|---|
| 129 | n/a | ' time.sleep(sleep_time)', |
|---|
| 130 | n/a | ' os.write(wr, data)', |
|---|
| 131 | n/a | )) |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | proc = self.subprocess(code, str(wr), pass_fds=[wr]) |
|---|
| 134 | n/a | with kill_on_error(proc): |
|---|
| 135 | n/a | os.close(wr) |
|---|
| 136 | n/a | for data in datas: |
|---|
| 137 | n/a | self.assertEqual(data, os.read(rd, len(data))) |
|---|
| 138 | n/a | self.assertEqual(proc.wait(), 0) |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | def test_write(self): |
|---|
| 141 | n/a | rd, wr = os.pipe() |
|---|
| 142 | n/a | self.addCleanup(os.close, wr) |
|---|
| 143 | n/a | # rd closed explicitly by parent |
|---|
| 144 | n/a | |
|---|
| 145 | n/a | # we must write enough data for the write() to block |
|---|
| 146 | n/a | data = b"x" * support.PIPE_MAX_SIZE |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | code = '\n'.join(( |
|---|
| 149 | n/a | 'import io, os, sys, time', |
|---|
| 150 | n/a | '', |
|---|
| 151 | n/a | 'rd = int(sys.argv[1])', |
|---|
| 152 | n/a | 'sleep_time = %r' % self.sleep_time, |
|---|
| 153 | n/a | 'data = b"x" * %s' % support.PIPE_MAX_SIZE, |
|---|
| 154 | n/a | 'data_len = len(data)', |
|---|
| 155 | n/a | '', |
|---|
| 156 | n/a | '# let the parent block on write()', |
|---|
| 157 | n/a | 'time.sleep(sleep_time)', |
|---|
| 158 | n/a | '', |
|---|
| 159 | n/a | 'read_data = io.BytesIO()', |
|---|
| 160 | n/a | 'while len(read_data.getvalue()) < data_len:', |
|---|
| 161 | n/a | ' chunk = os.read(rd, 2 * data_len)', |
|---|
| 162 | n/a | ' read_data.write(chunk)', |
|---|
| 163 | n/a | '', |
|---|
| 164 | n/a | 'value = read_data.getvalue()', |
|---|
| 165 | n/a | 'if value != data:', |
|---|
| 166 | n/a | ' raise Exception("read error: %s vs %s bytes"', |
|---|
| 167 | n/a | ' % (len(value), data_len))', |
|---|
| 168 | n/a | )) |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | proc = self.subprocess(code, str(rd), pass_fds=[rd]) |
|---|
| 171 | n/a | with kill_on_error(proc): |
|---|
| 172 | n/a | os.close(rd) |
|---|
| 173 | n/a | written = 0 |
|---|
| 174 | n/a | while written < len(data): |
|---|
| 175 | n/a | written += os.write(wr, memoryview(data)[written:]) |
|---|
| 176 | n/a | self.assertEqual(proc.wait(), 0) |
|---|
| 177 | n/a | |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()") |
|---|
| 180 | n/a | class SocketEINTRTest(EINTRBaseTest): |
|---|
| 181 | n/a | """ EINTR tests for the socket module. """ |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | @unittest.skipUnless(hasattr(socket, 'socketpair'), 'needs socketpair()') |
|---|
| 184 | n/a | def _test_recv(self, recv_func): |
|---|
| 185 | n/a | rd, wr = socket.socketpair() |
|---|
| 186 | n/a | self.addCleanup(rd.close) |
|---|
| 187 | n/a | # wr closed explicitly by parent |
|---|
| 188 | n/a | |
|---|
| 189 | n/a | # single-byte payload guard us against partial recv |
|---|
| 190 | n/a | datas = [b"x", b"y", b"z"] |
|---|
| 191 | n/a | |
|---|
| 192 | n/a | code = '\n'.join(( |
|---|
| 193 | n/a | 'import os, socket, sys, time', |
|---|
| 194 | n/a | '', |
|---|
| 195 | n/a | 'fd = int(sys.argv[1])', |
|---|
| 196 | n/a | 'family = %s' % int(wr.family), |
|---|
| 197 | n/a | 'sock_type = %s' % int(wr.type), |
|---|
| 198 | n/a | 'datas = %r' % datas, |
|---|
| 199 | n/a | 'sleep_time = %r' % self.sleep_time, |
|---|
| 200 | n/a | '', |
|---|
| 201 | n/a | 'wr = socket.fromfd(fd, family, sock_type)', |
|---|
| 202 | n/a | 'os.close(fd)', |
|---|
| 203 | n/a | '', |
|---|
| 204 | n/a | 'with wr:', |
|---|
| 205 | n/a | ' for data in datas:', |
|---|
| 206 | n/a | ' # let the parent block on recv()', |
|---|
| 207 | n/a | ' time.sleep(sleep_time)', |
|---|
| 208 | n/a | ' wr.sendall(data)', |
|---|
| 209 | n/a | )) |
|---|
| 210 | n/a | |
|---|
| 211 | n/a | fd = wr.fileno() |
|---|
| 212 | n/a | proc = self.subprocess(code, str(fd), pass_fds=[fd]) |
|---|
| 213 | n/a | with kill_on_error(proc): |
|---|
| 214 | n/a | wr.close() |
|---|
| 215 | n/a | for data in datas: |
|---|
| 216 | n/a | self.assertEqual(data, recv_func(rd, len(data))) |
|---|
| 217 | n/a | self.assertEqual(proc.wait(), 0) |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | def test_recv(self): |
|---|
| 220 | n/a | self._test_recv(socket.socket.recv) |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | @unittest.skipUnless(hasattr(socket.socket, 'recvmsg'), 'needs recvmsg()') |
|---|
| 223 | n/a | def test_recvmsg(self): |
|---|
| 224 | n/a | self._test_recv(lambda sock, data: sock.recvmsg(data)[0]) |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | def _test_send(self, send_func): |
|---|
| 227 | n/a | rd, wr = socket.socketpair() |
|---|
| 228 | n/a | self.addCleanup(wr.close) |
|---|
| 229 | n/a | # rd closed explicitly by parent |
|---|
| 230 | n/a | |
|---|
| 231 | n/a | # we must send enough data for the send() to block |
|---|
| 232 | n/a | data = b"xyz" * (support.SOCK_MAX_SIZE // 3) |
|---|
| 233 | n/a | |
|---|
| 234 | n/a | code = '\n'.join(( |
|---|
| 235 | n/a | 'import os, socket, sys, time', |
|---|
| 236 | n/a | '', |
|---|
| 237 | n/a | 'fd = int(sys.argv[1])', |
|---|
| 238 | n/a | 'family = %s' % int(rd.family), |
|---|
| 239 | n/a | 'sock_type = %s' % int(rd.type), |
|---|
| 240 | n/a | 'sleep_time = %r' % self.sleep_time, |
|---|
| 241 | n/a | 'data = b"xyz" * %s' % (support.SOCK_MAX_SIZE // 3), |
|---|
| 242 | n/a | 'data_len = len(data)', |
|---|
| 243 | n/a | '', |
|---|
| 244 | n/a | 'rd = socket.fromfd(fd, family, sock_type)', |
|---|
| 245 | n/a | 'os.close(fd)', |
|---|
| 246 | n/a | '', |
|---|
| 247 | n/a | 'with rd:', |
|---|
| 248 | n/a | ' # let the parent block on send()', |
|---|
| 249 | n/a | ' time.sleep(sleep_time)', |
|---|
| 250 | n/a | '', |
|---|
| 251 | n/a | ' received_data = bytearray(data_len)', |
|---|
| 252 | n/a | ' n = 0', |
|---|
| 253 | n/a | ' while n < data_len:', |
|---|
| 254 | n/a | ' n += rd.recv_into(memoryview(received_data)[n:])', |
|---|
| 255 | n/a | '', |
|---|
| 256 | n/a | 'if received_data != data:', |
|---|
| 257 | n/a | ' raise Exception("recv error: %s vs %s bytes"', |
|---|
| 258 | n/a | ' % (len(received_data), data_len))', |
|---|
| 259 | n/a | )) |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | fd = rd.fileno() |
|---|
| 262 | n/a | proc = self.subprocess(code, str(fd), pass_fds=[fd]) |
|---|
| 263 | n/a | with kill_on_error(proc): |
|---|
| 264 | n/a | rd.close() |
|---|
| 265 | n/a | written = 0 |
|---|
| 266 | n/a | while written < len(data): |
|---|
| 267 | n/a | sent = send_func(wr, memoryview(data)[written:]) |
|---|
| 268 | n/a | # sendall() returns None |
|---|
| 269 | n/a | written += len(data) if sent is None else sent |
|---|
| 270 | n/a | self.assertEqual(proc.wait(), 0) |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | def test_send(self): |
|---|
| 273 | n/a | self._test_send(socket.socket.send) |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | def test_sendall(self): |
|---|
| 276 | n/a | self._test_send(socket.socket.sendall) |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | @unittest.skipUnless(hasattr(socket.socket, 'sendmsg'), 'needs sendmsg()') |
|---|
| 279 | n/a | def test_sendmsg(self): |
|---|
| 280 | n/a | self._test_send(lambda sock, data: sock.sendmsg([data])) |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | def test_accept(self): |
|---|
| 283 | n/a | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|---|
| 284 | n/a | self.addCleanup(sock.close) |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | sock.bind((support.HOST, 0)) |
|---|
| 287 | n/a | port = sock.getsockname()[1] |
|---|
| 288 | n/a | sock.listen() |
|---|
| 289 | n/a | |
|---|
| 290 | n/a | code = '\n'.join(( |
|---|
| 291 | n/a | 'import socket, time', |
|---|
| 292 | n/a | '', |
|---|
| 293 | n/a | 'host = %r' % support.HOST, |
|---|
| 294 | n/a | 'port = %s' % port, |
|---|
| 295 | n/a | 'sleep_time = %r' % self.sleep_time, |
|---|
| 296 | n/a | '', |
|---|
| 297 | n/a | '# let parent block on accept()', |
|---|
| 298 | n/a | 'time.sleep(sleep_time)', |
|---|
| 299 | n/a | 'with socket.create_connection((host, port)):', |
|---|
| 300 | n/a | ' time.sleep(sleep_time)', |
|---|
| 301 | n/a | )) |
|---|
| 302 | n/a | |
|---|
| 303 | n/a | proc = self.subprocess(code) |
|---|
| 304 | n/a | with kill_on_error(proc): |
|---|
| 305 | n/a | client_sock, _ = sock.accept() |
|---|
| 306 | n/a | client_sock.close() |
|---|
| 307 | n/a | self.assertEqual(proc.wait(), 0) |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | # Issue #25122: There is a race condition in the FreeBSD kernel on |
|---|
| 310 | n/a | # handling signals in the FIFO device. Skip the test until the bug is |
|---|
| 311 | n/a | # fixed in the kernel. |
|---|
| 312 | n/a | # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162 |
|---|
| 313 | n/a | @support.requires_freebsd_version(10, 3) |
|---|
| 314 | n/a | @unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()') |
|---|
| 315 | n/a | @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") |
|---|
| 316 | n/a | def _test_open(self, do_open_close_reader, do_open_close_writer): |
|---|
| 317 | n/a | filename = support.TESTFN |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | # Use a fifo: until the child opens it for reading, the parent will |
|---|
| 320 | n/a | # block when trying to open it for writing. |
|---|
| 321 | n/a | support.unlink(filename) |
|---|
| 322 | n/a | os.mkfifo(filename) |
|---|
| 323 | n/a | self.addCleanup(support.unlink, filename) |
|---|
| 324 | n/a | |
|---|
| 325 | n/a | code = '\n'.join(( |
|---|
| 326 | n/a | 'import os, time', |
|---|
| 327 | n/a | '', |
|---|
| 328 | n/a | 'path = %a' % filename, |
|---|
| 329 | n/a | 'sleep_time = %r' % self.sleep_time, |
|---|
| 330 | n/a | '', |
|---|
| 331 | n/a | '# let the parent block', |
|---|
| 332 | n/a | 'time.sleep(sleep_time)', |
|---|
| 333 | n/a | '', |
|---|
| 334 | n/a | do_open_close_reader, |
|---|
| 335 | n/a | )) |
|---|
| 336 | n/a | |
|---|
| 337 | n/a | proc = self.subprocess(code) |
|---|
| 338 | n/a | with kill_on_error(proc): |
|---|
| 339 | n/a | do_open_close_writer(filename) |
|---|
| 340 | n/a | self.assertEqual(proc.wait(), 0) |
|---|
| 341 | n/a | |
|---|
| 342 | n/a | def python_open(self, path): |
|---|
| 343 | n/a | fp = open(path, 'w') |
|---|
| 344 | n/a | fp.close() |
|---|
| 345 | n/a | |
|---|
| 346 | n/a | def test_open(self): |
|---|
| 347 | n/a | self._test_open("fp = open(path, 'r')\nfp.close()", |
|---|
| 348 | n/a | self.python_open) |
|---|
| 349 | n/a | |
|---|
| 350 | n/a | @unittest.skipIf(sys.platform == 'darwin', "hangs under OS X; see issue #25234") |
|---|
| 351 | n/a | def os_open(self, path): |
|---|
| 352 | n/a | fd = os.open(path, os.O_WRONLY) |
|---|
| 353 | n/a | os.close(fd) |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | @unittest.skipIf(sys.platform == "darwin", "hangs under OS X; see issue #25234") |
|---|
| 356 | n/a | def test_os_open(self): |
|---|
| 357 | n/a | self._test_open("fd = os.open(path, os.O_RDONLY)\nos.close(fd)", |
|---|
| 358 | n/a | self.os_open) |
|---|
| 359 | n/a | |
|---|
| 360 | n/a | |
|---|
| 361 | n/a | @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()") |
|---|
| 362 | n/a | class TimeEINTRTest(EINTRBaseTest): |
|---|
| 363 | n/a | """ EINTR tests for the time module. """ |
|---|
| 364 | n/a | |
|---|
| 365 | n/a | def test_sleep(self): |
|---|
| 366 | n/a | t0 = time.monotonic() |
|---|
| 367 | n/a | time.sleep(self.sleep_time) |
|---|
| 368 | n/a | self.stop_alarm() |
|---|
| 369 | n/a | dt = time.monotonic() - t0 |
|---|
| 370 | n/a | self.assertGreaterEqual(dt, self.sleep_time) |
|---|
| 371 | n/a | |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()") |
|---|
| 374 | n/a | class SignalEINTRTest(EINTRBaseTest): |
|---|
| 375 | n/a | """ EINTR tests for the signal module. """ |
|---|
| 376 | n/a | |
|---|
| 377 | n/a | @unittest.skipUnless(hasattr(signal, 'sigtimedwait'), |
|---|
| 378 | n/a | 'need signal.sigtimedwait()') |
|---|
| 379 | n/a | def test_sigtimedwait(self): |
|---|
| 380 | n/a | t0 = time.monotonic() |
|---|
| 381 | n/a | signal.sigtimedwait([signal.SIGUSR1], self.sleep_time) |
|---|
| 382 | n/a | dt = time.monotonic() - t0 |
|---|
| 383 | n/a | self.assertGreaterEqual(dt, self.sleep_time) |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | @unittest.skipUnless(hasattr(signal, 'sigwaitinfo'), |
|---|
| 386 | n/a | 'need signal.sigwaitinfo()') |
|---|
| 387 | n/a | def test_sigwaitinfo(self): |
|---|
| 388 | n/a | # Issue #25277, #25868: give a few milliseconds to the parent process |
|---|
| 389 | n/a | # between os.write() and signal.sigwaitinfo() to works around a race |
|---|
| 390 | n/a | # condition |
|---|
| 391 | n/a | self.sleep_time = 0.100 |
|---|
| 392 | n/a | |
|---|
| 393 | n/a | signum = signal.SIGUSR1 |
|---|
| 394 | n/a | pid = os.getpid() |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | old_handler = signal.signal(signum, lambda *args: None) |
|---|
| 397 | n/a | self.addCleanup(signal.signal, signum, old_handler) |
|---|
| 398 | n/a | |
|---|
| 399 | n/a | rpipe, wpipe = os.pipe() |
|---|
| 400 | n/a | |
|---|
| 401 | n/a | code = '\n'.join(( |
|---|
| 402 | n/a | 'import os, time', |
|---|
| 403 | n/a | 'pid = %s' % os.getpid(), |
|---|
| 404 | n/a | 'signum = %s' % int(signum), |
|---|
| 405 | n/a | 'sleep_time = %r' % self.sleep_time, |
|---|
| 406 | n/a | 'rpipe = %r' % rpipe, |
|---|
| 407 | n/a | 'os.read(rpipe, 1)', |
|---|
| 408 | n/a | 'os.close(rpipe)', |
|---|
| 409 | n/a | 'time.sleep(sleep_time)', |
|---|
| 410 | n/a | 'os.kill(pid, signum)', |
|---|
| 411 | n/a | )) |
|---|
| 412 | n/a | |
|---|
| 413 | n/a | t0 = time.monotonic() |
|---|
| 414 | n/a | proc = self.subprocess(code, pass_fds=(rpipe,)) |
|---|
| 415 | n/a | os.close(rpipe) |
|---|
| 416 | n/a | with kill_on_error(proc): |
|---|
| 417 | n/a | # sync child-parent |
|---|
| 418 | n/a | os.write(wpipe, b'x') |
|---|
| 419 | n/a | os.close(wpipe) |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | # parent |
|---|
| 422 | n/a | signal.sigwaitinfo([signum]) |
|---|
| 423 | n/a | dt = time.monotonic() - t0 |
|---|
| 424 | n/a | self.assertEqual(proc.wait(), 0) |
|---|
| 425 | n/a | |
|---|
| 426 | n/a | self.assertGreaterEqual(dt, self.sleep_time) |
|---|
| 427 | n/a | |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | @unittest.skipUnless(hasattr(signal, "setitimer"), "requires setitimer()") |
|---|
| 430 | n/a | class SelectEINTRTest(EINTRBaseTest): |
|---|
| 431 | n/a | """ EINTR tests for the select module. """ |
|---|
| 432 | n/a | |
|---|
| 433 | n/a | def test_select(self): |
|---|
| 434 | n/a | t0 = time.monotonic() |
|---|
| 435 | n/a | select.select([], [], [], self.sleep_time) |
|---|
| 436 | n/a | dt = time.monotonic() - t0 |
|---|
| 437 | n/a | self.stop_alarm() |
|---|
| 438 | n/a | self.assertGreaterEqual(dt, self.sleep_time) |
|---|
| 439 | n/a | |
|---|
| 440 | n/a | @unittest.skipUnless(hasattr(select, 'poll'), 'need select.poll') |
|---|
| 441 | n/a | def test_poll(self): |
|---|
| 442 | n/a | poller = select.poll() |
|---|
| 443 | n/a | |
|---|
| 444 | n/a | t0 = time.monotonic() |
|---|
| 445 | n/a | poller.poll(self.sleep_time * 1e3) |
|---|
| 446 | n/a | dt = time.monotonic() - t0 |
|---|
| 447 | n/a | self.stop_alarm() |
|---|
| 448 | n/a | self.assertGreaterEqual(dt, self.sleep_time) |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | @unittest.skipUnless(hasattr(select, 'epoll'), 'need select.epoll') |
|---|
| 451 | n/a | def test_epoll(self): |
|---|
| 452 | n/a | poller = select.epoll() |
|---|
| 453 | n/a | self.addCleanup(poller.close) |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | t0 = time.monotonic() |
|---|
| 456 | n/a | poller.poll(self.sleep_time) |
|---|
| 457 | n/a | dt = time.monotonic() - t0 |
|---|
| 458 | n/a | self.stop_alarm() |
|---|
| 459 | n/a | self.assertGreaterEqual(dt, self.sleep_time) |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | @unittest.skipUnless(hasattr(select, 'kqueue'), 'need select.kqueue') |
|---|
| 462 | n/a | def test_kqueue(self): |
|---|
| 463 | n/a | kqueue = select.kqueue() |
|---|
| 464 | n/a | self.addCleanup(kqueue.close) |
|---|
| 465 | n/a | |
|---|
| 466 | n/a | t0 = time.monotonic() |
|---|
| 467 | n/a | kqueue.control(None, 1, self.sleep_time) |
|---|
| 468 | n/a | dt = time.monotonic() - t0 |
|---|
| 469 | n/a | self.stop_alarm() |
|---|
| 470 | n/a | self.assertGreaterEqual(dt, self.sleep_time) |
|---|
| 471 | n/a | |
|---|
| 472 | n/a | @unittest.skipUnless(hasattr(select, 'devpoll'), 'need select.devpoll') |
|---|
| 473 | n/a | def test_devpoll(self): |
|---|
| 474 | n/a | poller = select.devpoll() |
|---|
| 475 | n/a | self.addCleanup(poller.close) |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | t0 = time.monotonic() |
|---|
| 478 | n/a | poller.poll(self.sleep_time * 1e3) |
|---|
| 479 | n/a | dt = time.monotonic() - t0 |
|---|
| 480 | n/a | self.stop_alarm() |
|---|
| 481 | n/a | self.assertGreaterEqual(dt, self.sleep_time) |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | |
|---|
| 484 | n/a | def test_main(): |
|---|
| 485 | n/a | support.run_unittest( |
|---|
| 486 | n/a | OSEINTRTest, |
|---|
| 487 | n/a | SocketEINTRTest, |
|---|
| 488 | n/a | TimeEINTRTest, |
|---|
| 489 | n/a | SignalEINTRTest, |
|---|
| 490 | n/a | SelectEINTRTest) |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | |
|---|
| 493 | n/a | if __name__ == "__main__": |
|---|
| 494 | n/a | test_main() |
|---|