blob: 3b7926d5b22cf432e5019b0a042690e64103413a [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
Richard Oudkerk07c34bf2012-12-09 16:05:22 +00003import os, select, random, unittest, subprocess
Benjamin Petersone549ead2009-03-28 21:42:05 +00004from test.support import TESTFN, run_unittest
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +00005
6try:
7 select.poll
8except AttributeError:
Brett Cannoncd8efa32012-11-14 15:16:53 -05009 raise unittest.SkipTest("select.poll not defined")
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000010
11
12def find_ready_matching(ready, flag):
13 match = []
14 for fd, mode in ready:
15 if mode & flag:
16 match.append(fd)
17 return match
18
Thomas Wouters89f507f2006-12-13 04:49:30 +000019class PollTests(unittest.TestCase):
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000020
Thomas Wouters89f507f2006-12-13 04:49:30 +000021 def test_poll1(self):
22 # Basic functional test of poll object
23 # Create a bunch of pipe and test that poll works with them.
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000024
Thomas Wouters89f507f2006-12-13 04:49:30 +000025 p = select.poll()
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000026
Thomas Wouters89f507f2006-12-13 04:49:30 +000027 NUM_PIPES = 12
Guido van Rossumbdab7d32007-07-11 20:43:16 +000028 MSG = b" This is a test."
Thomas Wouters89f507f2006-12-13 04:49:30 +000029 MSG_LEN = len(MSG)
30 readers = []
31 writers = []
32 r2w = {}
33 w2r = {}
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000034
Thomas Wouters89f507f2006-12-13 04:49:30 +000035 for i in range(NUM_PIPES):
36 rd, wr = os.pipe()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000037 p.register(rd)
38 p.modify(rd, select.POLLIN)
Thomas Wouters89f507f2006-12-13 04:49:30 +000039 p.register(wr, select.POLLOUT)
40 readers.append(rd)
41 writers.append(wr)
42 r2w[rd] = wr
43 w2r[wr] = rd
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000044
Thomas Wouters89f507f2006-12-13 04:49:30 +000045 bufs = []
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000046
Thomas Wouters89f507f2006-12-13 04:49:30 +000047 while writers:
48 ready = p.poll()
49 ready_writers = find_ready_matching(ready, select.POLLOUT)
50 if not ready_writers:
Collin Winter3add4d72007-08-29 23:37:32 +000051 raise RuntimeError("no pipes ready for writing")
Thomas Wouters89f507f2006-12-13 04:49:30 +000052 wr = random.choice(ready_writers)
53 os.write(wr, MSG)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000054
Thomas Wouters89f507f2006-12-13 04:49:30 +000055 ready = p.poll()
56 ready_readers = find_ready_matching(ready, select.POLLIN)
57 if not ready_readers:
Collin Winter3add4d72007-08-29 23:37:32 +000058 raise RuntimeError("no pipes ready for reading")
Thomas Wouters89f507f2006-12-13 04:49:30 +000059 rd = random.choice(ready_readers)
60 buf = os.read(rd, MSG_LEN)
61 self.assertEqual(len(buf), MSG_LEN)
62 bufs.append(buf)
63 os.close(r2w[rd]) ; os.close( rd )
64 p.unregister( r2w[rd] )
65 p.unregister( rd )
66 writers.remove(r2w[rd])
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000067
Thomas Wouters89f507f2006-12-13 04:49:30 +000068 self.assertEqual(bufs, [MSG] * NUM_PIPES)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000069
Richard Oudkerk53dff0c2012-12-09 16:05:20 +000070 def test_poll_unit_tests(self):
Thomas Wouters89f507f2006-12-13 04:49:30 +000071 # returns NVAL for invalid file descriptor
Richard Oudkerk53dff0c2012-12-09 16:05:20 +000072 FD, w = os.pipe()
73 os.close(FD)
74 os.close(w)
Thomas Wouters89f507f2006-12-13 04:49:30 +000075 p = select.poll()
76 p.register(FD)
77 r = p.poll()
78 self.assertEqual(r[0], (FD, select.POLLNVAL))
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000079
Thomas Wouters89f507f2006-12-13 04:49:30 +000080 f = open(TESTFN, 'w')
81 fd = f.fileno()
82 p = select.poll()
83 p.register(f)
84 r = p.poll()
85 self.assertEqual(r[0][0], fd)
86 f.close()
87 r = p.poll()
88 self.assertEqual(r[0], (fd, select.POLLNVAL))
89 os.unlink(TESTFN)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000090
Thomas Wouters89f507f2006-12-13 04:49:30 +000091 # type error for invalid arguments
92 p = select.poll()
93 self.assertRaises(TypeError, p.register, p)
94 self.assertRaises(TypeError, p.unregister, p)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000095
Thomas Wouters89f507f2006-12-13 04:49:30 +000096 # can't unregister non-existent object
97 p = select.poll()
98 self.assertRaises(KeyError, p.unregister, 3)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +000099
Thomas Wouters89f507f2006-12-13 04:49:30 +0000100 # Test error cases
101 pollster = select.poll()
102 class Nope:
103 pass
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000104
Thomas Wouters89f507f2006-12-13 04:49:30 +0000105 class Almost:
106 def fileno(self):
107 return 'fileno'
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000108
Thomas Wouters89f507f2006-12-13 04:49:30 +0000109 self.assertRaises(TypeError, pollster.register, Nope(), 0)
110 self.assertRaises(TypeError, pollster.register, Almost(), 0)
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000111
Thomas Wouters89f507f2006-12-13 04:49:30 +0000112 # Another test case for poll(). This is copied from the test case for
113 # select(), modified to use poll() instead.
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000114
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 def test_poll2(self):
116 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 +0000117 proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
118 bufsize=0)
119 p = proc.stdout
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120 pollster = select.poll()
121 pollster.register( p, select.POLLIN )
122 for tout in (0, 1000, 2000, 4000, 8000, 16000) + (-1,)*10:
123 fdlist = pollster.poll(tout)
124 if (fdlist == []):
125 continue
126 fd, flags = fdlist[0]
127 if flags & select.POLLHUP:
128 line = p.readline()
Richard Oudkerk07c34bf2012-12-09 16:05:22 +0000129 if line != b"":
Thomas Wouters89f507f2006-12-13 04:49:30 +0000130 self.fail('error: pipe seems to be closed, but still returns data')
131 continue
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000132
Thomas Wouters89f507f2006-12-13 04:49:30 +0000133 elif flags & select.POLLIN:
134 line = p.readline()
135 if not line:
136 break
Richard Oudkerk07c34bf2012-12-09 16:05:22 +0000137 self.assertEqual(line, b'testing...\n')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000138 continue
139 else:
140 self.fail('Unexpected return value from select.poll: %s' % fdlist)
141 p.close()
Andrew M. Kuchling3227cc82000-08-25 01:18:45 +0000142
Thomas Wouters89f507f2006-12-13 04:49:30 +0000143 def test_poll3(self):
144 # test int overflow
145 pollster = select.poll()
146 pollster.register(1)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000147
Guido van Rossume2a383d2007-01-15 16:59:06 +0000148 self.assertRaises(OverflowError, pollster.poll, 1 << 64)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000149
Thomas Wouters89f507f2006-12-13 04:49:30 +0000150 x = 2 + 3
151 if x != 5:
152 self.fail('Overflow must have occurred')
Tim Peters536cf992005-12-25 23:18:31 +0000153
Thomas Wouters89f507f2006-12-13 04:49:30 +0000154def test_main():
155 run_unittest(PollTests)
Neal Norwitz0f46bbf2005-11-03 05:00:25 +0000156
Thomas Wouters89f507f2006-12-13 04:49:30 +0000157if __name__ == '__main__':
158 test_main()