Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 1 | # Test case for the os.poll() function |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 2 | |
Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 3 | import os |
Christian Heimes | 0688897 | 2013-08-20 22:09:41 +0200 | [diff] [blame] | 4 | import subprocess |
Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 5 | import random |
| 6 | import select |
Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 7 | try: |
| 8 | import threading |
| 9 | except ImportError: |
| 10 | threading = None |
| 11 | import time |
| 12 | import unittest |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 13 | from test.support import TESTFN, run_unittest, reap_threads, cpython_only |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 14 | |
| 15 | try: |
| 16 | select.poll |
| 17 | except AttributeError: |
Brett Cannon | cd8efa3 | 2012-11-14 15:16:53 -0500 | [diff] [blame] | 18 | raise unittest.SkipTest("select.poll not defined") |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 19 | |
| 20 | |
| 21 | def find_ready_matching(ready, flag): |
| 22 | match = [] |
| 23 | for fd, mode in ready: |
| 24 | if mode & flag: |
| 25 | match.append(fd) |
| 26 | return match |
| 27 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 28 | class PollTests(unittest.TestCase): |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 29 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 30 | def test_poll1(self): |
| 31 | # Basic functional test of poll object |
| 32 | # Create a bunch of pipe and test that poll works with them. |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 33 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 34 | p = select.poll() |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 35 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 36 | NUM_PIPES = 12 |
Guido van Rossum | bdab7d3 | 2007-07-11 20:43:16 +0000 | [diff] [blame] | 37 | MSG = b" This is a test." |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 38 | MSG_LEN = len(MSG) |
| 39 | readers = [] |
| 40 | writers = [] |
| 41 | r2w = {} |
| 42 | w2r = {} |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 43 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 44 | for i in range(NUM_PIPES): |
| 45 | rd, wr = os.pipe() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 46 | p.register(rd) |
| 47 | p.modify(rd, select.POLLIN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 48 | p.register(wr, select.POLLOUT) |
| 49 | readers.append(rd) |
| 50 | writers.append(wr) |
| 51 | r2w[rd] = wr |
| 52 | w2r[wr] = rd |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 54 | bufs = [] |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 55 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | while writers: |
| 57 | ready = p.poll() |
| 58 | ready_writers = find_ready_matching(ready, select.POLLOUT) |
| 59 | if not ready_writers: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 60 | raise RuntimeError("no pipes ready for writing") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 61 | wr = random.choice(ready_writers) |
| 62 | os.write(wr, MSG) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 63 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 64 | ready = p.poll() |
| 65 | ready_readers = find_ready_matching(ready, select.POLLIN) |
| 66 | if not ready_readers: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 67 | raise RuntimeError("no pipes ready for reading") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 68 | rd = random.choice(ready_readers) |
| 69 | buf = os.read(rd, MSG_LEN) |
| 70 | self.assertEqual(len(buf), MSG_LEN) |
| 71 | bufs.append(buf) |
| 72 | os.close(r2w[rd]) ; os.close( rd ) |
| 73 | p.unregister( r2w[rd] ) |
| 74 | p.unregister( rd ) |
| 75 | writers.remove(r2w[rd]) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 76 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 77 | self.assertEqual(bufs, [MSG] * NUM_PIPES) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 78 | |
Richard Oudkerk | 53dff0c | 2012-12-09 16:05:20 +0000 | [diff] [blame] | 79 | def test_poll_unit_tests(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 80 | # returns NVAL for invalid file descriptor |
Richard Oudkerk | 53dff0c | 2012-12-09 16:05:20 +0000 | [diff] [blame] | 81 | FD, w = os.pipe() |
| 82 | os.close(FD) |
| 83 | os.close(w) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 84 | p = select.poll() |
| 85 | p.register(FD) |
| 86 | r = p.poll() |
| 87 | self.assertEqual(r[0], (FD, select.POLLNVAL)) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 88 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 89 | f = open(TESTFN, 'w') |
| 90 | fd = f.fileno() |
| 91 | p = select.poll() |
| 92 | p.register(f) |
| 93 | r = p.poll() |
| 94 | self.assertEqual(r[0][0], fd) |
| 95 | f.close() |
| 96 | r = p.poll() |
| 97 | self.assertEqual(r[0], (fd, select.POLLNVAL)) |
| 98 | os.unlink(TESTFN) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 99 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 100 | # type error for invalid arguments |
| 101 | p = select.poll() |
| 102 | self.assertRaises(TypeError, p.register, p) |
| 103 | self.assertRaises(TypeError, p.unregister, p) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 104 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 105 | # can't unregister non-existent object |
| 106 | p = select.poll() |
| 107 | self.assertRaises(KeyError, p.unregister, 3) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 108 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 109 | # Test error cases |
| 110 | pollster = select.poll() |
| 111 | class Nope: |
| 112 | pass |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 113 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 114 | class Almost: |
| 115 | def fileno(self): |
| 116 | return 'fileno' |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 117 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 118 | self.assertRaises(TypeError, pollster.register, Nope(), 0) |
| 119 | self.assertRaises(TypeError, pollster.register, Almost(), 0) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 120 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 121 | # Another test case for poll(). This is copied from the test case for |
| 122 | # select(), modified to use poll() instead. |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 123 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 124 | def test_poll2(self): |
| 125 | cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' |
Richard Oudkerk | 07c34bf | 2012-12-09 16:05:22 +0000 | [diff] [blame] | 126 | proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, |
| 127 | bufsize=0) |
| 128 | p = proc.stdout |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 129 | pollster = select.poll() |
| 130 | pollster.register( p, select.POLLIN ) |
| 131 | for tout in (0, 1000, 2000, 4000, 8000, 16000) + (-1,)*10: |
| 132 | fdlist = pollster.poll(tout) |
| 133 | if (fdlist == []): |
| 134 | continue |
| 135 | fd, flags = fdlist[0] |
| 136 | if flags & select.POLLHUP: |
| 137 | line = p.readline() |
Richard Oudkerk | 07c34bf | 2012-12-09 16:05:22 +0000 | [diff] [blame] | 138 | if line != b"": |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 139 | self.fail('error: pipe seems to be closed, but still returns data') |
| 140 | continue |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 141 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 142 | elif flags & select.POLLIN: |
| 143 | line = p.readline() |
| 144 | if not line: |
| 145 | break |
Richard Oudkerk | 07c34bf | 2012-12-09 16:05:22 +0000 | [diff] [blame] | 146 | self.assertEqual(line, b'testing...\n') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 147 | continue |
| 148 | else: |
| 149 | self.fail('Unexpected return value from select.poll: %s' % fdlist) |
| 150 | p.close() |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 151 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 152 | def test_poll3(self): |
| 153 | # test int overflow |
| 154 | pollster = select.poll() |
| 155 | pollster.register(1) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 157 | self.assertRaises(OverflowError, pollster.poll, 1 << 64) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 158 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 159 | x = 2 + 3 |
| 160 | if x != 5: |
| 161 | self.fail('Overflow must have occurred') |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 162 | |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 163 | # Issues #15989, #17919 |
| 164 | self.assertRaises(OverflowError, pollster.register, 0, -1) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 165 | self.assertRaises(OverflowError, pollster.register, 0, 1 << 64) |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 166 | self.assertRaises(OverflowError, pollster.modify, 1, -1) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 167 | self.assertRaises(OverflowError, pollster.modify, 1, 1 << 64) |
| 168 | |
| 169 | @cpython_only |
| 170 | def test_poll_c_limits(self): |
| 171 | from _testcapi import USHRT_MAX, INT_MAX, UINT_MAX |
| 172 | pollster = select.poll() |
| 173 | pollster.register(1) |
| 174 | |
| 175 | # Issues #15989, #17919 |
| 176 | self.assertRaises(OverflowError, pollster.register, 0, USHRT_MAX + 1) |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 177 | self.assertRaises(OverflowError, pollster.modify, 1, USHRT_MAX + 1) |
| 178 | self.assertRaises(OverflowError, pollster.poll, INT_MAX + 1) |
| 179 | self.assertRaises(OverflowError, pollster.poll, UINT_MAX + 1) |
Serhiy Storchaka | 7898043 | 2013-01-15 01:12:17 +0200 | [diff] [blame] | 180 | |
Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 181 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 182 | @reap_threads |
| 183 | def test_threaded_poll(self): |
| 184 | r, w = os.pipe() |
| 185 | self.addCleanup(os.close, r) |
| 186 | self.addCleanup(os.close, w) |
| 187 | rfds = [] |
| 188 | for i in range(10): |
| 189 | fd = os.dup(r) |
| 190 | self.addCleanup(os.close, fd) |
| 191 | rfds.append(fd) |
| 192 | pollster = select.poll() |
| 193 | for fd in rfds: |
| 194 | pollster.register(fd, select.POLLIN) |
| 195 | |
| 196 | t = threading.Thread(target=pollster.poll) |
| 197 | t.start() |
| 198 | try: |
| 199 | time.sleep(0.5) |
| 200 | # trigger ufds array reallocation |
| 201 | for fd in rfds: |
| 202 | pollster.unregister(fd) |
| 203 | pollster.register(w, select.POLLOUT) |
| 204 | self.assertRaises(RuntimeError, pollster.poll) |
| 205 | finally: |
| 206 | # and make the call to poll() from the thread return |
| 207 | os.write(w, b'spam') |
| 208 | t.join() |
| 209 | |
| 210 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 211 | def test_main(): |
| 212 | run_unittest(PollTests) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 213 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 214 | if __name__ == '__main__': |
| 215 | test_main() |