blob: a0a332bdbcb75c93dcf21bf1b304902ae7f306dd [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
Serhiy Storchakab1973c22013-08-20 20:38:21 +03007try:
8 import threading
9except ImportError:
10 threading = None
11import time
12import unittest
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +020013from test.support import TESTFN, run_unittest, reap_threads, cpython_only
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000014
15try:
16 select.poll
17except AttributeError:
Brett Cannoncd8efa32012-11-14 15:16:53 -050018 raise unittest.SkipTest("select.poll not defined")
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000019
20
21def 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 Wouters89f507f2006-12-13 04:49:30 +000028class PollTests(unittest.TestCase):
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000029
Thomas Wouters89f507f2006-12-13 04:49:30 +000030 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. Kuchling3227cc82000-08-25 01:18:45 +000033
Thomas Wouters89f507f2006-12-13 04:49:30 +000034 p = select.poll()
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000035
Thomas Wouters89f507f2006-12-13 04:49:30 +000036 NUM_PIPES = 12
Guido van Rossumbdab7d32007-07-11 20:43:16 +000037 MSG = b" This is a test."
Thomas Wouters89f507f2006-12-13 04:49:30 +000038 MSG_LEN = len(MSG)
39 readers = []
40 writers = []
41 r2w = {}
42 w2r = {}
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000043
Thomas Wouters89f507f2006-12-13 04:49:30 +000044 for i in range(NUM_PIPES):
45 rd, wr = os.pipe()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000046 p.register(rd)
47 p.modify(rd, select.POLLIN)
Thomas Wouters89f507f2006-12-13 04:49:30 +000048 p.register(wr, select.POLLOUT)
49 readers.append(rd)
50 writers.append(wr)
51 r2w[rd] = wr
52 w2r[wr] = rd
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000053
Thomas Wouters89f507f2006-12-13 04:49:30 +000054 bufs = []
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000055
Thomas Wouters89f507f2006-12-13 04:49:30 +000056 while writers:
57 ready = p.poll()
58 ready_writers = find_ready_matching(ready, select.POLLOUT)
59 if not ready_writers:
Collin Winter3add4d72007-08-29 23:37:32 +000060 raise RuntimeError("no pipes ready for writing")
Thomas Wouters89f507f2006-12-13 04:49:30 +000061 wr = random.choice(ready_writers)
62 os.write(wr, MSG)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000063
Thomas Wouters89f507f2006-12-13 04:49:30 +000064 ready = p.poll()
65 ready_readers = find_ready_matching(ready, select.POLLIN)
66 if not ready_readers:
Collin Winter3add4d72007-08-29 23:37:32 +000067 raise RuntimeError("no pipes ready for reading")
Thomas Wouters89f507f2006-12-13 04:49:30 +000068 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. Kuchling3227cc82000-08-25 01:18:45 +000076
Thomas Wouters89f507f2006-12-13 04:49:30 +000077 self.assertEqual(bufs, [MSG] * NUM_PIPES)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000078
Richard Oudkerk53dff0c2012-12-09 16:05:20 +000079 def test_poll_unit_tests(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +000080 # returns NVAL for invalid file descriptor
Richard Oudkerk53dff0c2012-12-09 16:05:20 +000081 FD, w = os.pipe()
82 os.close(FD)
83 os.close(w)
Thomas Wouters89f507f2006-12-13 04:49:30 +000084 p = select.poll()
85 p.register(FD)
86 r = p.poll()
87 self.assertEqual(r[0], (FD, select.POLLNVAL))
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000088
Thomas Wouters89f507f2006-12-13 04:49:30 +000089 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. Kuchling3227cc82000-08-25 01:18:45 +000099
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100 # 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. Kuchling3227cc82000-08-25 01:18:45 +0000104
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 # can't unregister non-existent object
106 p = select.poll()
107 self.assertRaises(KeyError, p.unregister, 3)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000108
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109 # Test error cases
110 pollster = select.poll()
111 class Nope:
112 pass
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000113
Thomas Wouters89f507f2006-12-13 04:49:30 +0000114 class Almost:
115 def fileno(self):
116 return 'fileno'
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000117
Thomas Wouters89f507f2006-12-13 04:49:30 +0000118 self.assertRaises(TypeError, pollster.register, Nope(), 0)
119 self.assertRaises(TypeError, pollster.register, Almost(), 0)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000120
Thomas Wouters89f507f2006-12-13 04:49:30 +0000121 # Another test case for poll(). This is copied from the test case for
122 # select(), modified to use poll() instead.
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000123
Thomas Wouters89f507f2006-12-13 04:49:30 +0000124 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 Oudkerk07c34bf2012-12-09 16:05:22 +0000126 proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
127 bufsize=0)
128 p = proc.stdout
Thomas Wouters89f507f2006-12-13 04:49:30 +0000129 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 Oudkerk07c34bf2012-12-09 16:05:22 +0000138 if line != b"":
Thomas Wouters89f507f2006-12-13 04:49:30 +0000139 self.fail('error: pipe seems to be closed, but still returns data')
140 continue
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000141
Thomas Wouters89f507f2006-12-13 04:49:30 +0000142 elif flags & select.POLLIN:
143 line = p.readline()
144 if not line:
145 break
Richard Oudkerk07c34bf2012-12-09 16:05:22 +0000146 self.assertEqual(line, b'testing...\n')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000147 continue
148 else:
149 self.fail('Unexpected return value from select.poll: %s' % fdlist)
150 p.close()
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000151
Thomas Wouters89f507f2006-12-13 04:49:30 +0000152 def test_poll3(self):
153 # test int overflow
154 pollster = select.poll()
155 pollster.register(1)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000156
Guido van Rossume2a383d2007-01-15 16:59:06 +0000157 self.assertRaises(OverflowError, pollster.poll, 1 << 64)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000158
Thomas Wouters89f507f2006-12-13 04:49:30 +0000159 x = 2 + 3
160 if x != 5:
161 self.fail('Overflow must have occurred')
Tim Peters536cf992005-12-25 23:18:31 +0000162
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200163 # Issues #15989, #17919
164 self.assertRaises(OverflowError, pollster.register, 0, -1)
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200165 self.assertRaises(OverflowError, pollster.register, 0, 1 << 64)
Serhiy Storchaka5da107a2013-12-14 19:12:02 +0200166 self.assertRaises(OverflowError, pollster.modify, 1, -1)
Serhiy Storchaka5cfc79d2014-02-07 10:06:39 +0200167 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 Storchaka5da107a2013-12-14 19:12:02 +0200177 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 Storchaka78980432013-01-15 01:12:17 +0200180
Serhiy Storchakab1973c22013-08-20 20:38:21 +0300181 @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 Wouters89f507f2006-12-13 04:49:30 +0000211def test_main():
212 run_unittest(PollTests)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000213
Thomas Wouters89f507f2006-12-13 04:49:30 +0000214if __name__ == '__main__':
215 test_main()