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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 31 | from test import support |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 32 | if not hasattr(select, "epoll"): |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 33 | raise unittest.SkipTest("test works only on Linux 2.6") |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 34 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 35 | try: |
| 36 | select.epoll() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 37 | except OSError as e: |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 38 | if e.errno == errno.ENOSYS: |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 39 | raise unittest.SkipTest("kernel doesn't support epoll()") |
Benjamin Peterson | 0e61350 | 2011-12-27 15:16:34 -0600 | [diff] [blame] | 40 | raise |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 41 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 42 | class TestEPoll(unittest.TestCase): |
| 43 | |
| 44 | def setUp(self): |
| 45 | self.serverSocket = socket.socket() |
| 46 | self.serverSocket.bind(('127.0.0.1', 0)) |
| 47 | self.serverSocket.listen(1) |
| 48 | self.connections = [self.serverSocket] |
| 49 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 50 | def tearDown(self): |
| 51 | for skt in self.connections: |
| 52 | skt.close() |
| 53 | |
| 54 | def _connected_pair(self): |
| 55 | client = socket.socket() |
| 56 | client.setblocking(False) |
| 57 | try: |
| 58 | client.connect(('127.0.0.1', self.serverSocket.getsockname()[1])) |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 59 | except OSError as e: |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 60 | self.assertEqual(e.args[0], errno.EINPROGRESS) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 61 | else: |
| 62 | raise AssertionError("Connect should have raised EINPROGRESS") |
| 63 | server, addr = self.serverSocket.accept() |
| 64 | |
| 65 | self.connections.extend((client, server)) |
| 66 | return client, server |
| 67 | |
| 68 | def test_create(self): |
| 69 | try: |
| 70 | ep = select.epoll(16) |
| 71 | except OSError as e: |
| 72 | raise AssertionError(str(e)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 73 | self.assertTrue(ep.fileno() > 0, ep.fileno()) |
| 74 | self.assertTrue(not ep.closed) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 75 | ep.close() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 76 | self.assertTrue(ep.closed) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 77 | self.assertRaises(ValueError, ep.fileno) |
Benjamin Peterson | 70069fc | 2011-12-27 16:52:20 -0600 | [diff] [blame] | 78 | if hasattr(select, "EPOLL_CLOEXEC"): |
| 79 | select.epoll(select.EPOLL_CLOEXEC).close() |
| 80 | self.assertRaises(OSError, select.epoll, flags=12356) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 81 | |
| 82 | def test_badcreate(self): |
| 83 | self.assertRaises(TypeError, select.epoll, 1, 2, 3) |
| 84 | self.assertRaises(TypeError, select.epoll, 'foo') |
| 85 | self.assertRaises(TypeError, select.epoll, None) |
| 86 | self.assertRaises(TypeError, select.epoll, ()) |
| 87 | self.assertRaises(TypeError, select.epoll, ['foo']) |
| 88 | self.assertRaises(TypeError, select.epoll, {}) |
| 89 | |
Antoine Pitrou | 09bb89b | 2012-12-15 21:14:21 +0100 | [diff] [blame] | 90 | def test_context_manager(self): |
| 91 | with select.epoll(16) as ep: |
| 92 | self.assertGreater(ep.fileno(), 0) |
| 93 | self.assertFalse(ep.closed) |
| 94 | self.assertTrue(ep.closed) |
| 95 | self.assertRaises(ValueError, ep.fileno) |
| 96 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 97 | def test_add(self): |
| 98 | server, client = self._connected_pair() |
| 99 | |
| 100 | ep = select.epoll(2) |
| 101 | try: |
| 102 | ep.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 103 | ep.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 104 | finally: |
| 105 | ep.close() |
| 106 | |
R. David Murray | 1855c21 | 2009-05-31 19:44:27 +0000 | [diff] [blame] | 107 | # adding by object w/ fileno works, too. |
| 108 | ep = select.epoll(2) |
| 109 | try: |
| 110 | ep.register(server, select.EPOLLIN | select.EPOLLOUT) |
| 111 | ep.register(client, select.EPOLLIN | select.EPOLLOUT) |
| 112 | finally: |
| 113 | ep.close() |
| 114 | |
| 115 | ep = select.epoll(2) |
| 116 | try: |
| 117 | # TypeError: argument must be an int, or have a fileno() method. |
| 118 | self.assertRaises(TypeError, ep.register, object(), |
| 119 | select.EPOLLIN | select.EPOLLOUT) |
| 120 | self.assertRaises(TypeError, ep.register, None, |
| 121 | select.EPOLLIN | select.EPOLLOUT) |
| 122 | # ValueError: file descriptor cannot be a negative integer (-1) |
| 123 | self.assertRaises(ValueError, ep.register, -1, |
| 124 | select.EPOLLIN | select.EPOLLOUT) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 125 | # OSError: [Errno 9] Bad file descriptor |
| 126 | self.assertRaises(OSError, ep.register, 10000, |
R. David Murray | 1855c21 | 2009-05-31 19:44:27 +0000 | [diff] [blame] | 127 | select.EPOLLIN | select.EPOLLOUT) |
| 128 | # registering twice also raises an exception |
| 129 | ep.register(server, select.EPOLLIN | select.EPOLLOUT) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 130 | self.assertRaises(OSError, ep.register, server, |
R. David Murray | 1855c21 | 2009-05-31 19:44:27 +0000 | [diff] [blame] | 131 | select.EPOLLIN | select.EPOLLOUT) |
| 132 | finally: |
| 133 | ep.close() |
| 134 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 135 | def test_fromfd(self): |
| 136 | server, client = self._connected_pair() |
| 137 | |
| 138 | ep = select.epoll(2) |
| 139 | ep2 = select.epoll.fromfd(ep.fileno()) |
| 140 | |
| 141 | ep2.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 142 | ep2.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 143 | |
| 144 | events = ep.poll(1, 4) |
| 145 | events2 = ep2.poll(0.9, 4) |
| 146 | self.assertEqual(len(events), 2) |
| 147 | self.assertEqual(len(events2), 2) |
| 148 | |
| 149 | ep.close() |
| 150 | try: |
| 151 | ep2.poll(1, 4) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 152 | except OSError as e: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 153 | self.assertEqual(e.args[0], errno.EBADF, e) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 154 | else: |
| 155 | self.fail("epoll on closed fd didn't raise EBADF") |
| 156 | |
| 157 | def test_control_and_wait(self): |
| 158 | client, server = self._connected_pair() |
| 159 | |
| 160 | ep = select.epoll(16) |
| 161 | ep.register(server.fileno(), |
| 162 | select.EPOLLIN | select.EPOLLOUT | select.EPOLLET) |
| 163 | ep.register(client.fileno(), |
| 164 | select.EPOLLIN | select.EPOLLOUT | select.EPOLLET) |
| 165 | |
| 166 | now = time.time() |
| 167 | events = ep.poll(1, 4) |
| 168 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 169 | self.assertFalse(then - now > 0.1, then - now) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 170 | |
| 171 | events.sort() |
| 172 | expected = [(client.fileno(), select.EPOLLOUT), |
| 173 | (server.fileno(), select.EPOLLOUT)] |
| 174 | expected.sort() |
| 175 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 176 | self.assertEqual(events, expected) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 177 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 178 | events = ep.poll(timeout=2.1, maxevents=4) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 179 | self.assertFalse(events) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 180 | |
| 181 | client.send(b"Hello!") |
| 182 | server.send(b"world!!!") |
| 183 | |
| 184 | now = time.time() |
| 185 | events = ep.poll(1, 4) |
| 186 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 187 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 188 | |
| 189 | events.sort() |
| 190 | expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT), |
| 191 | (server.fileno(), select.EPOLLIN | select.EPOLLOUT)] |
| 192 | expected.sort() |
| 193 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 194 | self.assertEqual(events, expected) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 195 | |
| 196 | ep.unregister(client.fileno()) |
| 197 | ep.modify(server.fileno(), select.EPOLLOUT) |
| 198 | now = time.time() |
| 199 | events = ep.poll(1, 4) |
| 200 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 201 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 202 | |
| 203 | expected = [(server.fileno(), select.EPOLLOUT)] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 204 | self.assertEqual(events, expected) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 205 | |
| 206 | def test_errors(self): |
| 207 | self.assertRaises(ValueError, select.epoll, -2) |
| 208 | self.assertRaises(ValueError, select.epoll().register, -1, |
| 209 | select.EPOLLIN) |
| 210 | |
| 211 | def test_unregister_closed(self): |
| 212 | server, client = self._connected_pair() |
| 213 | fd = server.fileno() |
| 214 | ep = select.epoll(16) |
| 215 | ep.register(server) |
| 216 | |
| 217 | now = time.time() |
| 218 | events = ep.poll(1, 4) |
| 219 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 220 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 221 | |
| 222 | server.close() |
| 223 | ep.unregister(fd) |
| 224 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 225 | def test_close(self): |
| 226 | open_file = open(__file__, "rb") |
| 227 | self.addCleanup(open_file.close) |
| 228 | fd = open_file.fileno() |
| 229 | epoll = select.epoll() |
| 230 | |
| 231 | # test fileno() method and closed attribute |
| 232 | self.assertIsInstance(epoll.fileno(), int) |
| 233 | self.assertFalse(epoll.closed) |
| 234 | |
| 235 | # test close() |
| 236 | epoll.close() |
| 237 | self.assertTrue(epoll.closed) |
| 238 | self.assertRaises(ValueError, epoll.fileno) |
| 239 | |
| 240 | # close() can be called more than once |
| 241 | epoll.close() |
| 242 | |
| 243 | # operations must fail with ValueError("I/O operation on closed ...") |
| 244 | self.assertRaises(ValueError, epoll.modify, fd, select.EPOLLIN) |
| 245 | self.assertRaises(ValueError, epoll.poll, 1.0) |
| 246 | self.assertRaises(ValueError, epoll.register, fd, select.EPOLLIN) |
| 247 | self.assertRaises(ValueError, epoll.unregister, fd) |
| 248 | |
Victor Stinner | daf4555 | 2013-08-28 00:53:59 +0200 | [diff] [blame] | 249 | def test_fd_non_inheritable(self): |
| 250 | epoll = select.epoll() |
| 251 | self.addCleanup(epoll.close) |
| 252 | self.assertEqual(os.get_inheritable(epoll.fileno()), False) |
| 253 | |
Victor Stinner | 13423c3 | 2013-08-22 00:19:50 +0200 | [diff] [blame] | 254 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 255 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 256 | support.run_unittest(TestEPoll) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 257 | |
| 258 | if __name__ == "__main__": |
| 259 | test_main() |