Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 1 | # Test case for the select.devpoll() function |
| 2 | |
| 3 | # Initial tests are copied as is from "test_poll.py" |
| 4 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 5 | import os |
| 6 | import random |
| 7 | import select |
| 8 | import sys |
| 9 | import unittest |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 10 | from test.support import TESTFN, run_unittest, cpython_only |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 11 | |
Jesus Cea | da25109 | 2014-04-29 18:18:37 +0200 | [diff] [blame] | 12 | if not hasattr(select, 'devpoll') : |
| 13 | raise unittest.SkipTest('test works only on Solaris OS family') |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def find_ready_matching(ready, flag): |
| 17 | match = [] |
| 18 | for fd, mode in ready: |
| 19 | if mode & flag: |
| 20 | match.append(fd) |
| 21 | return match |
| 22 | |
| 23 | class DevPollTests(unittest.TestCase): |
| 24 | |
| 25 | def test_devpoll1(self): |
| 26 | # Basic functional test of poll object |
| 27 | # Create a bunch of pipe and test that poll works with them. |
| 28 | |
| 29 | p = select.devpoll() |
| 30 | |
| 31 | NUM_PIPES = 12 |
| 32 | MSG = b" This is a test." |
| 33 | MSG_LEN = len(MSG) |
| 34 | readers = [] |
| 35 | writers = [] |
| 36 | r2w = {} |
| 37 | w2r = {} |
| 38 | |
| 39 | for i in range(NUM_PIPES): |
| 40 | rd, wr = os.pipe() |
| 41 | p.register(rd) |
| 42 | p.modify(rd, select.POLLIN) |
| 43 | p.register(wr, select.POLLOUT) |
| 44 | readers.append(rd) |
| 45 | writers.append(wr) |
| 46 | r2w[rd] = wr |
| 47 | w2r[wr] = rd |
| 48 | |
| 49 | bufs = [] |
| 50 | |
| 51 | while writers: |
| 52 | ready = p.poll() |
| 53 | ready_writers = find_ready_matching(ready, select.POLLOUT) |
| 54 | if not ready_writers: |
| 55 | self.fail("no pipes ready for writing") |
| 56 | wr = random.choice(ready_writers) |
| 57 | os.write(wr, MSG) |
| 58 | |
| 59 | ready = p.poll() |
| 60 | ready_readers = find_ready_matching(ready, select.POLLIN) |
| 61 | if not ready_readers: |
| 62 | self.fail("no pipes ready for reading") |
| 63 | self.assertEqual([w2r[wr]], ready_readers) |
| 64 | rd = ready_readers[0] |
| 65 | buf = os.read(rd, MSG_LEN) |
| 66 | self.assertEqual(len(buf), MSG_LEN) |
| 67 | bufs.append(buf) |
| 68 | os.close(r2w[rd]) ; os.close(rd) |
| 69 | p.unregister(r2w[rd]) |
| 70 | p.unregister(rd) |
| 71 | writers.remove(r2w[rd]) |
| 72 | |
| 73 | self.assertEqual(bufs, [MSG] * NUM_PIPES) |
| 74 | |
| 75 | def test_timeout_overflow(self): |
| 76 | pollster = select.devpoll() |
| 77 | w, r = os.pipe() |
| 78 | pollster.register(w) |
| 79 | |
| 80 | pollster.poll(-1) |
| 81 | self.assertRaises(OverflowError, pollster.poll, -2) |
| 82 | self.assertRaises(OverflowError, pollster.poll, -1 << 31) |
| 83 | self.assertRaises(OverflowError, pollster.poll, -1 << 64) |
| 84 | |
| 85 | pollster.poll(0) |
| 86 | pollster.poll(1) |
| 87 | pollster.poll(1 << 30) |
| 88 | self.assertRaises(OverflowError, pollster.poll, 1 << 31) |
| 89 | self.assertRaises(OverflowError, pollster.poll, 1 << 63) |
| 90 | self.assertRaises(OverflowError, pollster.poll, 1 << 64) |
| 91 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 92 | def test_close(self): |
| 93 | open_file = open(__file__, "rb") |
| 94 | self.addCleanup(open_file.close) |
| 95 | fd = open_file.fileno() |
| 96 | devpoll = select.devpoll() |
| 97 | |
| 98 | # test fileno() method and closed attribute |
| 99 | self.assertIsInstance(devpoll.fileno(), int) |
| 100 | self.assertFalse(devpoll.closed) |
| 101 | |
| 102 | # test close() |
| 103 | devpoll.close() |
| 104 | self.assertTrue(devpoll.closed) |
| 105 | self.assertRaises(ValueError, devpoll.fileno) |
| 106 | |
| 107 | # close() can be called more than once |
| 108 | devpoll.close() |
| 109 | |
| 110 | # operations must fail with ValueError("I/O operation on closed ...") |
| 111 | self.assertRaises(ValueError, devpoll.modify, fd, select.POLLIN) |
| 112 | self.assertRaises(ValueError, devpoll.poll) |
| 113 | self.assertRaises(ValueError, devpoll.register, fd, fd, select.POLLIN) |
| 114 | self.assertRaises(ValueError, devpoll.unregister, fd) |
| 115 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 116 | def test_fd_non_inheritable(self): |
| 117 | devpoll = select.devpoll() |
| 118 | self.addCleanup(devpoll.close) |
| 119 | self.assertEqual(os.get_inheritable(devpoll.fileno()), False) |
| 120 | |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 121 | def test_events_mask_overflow(self): |
| 122 | pollster = select.devpoll() |
| 123 | w, r = os.pipe() |
| 124 | pollster.register(w) |
| 125 | # Issue #17919 |
| 126 | self.assertRaises(OverflowError, pollster.register, 0, -1) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 127 | self.assertRaises(OverflowError, pollster.register, 0, 1 << 64) |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 128 | self.assertRaises(OverflowError, pollster.modify, 1, -1) |
Serhiy Storchaka | 5cfc79d | 2014-02-07 10:06:39 +0200 | [diff] [blame] | 129 | self.assertRaises(OverflowError, pollster.modify, 1, 1 << 64) |
| 130 | |
| 131 | @cpython_only |
| 132 | def test_events_mask_overflow_c_limits(self): |
| 133 | from _testcapi import USHRT_MAX |
| 134 | pollster = select.devpoll() |
| 135 | w, r = os.pipe() |
| 136 | pollster.register(w) |
| 137 | # Issue #17919 |
| 138 | self.assertRaises(OverflowError, pollster.register, 0, USHRT_MAX + 1) |
Serhiy Storchaka | 5da107a | 2013-12-14 19:12:02 +0200 | [diff] [blame] | 139 | self.assertRaises(OverflowError, pollster.modify, 1, USHRT_MAX + 1) |
| 140 | |
| 141 | |
Jesus Cea | d8b9ae6 | 2011-11-14 19:07:41 +0100 | [diff] [blame] | 142 | def test_main(): |
| 143 | run_unittest(DevPollTests) |
| 144 | |
| 145 | if __name__ == '__main__': |
| 146 | test_main() |