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 | |
Richard Oudkerk | 07c34bf | 2012-12-09 16:05:22 +0000 | [diff] [blame] | 3 | import os, select, random, unittest, subprocess |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 4 | from test.support import TESTFN, run_unittest |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 5 | |
| 6 | try: |
| 7 | select.poll |
| 8 | except AttributeError: |
Brett Cannon | cd8efa3 | 2012-11-14 15:16:53 -0500 | [diff] [blame] | 9 | raise unittest.SkipTest("select.poll not defined") |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | def 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 Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 19 | class PollTests(unittest.TestCase): |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 20 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 21 | 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. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 24 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 25 | p = select.poll() |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 26 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 27 | NUM_PIPES = 12 |
Guido van Rossum | bdab7d3 | 2007-07-11 20:43:16 +0000 | [diff] [blame] | 28 | MSG = b" This is a test." |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 29 | MSG_LEN = len(MSG) |
| 30 | readers = [] |
| 31 | writers = [] |
| 32 | r2w = {} |
| 33 | w2r = {} |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 34 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 35 | for i in range(NUM_PIPES): |
| 36 | rd, wr = os.pipe() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 37 | p.register(rd) |
| 38 | p.modify(rd, select.POLLIN) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 39 | p.register(wr, select.POLLOUT) |
| 40 | readers.append(rd) |
| 41 | writers.append(wr) |
| 42 | r2w[rd] = wr |
| 43 | w2r[wr] = rd |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 44 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 45 | bufs = [] |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 46 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 47 | while writers: |
| 48 | ready = p.poll() |
| 49 | ready_writers = find_ready_matching(ready, select.POLLOUT) |
| 50 | if not ready_writers: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 51 | raise RuntimeError("no pipes ready for writing") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 52 | wr = random.choice(ready_writers) |
| 53 | os.write(wr, MSG) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 54 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 55 | ready = p.poll() |
| 56 | ready_readers = find_ready_matching(ready, select.POLLIN) |
| 57 | if not ready_readers: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 58 | raise RuntimeError("no pipes ready for reading") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 59 | 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. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 67 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 68 | self.assertEqual(bufs, [MSG] * NUM_PIPES) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 69 | |
Richard Oudkerk | 53dff0c | 2012-12-09 16:05:20 +0000 | [diff] [blame] | 70 | def test_poll_unit_tests(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 71 | # returns NVAL for invalid file descriptor |
Richard Oudkerk | 53dff0c | 2012-12-09 16:05:20 +0000 | [diff] [blame] | 72 | FD, w = os.pipe() |
| 73 | os.close(FD) |
| 74 | os.close(w) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 75 | p = select.poll() |
| 76 | p.register(FD) |
| 77 | r = p.poll() |
| 78 | self.assertEqual(r[0], (FD, select.POLLNVAL)) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 79 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 80 | 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. 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 | # 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. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 95 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 96 | # can't unregister non-existent object |
| 97 | p = select.poll() |
| 98 | self.assertRaises(KeyError, p.unregister, 3) |
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 | # Test error cases |
| 101 | pollster = select.poll() |
| 102 | class Nope: |
| 103 | pass |
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 | class Almost: |
| 106 | def fileno(self): |
| 107 | return 'fileno' |
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 | self.assertRaises(TypeError, pollster.register, Nope(), 0) |
| 110 | self.assertRaises(TypeError, pollster.register, Almost(), 0) |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 111 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 112 | # Another test case for poll(). This is copied from the test case for |
| 113 | # select(), modified to use poll() instead. |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 114 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 115 | 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 Oudkerk | 07c34bf | 2012-12-09 16:05:22 +0000 | [diff] [blame] | 117 | proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, |
| 118 | bufsize=0) |
| 119 | p = proc.stdout |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 120 | 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 Oudkerk | 07c34bf | 2012-12-09 16:05:22 +0000 | [diff] [blame] | 129 | if line != b"": |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | self.fail('error: pipe seems to be closed, but still returns data') |
| 131 | continue |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 132 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | elif flags & select.POLLIN: |
| 134 | line = p.readline() |
| 135 | if not line: |
| 136 | break |
Richard Oudkerk | 07c34bf | 2012-12-09 16:05:22 +0000 | [diff] [blame] | 137 | self.assertEqual(line, b'testing...\n') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 138 | continue |
| 139 | else: |
| 140 | self.fail('Unexpected return value from select.poll: %s' % fdlist) |
| 141 | p.close() |
Andrew M. Kuchling | 3227cc8 | 2000-08-25 01:18:45 +0000 | [diff] [blame] | 142 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 143 | def test_poll3(self): |
| 144 | # test int overflow |
| 145 | pollster = select.poll() |
| 146 | pollster.register(1) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 147 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 148 | self.assertRaises(OverflowError, pollster.poll, 1 << 64) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 149 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 150 | x = 2 + 3 |
| 151 | if x != 5: |
| 152 | self.fail('Overflow must have occurred') |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 153 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 154 | def test_main(): |
| 155 | run_unittest(PollTests) |
Neal Norwitz | 0f46bbf | 2005-11-03 05:00:25 +0000 | [diff] [blame] | 156 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 157 | if __name__ == '__main__': |
| 158 | test_main() |