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 |
| 4 | import random |
| 5 | import select |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 6 | import _testcapi |
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 |
| 13 | from test.support import TESTFN, run_unittest, reap_threads |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 14 | |
| 15 | try: |
| 16 | select.poll |
| 17 | except AttributeError: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 18 | raise unittest.SkipTest("select.poll not defined -- skipping test_poll") |
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 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 79 | def poll_unit_tests(self): |
| 80 | # returns NVAL for invalid file descriptor |
| 81 | FD = 42 |
| 82 | try: |
| 83 | os.close(FD) |
| 84 | except OSError: |
| 85 | pass |
| 86 | p = select.poll() |
| 87 | p.register(FD) |
| 88 | r = p.poll() |
| 89 | self.assertEqual(r[0], (FD, select.POLLNVAL)) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 90 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 91 | f = open(TESTFN, 'w') |
| 92 | fd = f.fileno() |
| 93 | p = select.poll() |
| 94 | p.register(f) |
| 95 | r = p.poll() |
| 96 | self.assertEqual(r[0][0], fd) |
| 97 | f.close() |
| 98 | r = p.poll() |
| 99 | self.assertEqual(r[0], (fd, select.POLLNVAL)) |
| 100 | os.unlink(TESTFN) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 101 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 102 | # type error for invalid arguments |
| 103 | p = select.poll() |
| 104 | self.assertRaises(TypeError, p.register, p) |
| 105 | self.assertRaises(TypeError, p.unregister, p) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 106 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 107 | # can't unregister non-existent object |
| 108 | p = select.poll() |
| 109 | self.assertRaises(KeyError, p.unregister, 3) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 110 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 111 | # Test error cases |
| 112 | pollster = select.poll() |
| 113 | class Nope: |
| 114 | pass |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 115 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 116 | class Almost: |
| 117 | def fileno(self): |
| 118 | return 'fileno' |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 119 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 120 | self.assertRaises(TypeError, pollster.register, Nope(), 0) |
| 121 | self.assertRaises(TypeError, pollster.register, Almost(), 0) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 122 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 123 | # Another test case for poll(). This is copied from the test case for |
| 124 | # select(), modified to use poll() instead. |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 125 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 126 | def test_poll2(self): |
| 127 | cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' |
| 128 | p = os.popen(cmd, 'r') |
| 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() |
| 138 | if line != "": |
| 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 |
| 146 | continue |
| 147 | else: |
| 148 | self.fail('Unexpected return value from select.poll: %s' % fdlist) |
| 149 | p.close() |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 150 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 151 | def test_poll3(self): |
| 152 | # test int overflow |
| 153 | pollster = select.poll() |
| 154 | pollster.register(1) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 155 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 156 | self.assertRaises(OverflowError, pollster.poll, 1 << 64) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 157 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 158 | x = 2 + 3 |
| 159 | if x != 5: |
| 160 | self.fail('Overflow must have occurred') |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 161 | |
Serhiy Storchaka | 441d30f | 2013-01-19 12:26:26 +0200 | [diff] [blame] | 162 | pollster = select.poll() |
| 163 | # Issue 15989 |
| 164 | self.assertRaises(OverflowError, pollster.register, 0, |
| 165 | _testcapi.SHRT_MAX + 1) |
| 166 | self.assertRaises(OverflowError, pollster.register, 0, |
| 167 | _testcapi.USHRT_MAX + 1) |
| 168 | self.assertRaises(OverflowError, pollster.poll, _testcapi.INT_MAX + 1) |
| 169 | self.assertRaises(OverflowError, pollster.poll, _testcapi.UINT_MAX + 1) |
| 170 | |
Serhiy Storchaka | b1973c2 | 2013-08-20 20:38:21 +0300 | [diff] [blame] | 171 | @unittest.skipUnless(threading, 'Threading required for this test.') |
| 172 | @reap_threads |
| 173 | def test_threaded_poll(self): |
| 174 | r, w = os.pipe() |
| 175 | self.addCleanup(os.close, r) |
| 176 | self.addCleanup(os.close, w) |
| 177 | rfds = [] |
| 178 | for i in range(10): |
| 179 | fd = os.dup(r) |
| 180 | self.addCleanup(os.close, fd) |
| 181 | rfds.append(fd) |
| 182 | pollster = select.poll() |
| 183 | for fd in rfds: |
| 184 | pollster.register(fd, select.POLLIN) |
| 185 | |
| 186 | t = threading.Thread(target=pollster.poll) |
| 187 | t.start() |
| 188 | try: |
| 189 | time.sleep(0.5) |
| 190 | # trigger ufds array reallocation |
| 191 | for fd in rfds: |
| 192 | pollster.unregister(fd) |
| 193 | pollster.register(w, select.POLLOUT) |
| 194 | self.assertRaises(RuntimeError, pollster.poll) |
| 195 | finally: |
| 196 | # and make the call to poll() from the thread return |
| 197 | os.write(w, b'spam') |
| 198 | t.join() |
| 199 | |
| 200 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 201 | def test_main(): |
| 202 | run_unittest(PollTests) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 203 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 204 | if __name__ == '__main__': |
| 205 | test_main() |