Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 1 | # Copyright (c) 2001-2006 Twisted Matrix Laboratories. |
| 2 | # |
| 3 | # Permission is hereby granted, free of charge, to any person obtaining |
| 4 | # a copy of this software and associated documentation files (the |
| 5 | # "Software"), to deal in the Software without restriction, including |
| 6 | # without limitation the rights to use, copy, modify, merge, publish, |
| 7 | # distribute, sublicense, and/or sell copies of the Software, and to |
| 8 | # permit persons to whom the Software is furnished to do so, subject to |
| 9 | # the following conditions: |
| 10 | # |
| 11 | # The above copyright notice and this permission notice shall be |
| 12 | # included in all copies or substantial portions of the Software. |
| 13 | # |
| 14 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 17 | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 18 | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 19 | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 20 | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | """ |
| 22 | Tests for epoll wrapper. |
| 23 | """ |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 24 | import errno |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 25 | import os |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 26 | import select |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 27 | import socket |
| 28 | import time |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 29 | import unittest |
| 30 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 31 | if not hasattr(select, "epoll"): |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 32 | raise unittest.SkipTest("test works only on Linux 2.6") |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 33 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 34 | try: |
| 35 | select.epoll() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 36 | except OSError as e: |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 37 | if e.errno == errno.ENOSYS: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 38 | raise unittest.SkipTest("kernel doesn't support epoll()") |
Benjamin Peterson | 0e61350 | 2011-12-27 15:16:34 -0600 | [diff] [blame] | 39 | raise |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 40 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 41 | class TestEPoll(unittest.TestCase): |
| 42 | |
| 43 | def setUp(self): |
| 44 | self.serverSocket = socket.socket() |
| 45 | self.serverSocket.bind(('127.0.0.1', 0)) |
Charles-François Natali | 6e20460 | 2014-07-23 19:28:13 +0100 | [diff] [blame] | 46 | self.serverSocket.listen() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 47 | self.connections = [self.serverSocket] |
| 48 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 49 | def tearDown(self): |
| 50 | for skt in self.connections: |
| 51 | skt.close() |
| 52 | |
| 53 | def _connected_pair(self): |
| 54 | client = socket.socket() |
| 55 | client.setblocking(False) |
| 56 | try: |
| 57 | client.connect(('127.0.0.1', self.serverSocket.getsockname()[1])) |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 58 | except OSError as e: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 59 | self.assertEqual(e.args[0], errno.EINPROGRESS) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 60 | else: |
| 61 | raise AssertionError("Connect should have raised EINPROGRESS") |
| 62 | server, addr = self.serverSocket.accept() |
| 63 | |
| 64 | self.connections.extend((client, server)) |
| 65 | return client, server |
| 66 | |
| 67 | def test_create(self): |
| 68 | try: |
| 69 | ep = select.epoll(16) |
| 70 | except OSError as e: |
| 71 | raise AssertionError(str(e)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 72 | self.assertTrue(ep.fileno() > 0, ep.fileno()) |
| 73 | self.assertTrue(not ep.closed) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 74 | ep.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 75 | self.assertTrue(ep.closed) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 76 | self.assertRaises(ValueError, ep.fileno) |
Benjamin Peterson | 70069fc | 2011-12-27 16:52:20 -0600 | [diff] [blame] | 77 | if hasattr(select, "EPOLL_CLOEXEC"): |
| 78 | select.epoll(select.EPOLL_CLOEXEC).close() |
Berker Peksag | e2197d1 | 2016-09-26 23:30:41 +0300 | [diff] [blame] | 79 | select.epoll(flags=select.EPOLL_CLOEXEC).close() |
| 80 | select.epoll(flags=0).close() |
Benjamin Peterson | 70069fc | 2011-12-27 16:52:20 -0600 | [diff] [blame] | 81 | self.assertRaises(OSError, select.epoll, flags=12356) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 82 | |
| 83 | def test_badcreate(self): |
| 84 | self.assertRaises(TypeError, select.epoll, 1, 2, 3) |
| 85 | self.assertRaises(TypeError, select.epoll, 'foo') |
| 86 | self.assertRaises(TypeError, select.epoll, None) |
| 87 | self.assertRaises(TypeError, select.epoll, ()) |
| 88 | self.assertRaises(TypeError, select.epoll, ['foo']) |
| 89 | self.assertRaises(TypeError, select.epoll, {}) |
| 90 | |
Antoine Pitrou | 09bb89b | 2012-12-15 21:14:21 +0100 | [diff] [blame] | 91 | def test_context_manager(self): |
| 92 | with select.epoll(16) as ep: |
| 93 | self.assertGreater(ep.fileno(), 0) |
| 94 | self.assertFalse(ep.closed) |
| 95 | self.assertTrue(ep.closed) |
| 96 | self.assertRaises(ValueError, ep.fileno) |
| 97 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 98 | def test_add(self): |
| 99 | server, client = self._connected_pair() |
| 100 | |
| 101 | ep = select.epoll(2) |
| 102 | try: |
| 103 | ep.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 104 | ep.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 105 | finally: |
| 106 | ep.close() |
| 107 | |
R. David Murray | 1855c21 | 2009-05-31 19:44:27 +0000 | [diff] [blame] | 108 | # adding by object w/ fileno works, too. |
| 109 | ep = select.epoll(2) |
| 110 | try: |
| 111 | ep.register(server, select.EPOLLIN | select.EPOLLOUT) |
| 112 | ep.register(client, select.EPOLLIN | select.EPOLLOUT) |
| 113 | finally: |
| 114 | ep.close() |
| 115 | |
| 116 | ep = select.epoll(2) |
| 117 | try: |
| 118 | # TypeError: argument must be an int, or have a fileno() method. |
| 119 | self.assertRaises(TypeError, ep.register, object(), |
| 120 | select.EPOLLIN | select.EPOLLOUT) |
| 121 | self.assertRaises(TypeError, ep.register, None, |
| 122 | select.EPOLLIN | select.EPOLLOUT) |
| 123 | # ValueError: file descriptor cannot be a negative integer (-1) |
| 124 | self.assertRaises(ValueError, ep.register, -1, |
| 125 | select.EPOLLIN | select.EPOLLOUT) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 126 | # OSError: [Errno 9] Bad file descriptor |
| 127 | self.assertRaises(OSError, ep.register, 10000, |
R. David Murray | 1855c21 | 2009-05-31 19:44:27 +0000 | [diff] [blame] | 128 | select.EPOLLIN | select.EPOLLOUT) |
| 129 | # registering twice also raises an exception |
| 130 | ep.register(server, select.EPOLLIN | select.EPOLLOUT) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 131 | self.assertRaises(OSError, ep.register, server, |
R. David Murray | 1855c21 | 2009-05-31 19:44:27 +0000 | [diff] [blame] | 132 | select.EPOLLIN | select.EPOLLOUT) |
| 133 | finally: |
| 134 | ep.close() |
| 135 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 136 | def test_fromfd(self): |
| 137 | server, client = self._connected_pair() |
| 138 | |
| 139 | ep = select.epoll(2) |
| 140 | ep2 = select.epoll.fromfd(ep.fileno()) |
| 141 | |
| 142 | ep2.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 143 | ep2.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 144 | |
| 145 | events = ep.poll(1, 4) |
| 146 | events2 = ep2.poll(0.9, 4) |
| 147 | self.assertEqual(len(events), 2) |
| 148 | self.assertEqual(len(events2), 2) |
| 149 | |
| 150 | ep.close() |
| 151 | try: |
| 152 | ep2.poll(1, 4) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 153 | except OSError as e: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 154 | self.assertEqual(e.args[0], errno.EBADF, e) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 155 | else: |
| 156 | self.fail("epoll on closed fd didn't raise EBADF") |
| 157 | |
| 158 | def test_control_and_wait(self): |
| 159 | client, server = self._connected_pair() |
| 160 | |
| 161 | ep = select.epoll(16) |
| 162 | ep.register(server.fileno(), |
| 163 | select.EPOLLIN | select.EPOLLOUT | select.EPOLLET) |
| 164 | ep.register(client.fileno(), |
| 165 | select.EPOLLIN | select.EPOLLOUT | select.EPOLLET) |
| 166 | |
Victor Stinner | b60ac7a | 2014-03-25 12:50:50 +0100 | [diff] [blame] | 167 | now = time.monotonic() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 168 | events = ep.poll(1, 4) |
Victor Stinner | b60ac7a | 2014-03-25 12:50:50 +0100 | [diff] [blame] | 169 | then = time.monotonic() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 170 | self.assertFalse(then - now > 0.1, then - now) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 171 | |
| 172 | events.sort() |
| 173 | expected = [(client.fileno(), select.EPOLLOUT), |
| 174 | (server.fileno(), select.EPOLLOUT)] |
| 175 | expected.sort() |
| 176 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 177 | self.assertEqual(events, expected) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 178 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 179 | events = ep.poll(timeout=2.1, maxevents=4) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 180 | self.assertFalse(events) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 181 | |
| 182 | client.send(b"Hello!") |
| 183 | server.send(b"world!!!") |
| 184 | |
Victor Stinner | b60ac7a | 2014-03-25 12:50:50 +0100 | [diff] [blame] | 185 | now = time.monotonic() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 186 | events = ep.poll(1, 4) |
Victor Stinner | b60ac7a | 2014-03-25 12:50:50 +0100 | [diff] [blame] | 187 | then = time.monotonic() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 188 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 189 | |
| 190 | events.sort() |
| 191 | expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT), |
| 192 | (server.fileno(), select.EPOLLIN | select.EPOLLOUT)] |
| 193 | expected.sort() |
| 194 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 195 | self.assertEqual(events, expected) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 196 | |
| 197 | ep.unregister(client.fileno()) |
| 198 | ep.modify(server.fileno(), select.EPOLLOUT) |
Victor Stinner | b60ac7a | 2014-03-25 12:50:50 +0100 | [diff] [blame] | 199 | now = time.monotonic() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 200 | events = ep.poll(1, 4) |
Victor Stinner | b60ac7a | 2014-03-25 12:50:50 +0100 | [diff] [blame] | 201 | then = time.monotonic() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 202 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 203 | |
| 204 | expected = [(server.fileno(), select.EPOLLOUT)] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 205 | self.assertEqual(events, expected) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 206 | |
| 207 | def test_errors(self): |
| 208 | self.assertRaises(ValueError, select.epoll, -2) |
| 209 | self.assertRaises(ValueError, select.epoll().register, -1, |
| 210 | select.EPOLLIN) |
| 211 | |
| 212 | def test_unregister_closed(self): |
| 213 | server, client = self._connected_pair() |
| 214 | fd = server.fileno() |
| 215 | ep = select.epoll(16) |
| 216 | ep.register(server) |
| 217 | |
Victor Stinner | b60ac7a | 2014-03-25 12:50:50 +0100 | [diff] [blame] | 218 | now = time.monotonic() |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 219 | events = ep.poll(1, 4) |
Victor Stinner | b60ac7a | 2014-03-25 12:50:50 +0100 | [diff] [blame] | 220 | then = time.monotonic() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 221 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 222 | |
| 223 | server.close() |
| 224 | ep.unregister(fd) |
| 225 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 226 | def test_close(self): |
| 227 | open_file = open(__file__, "rb") |
| 228 | self.addCleanup(open_file.close) |
| 229 | fd = open_file.fileno() |
| 230 | epoll = select.epoll() |
| 231 | |
| 232 | # test fileno() method and closed attribute |
| 233 | self.assertIsInstance(epoll.fileno(), int) |
| 234 | self.assertFalse(epoll.closed) |
| 235 | |
| 236 | # test close() |
| 237 | epoll.close() |
| 238 | self.assertTrue(epoll.closed) |
| 239 | self.assertRaises(ValueError, epoll.fileno) |
| 240 | |
| 241 | # close() can be called more than once |
| 242 | epoll.close() |
| 243 | |
| 244 | # operations must fail with ValueError("I/O operation on closed ...") |
| 245 | self.assertRaises(ValueError, epoll.modify, fd, select.EPOLLIN) |
| 246 | self.assertRaises(ValueError, epoll.poll, 1.0) |
| 247 | self.assertRaises(ValueError, epoll.register, fd, select.EPOLLIN) |
| 248 | self.assertRaises(ValueError, epoll.unregister, fd) |
| 249 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 250 | def test_fd_non_inheritable(self): |
| 251 | epoll = select.epoll() |
| 252 | self.addCleanup(epoll.close) |
| 253 | self.assertEqual(os.get_inheritable(epoll.fileno()), False) |
| 254 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 255 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 256 | if __name__ == "__main__": |
Zachary Ware | 38c707e | 2015-04-13 15:00:43 -0500 | [diff] [blame] | 257 | unittest.main() |