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 socket |
| 25 | import errno |
| 26 | import time |
| 27 | import select |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 28 | import unittest |
| 29 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 30 | from test import support |
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() |
| 36 | except IOError as e: |
| 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)) |
| 46 | self.serverSocket.listen(1) |
| 47 | self.connections = [self.serverSocket] |
| 48 | |
| 49 | |
| 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])) |
| 59 | except socket.error 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 | |
| 90 | def test_add(self): |
| 91 | server, client = self._connected_pair() |
| 92 | |
| 93 | ep = select.epoll(2) |
| 94 | try: |
| 95 | ep.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 96 | ep.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 97 | finally: |
| 98 | ep.close() |
| 99 | |
R. David Murray | 1855c21 | 2009-05-31 19:44:27 +0000 | [diff] [blame] | 100 | # adding by object w/ fileno works, too. |
| 101 | ep = select.epoll(2) |
| 102 | try: |
| 103 | ep.register(server, select.EPOLLIN | select.EPOLLOUT) |
| 104 | ep.register(client, select.EPOLLIN | select.EPOLLOUT) |
| 105 | finally: |
| 106 | ep.close() |
| 107 | |
| 108 | ep = select.epoll(2) |
| 109 | try: |
| 110 | # TypeError: argument must be an int, or have a fileno() method. |
| 111 | self.assertRaises(TypeError, ep.register, object(), |
| 112 | select.EPOLLIN | select.EPOLLOUT) |
| 113 | self.assertRaises(TypeError, ep.register, None, |
| 114 | select.EPOLLIN | select.EPOLLOUT) |
| 115 | # ValueError: file descriptor cannot be a negative integer (-1) |
| 116 | self.assertRaises(ValueError, ep.register, -1, |
| 117 | select.EPOLLIN | select.EPOLLOUT) |
| 118 | # IOError: [Errno 9] Bad file descriptor |
| 119 | self.assertRaises(IOError, ep.register, 10000, |
| 120 | select.EPOLLIN | select.EPOLLOUT) |
| 121 | # registering twice also raises an exception |
| 122 | ep.register(server, select.EPOLLIN | select.EPOLLOUT) |
| 123 | self.assertRaises(IOError, ep.register, server, |
| 124 | select.EPOLLIN | select.EPOLLOUT) |
| 125 | finally: |
| 126 | ep.close() |
| 127 | |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 128 | def test_fromfd(self): |
| 129 | server, client = self._connected_pair() |
| 130 | |
| 131 | ep = select.epoll(2) |
| 132 | ep2 = select.epoll.fromfd(ep.fileno()) |
| 133 | |
| 134 | ep2.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 135 | ep2.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT) |
| 136 | |
| 137 | events = ep.poll(1, 4) |
| 138 | events2 = ep2.poll(0.9, 4) |
| 139 | self.assertEqual(len(events), 2) |
| 140 | self.assertEqual(len(events2), 2) |
| 141 | |
| 142 | ep.close() |
| 143 | try: |
| 144 | ep2.poll(1, 4) |
| 145 | except IOError as e: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 146 | self.assertEqual(e.args[0], errno.EBADF, e) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 147 | else: |
| 148 | self.fail("epoll on closed fd didn't raise EBADF") |
| 149 | |
| 150 | def test_control_and_wait(self): |
| 151 | client, server = self._connected_pair() |
| 152 | |
| 153 | ep = select.epoll(16) |
| 154 | ep.register(server.fileno(), |
| 155 | select.EPOLLIN | select.EPOLLOUT | select.EPOLLET) |
| 156 | ep.register(client.fileno(), |
| 157 | select.EPOLLIN | select.EPOLLOUT | select.EPOLLET) |
| 158 | |
| 159 | now = time.time() |
| 160 | events = ep.poll(1, 4) |
| 161 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 162 | self.assertFalse(then - now > 0.1, then - now) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 163 | |
| 164 | events.sort() |
| 165 | expected = [(client.fileno(), select.EPOLLOUT), |
| 166 | (server.fileno(), select.EPOLLOUT)] |
| 167 | expected.sort() |
| 168 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 169 | self.assertEqual(events, expected) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 170 | self.assertFalse(then - now > 0.01, then - now) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 171 | |
| 172 | now = time.time() |
| 173 | events = ep.poll(timeout=2.1, maxevents=4) |
| 174 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 175 | self.assertFalse(events) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 176 | |
| 177 | client.send(b"Hello!") |
| 178 | server.send(b"world!!!") |
| 179 | |
| 180 | now = time.time() |
| 181 | events = ep.poll(1, 4) |
| 182 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 183 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 184 | |
| 185 | events.sort() |
| 186 | expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT), |
| 187 | (server.fileno(), select.EPOLLIN | select.EPOLLOUT)] |
| 188 | expected.sort() |
| 189 | |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 190 | self.assertEqual(events, expected) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 191 | |
| 192 | ep.unregister(client.fileno()) |
| 193 | ep.modify(server.fileno(), select.EPOLLOUT) |
| 194 | now = time.time() |
| 195 | events = ep.poll(1, 4) |
| 196 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 197 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 198 | |
| 199 | expected = [(server.fileno(), select.EPOLLOUT)] |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 200 | self.assertEqual(events, expected) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 201 | |
| 202 | def test_errors(self): |
| 203 | self.assertRaises(ValueError, select.epoll, -2) |
| 204 | self.assertRaises(ValueError, select.epoll().register, -1, |
| 205 | select.EPOLLIN) |
| 206 | |
| 207 | def test_unregister_closed(self): |
| 208 | server, client = self._connected_pair() |
| 209 | fd = server.fileno() |
| 210 | ep = select.epoll(16) |
| 211 | ep.register(server) |
| 212 | |
| 213 | now = time.time() |
| 214 | events = ep.poll(1, 4) |
| 215 | then = time.time() |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 216 | self.assertFalse(then - now > 0.01) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 217 | |
| 218 | server.close() |
| 219 | ep.unregister(fd) |
| 220 | |
| 221 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 222 | support.run_unittest(TestEPoll) |
Christian Heimes | 4fbc72b | 2008-03-22 00:47:35 +0000 | [diff] [blame] | 223 | |
| 224 | if __name__ == "__main__": |
| 225 | test_main() |