blob: d593495a999f20c350f67bc41aadd87e4bc6f535 [file] [log] [blame]
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +00001# Test case for the os.poll() function
Fred Drake004d5e62000-10-23 17:22:08 +00002
Serhiy Storchakab1973c22013-08-20 20:38:21 +03003import os
Christian Heimes06888972013-08-20 22:09:41 +02004import subprocess
Serhiy Storchakab1973c22013-08-20 20:38:21 +03005import random
6import select
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02007import threading
Serhiy Storchakab1973c22013-08-20 20:38:21 +03008import time
9import unittest
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020010from test.support import TESTFN, run_unittest, reap_threads, cpython_only
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000011
12try:
13 select.poll
14except AttributeError:
Brett Cannoncd8efa32012-11-14 15:16:53 -050015 raise unittest.SkipTest("select.poll not defined")
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000016
17
18def find_ready_matching(ready, flag):
19 match = []
20 for fd, mode in ready:
21 if mode & flag:
22 match.append(fd)
23 return match
24
Thomas Wouters89f507f2006-12-13 04:49:30 +000025class PollTests(unittest.TestCase):
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000026
Thomas Wouters89f507f2006-12-13 04:49:30 +000027 def test_poll1(self):
28 # Basic functional test of poll object
29 # Create a bunch of pipe and test that poll works with them.
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000030
Thomas Wouters89f507f2006-12-13 04:49:30 +000031 p = select.poll()
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000032
Thomas Wouters89f507f2006-12-13 04:49:30 +000033 NUM_PIPES = 12
Guido van Rossumbdab7d32007-07-11 20:43:16 +000034 MSG = b" This is a test."
Thomas Wouters89f507f2006-12-13 04:49:30 +000035 MSG_LEN = len(MSG)
36 readers = []
37 writers = []
38 r2w = {}
39 w2r = {}
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000040
Thomas Wouters89f507f2006-12-13 04:49:30 +000041 for i in range(NUM_PIPES):
42 rd, wr = os.pipe()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000043 p.register(rd)
44 p.modify(rd, select.POLLIN)
Thomas Wouters89f507f2006-12-13 04:49:30 +000045 p.register(wr, select.POLLOUT)
46 readers.append(rd)
47 writers.append(wr)
48 r2w[rd] = wr
49 w2r[wr] = rd
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000050
Thomas Wouters89f507f2006-12-13 04:49:30 +000051 bufs = []
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000052
Thomas Wouters89f507f2006-12-13 04:49:30 +000053 while writers:
54 ready = p.poll()
55 ready_writers = find_ready_matching(ready, select.POLLOUT)
56 if not ready_writers:
Collin Winter3add4d72007-08-29 23:37:32 +000057 raise RuntimeError("no pipes ready for writing")
Thomas Wouters89f507f2006-12-13 04:49:30 +000058 wr = random.choice(ready_writers)
59 os.write(wr, MSG)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000060
Thomas Wouters89f507f2006-12-13 04:49:30 +000061 ready = p.poll()
62 ready_readers = find_ready_matching(ready, select.POLLIN)
63 if not ready_readers:
Collin Winter3add4d72007-08-29 23:37:32 +000064 raise RuntimeError("no pipes ready for reading")
Thomas Wouters89f507f2006-12-13 04:49:30 +000065 rd = random.choice(ready_readers)
66 buf = os.read(rd, MSG_LEN)
67 self.assertEqual(len(buf), MSG_LEN)
68 bufs.append(buf)
69 os.close(r2w[rd]) ; os.close( rd )
70 p.unregister( r2w[rd] )
71 p.unregister( rd )
72 writers.remove(r2w[rd])
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000073
Thomas Wouters89f507f2006-12-13 04:49:30 +000074 self.assertEqual(bufs, [MSG] * NUM_PIPES)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000075
Richard Oudkerk53dff0c2012-12-09 16:05:20 +000076 def test_poll_unit_tests(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 # returns NVAL for invalid file descriptor
Richard Oudkerk53dff0c2012-12-09 16:05:20 +000078 FD, w = os.pipe()
79 os.close(FD)
80 os.close(w)
Thomas Wouters89f507f2006-12-13 04:49:30 +000081 p = select.poll()
82 p.register(FD)
83 r = p.poll()
84 self.assertEqual(r[0], (FD, select.POLLNVAL))
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000085
Thomas Wouters89f507f2006-12-13 04:49:30 +000086 f = open(TESTFN, 'w')
87 fd = f.fileno()
88 p = select.poll()
89 p.register(f)
90 r = p.poll()
91 self.assertEqual(r[0][0], fd)
92 f.close()
93 r = p.poll()
94 self.assertEqual(r[0], (fd, select.POLLNVAL))
95 os.unlink(TESTFN)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000096
Thomas Wouters89f507f2006-12-13 04:49:30 +000097 # type error for invalid arguments
98 p = select.poll()
99 self.assertRaises(TypeError, p.register, p)
100 self.assertRaises(TypeError, p.unregister, p)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000101
Thomas Wouters89f507f2006-12-13 04:49:30 +0000102 # can't unregister non-existent object
103 p = select.poll()
104 self.assertRaises(KeyError, p.unregister, 3)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000105
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106 # Test error cases
107 pollster = select.poll()
108 class Nope:
109 pass
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000110
Thomas Wouters89f507f2006-12-13 04:49:30 +0000111 class Almost:
112 def fileno(self):
113 return 'fileno'
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000114
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 self.assertRaises(TypeError, pollster.register, Nope(), 0)
116 self.assertRaises(TypeError, pollster.register, Almost(), 0)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000117
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 # Another test case for poll(). This is copied from the test case for
119 # select(), modified to use poll() instead.
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000120
Thomas Wouters89f507f2006-12-13 04:49:30 +0000121 def test_poll2(self):
122 cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
Richard Oudkerk07c34bf2012-12-09 16:05:22 +0000123 proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
124 bufsize=0)
Martin Panter3cf0b252016-08-12 11:59:52 +0000125 proc.__enter__()
126 self.addCleanup(proc.__exit__, None, None, None)
Richard Oudkerk07c34bf2012-12-09 16:05:22 +0000127 p = proc.stdout
Thomas Wouters89f507f2006-12-13 04:49:30 +0000128 pollster = select.poll()
129 pollster.register( p, select.POLLIN )
130 for tout in (0, 1000, 2000, 4000, 8000, 16000) + (-1,)*10:
131 fdlist = pollster.poll(tout)
132 if (fdlist == []):
133 continue
134 fd, flags = fdlist[0]
135 if flags & select.POLLHUP:
136 line = p.readline()
Richard Oudkerk07c34bf2012-12-09 16:05:22 +0000137 if line != b"":
Thomas Wouters89f507f2006-12-13 04:49:30 +0000138 self.fail('error: pipe seems to be closed, but still returns data')
139 continue
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000140
Thomas Wouters89f507f2006-12-13 04:49:30 +0000141 elif flags & select.POLLIN:
142 line = p.readline()
143 if not line:
144 break
Richard Oudkerk07c34bf2012-12-09 16:05:22 +0000145 self.assertEqual(line, b'testing...\n')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000146 continue
147 else:
148 self.fail('Unexpected return value from select.poll: %s' % fdlist)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000149
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 def test_poll3(self):
151 # test int overflow
152 pollster = select.poll()
153 pollster.register(1)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000154
Guido van Rossume2a383d2007-01-15 16:59:06 +0000155 self.assertRaises(OverflowError, pollster.poll, 1 << 64)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000156
Thomas Wouters89f507f2006-12-13 04:49:30 +0000157 x = 2 + 3
158 if x != 5:
159 self.fail('Overflow must have occurred')
Tim Peters536cf992005-12-25 23:18:31 +0000160
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200161 # Issues #15989, #17919
162 self.assertRaises(OverflowError, pollster.register, 0, -1)
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200163 self.assertRaises(OverflowError, pollster.register, 0, 1 << 64)
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200164 self.assertRaises(OverflowError, pollster.modify, 1, -1)
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200165 self.assertRaises(OverflowError, pollster.modify, 1, 1 << 64)
166
167 @cpython_only
168 def test_poll_c_limits(self):
169 from _testcapi import USHRT_MAX, INT_MAX, UINT_MAX
170 pollster = select.poll()
171 pollster.register(1)
172
173 # Issues #15989, #17919
174 self.assertRaises(OverflowError, pollster.register, 0, USHRT_MAX + 1)
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200175 self.assertRaises(OverflowError, pollster.modify, 1, USHRT_MAX + 1)
176 self.assertRaises(OverflowError, pollster.poll, INT_MAX + 1)
177 self.assertRaises(OverflowError, pollster.poll, UINT_MAX + 1)
Serhiy Storchaka78980432013-01-15 01:12:17 +0200178
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300179 @reap_threads
180 def test_threaded_poll(self):
181 r, w = os.pipe()
182 self.addCleanup(os.close, r)
183 self.addCleanup(os.close, w)
184 rfds = []
185 for i in range(10):
186 fd = os.dup(r)
187 self.addCleanup(os.close, fd)
188 rfds.append(fd)
189 pollster = select.poll()
190 for fd in rfds:
191 pollster.register(fd, select.POLLIN)
192
193 t = threading.Thread(target=pollster.poll)
194 t.start()
195 try:
196 time.sleep(0.5)
197 # trigger ufds array reallocation
198 for fd in rfds:
199 pollster.unregister(fd)
200 pollster.register(w, select.POLLOUT)
201 self.assertRaises(RuntimeError, pollster.poll)
202 finally:
203 # and make the call to poll() from the thread return
204 os.write(w, b'spam')
205 t.join()
206
Pablo Galindo2c15b292017-10-17 15:14:41 +0100207 @unittest.skipUnless(threading, 'Threading required for this test.')
208 @reap_threads
209 def test_poll_blocks_with_negative_ms(self):
Riccardo Coccioli6cfa9272017-10-17 21:45:07 +0200210 for timeout_ms in [None, -1000, -1, -1.0, -0.1, -1e-100]:
Pablo Galindo2c15b292017-10-17 15:14:41 +0100211 # Create two file descriptors. This will be used to unlock
212 # the blocking call to poll.poll inside the thread
213 r, w = os.pipe()
214 pollster = select.poll()
215 pollster.register(r, select.POLLIN)
216
217 poll_thread = threading.Thread(target=pollster.poll, args=(timeout_ms,))
218 poll_thread.start()
219 poll_thread.join(timeout=0.1)
220 self.assertTrue(poll_thread.is_alive())
221
222 # Write to the pipe so pollster.poll unblocks and the thread ends.
223 os.write(w, b'spam')
224 poll_thread.join()
225 self.assertFalse(poll_thread.is_alive())
226 os.close(r)
227 os.close(w)
228
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300229
Thomas Wouters89f507f2006-12-13 04:49:30 +0000230def test_main():
231 run_unittest(PollTests)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000232
Thomas Wouters89f507f2006-12-13 04:49:30 +0000233if __name__ == '__main__':
234 test_main()