blob: 549e0f712168be14c5a18bc4b1b00b5213779d00 [file] [log] [blame]
Christian Heimes4fbc72b2008-03-22 00:47:35 +00001# 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"""
22Tests for epoll wrapper.
23"""
Christian Heimes4fbc72b2008-03-22 00:47:35 +000024import errno
Victor Stinnerdaf45552013-08-28 00:53:59 +020025import os
Christian Heimes4fbc72b2008-03-22 00:47:35 +000026import select
Victor Stinnerdaf45552013-08-28 00:53:59 +020027import socket
28import time
Christian Heimes4fbc72b2008-03-22 00:47:35 +000029import unittest
30
Christian Heimes4fbc72b2008-03-22 00:47:35 +000031if not hasattr(select, "epoll"):
Benjamin Petersone549ead2009-03-28 21:42:05 +000032 raise unittest.SkipTest("test works only on Linux 2.6")
Christian Heimes4fbc72b2008-03-22 00:47:35 +000033
Christian Heimesfe337bf2008-03-23 21:54:12 +000034try:
35 select.epoll()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020036except OSError as e:
Christian Heimesfe337bf2008-03-23 21:54:12 +000037 if e.errno == errno.ENOSYS:
Benjamin Petersone549ead2009-03-28 21:42:05 +000038 raise unittest.SkipTest("kernel doesn't support epoll()")
Benjamin Peterson0e613502011-12-27 15:16:34 -060039 raise
Christian Heimesfe337bf2008-03-23 21:54:12 +000040
Christian Heimes4fbc72b2008-03-22 00:47:35 +000041class 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 Natali6e204602014-07-23 19:28:13 +010046 self.serverSocket.listen()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000047 self.connections = [self.serverSocket]
48
Christian Heimes4fbc72b2008-03-22 00:47:35 +000049 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 Svetlov0832af62012-12-18 23:10:48 +020058 except OSError as e:
Ezio Melottib3aedd42010-11-20 19:04:17 +000059 self.assertEqual(e.args[0], errno.EINPROGRESS)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000060 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 Petersonc9c0f202009-06-30 23:06:06 +000072 self.assertTrue(ep.fileno() > 0, ep.fileno())
73 self.assertTrue(not ep.closed)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000074 ep.close()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000075 self.assertTrue(ep.closed)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000076 self.assertRaises(ValueError, ep.fileno)
Benjamin Peterson70069fc2011-12-27 16:52:20 -060077 if hasattr(select, "EPOLL_CLOEXEC"):
78 select.epoll(select.EPOLL_CLOEXEC).close()
Berker Peksage2197d12016-09-26 23:30:41 +030079 select.epoll(flags=select.EPOLL_CLOEXEC).close()
80 select.epoll(flags=0).close()
Benjamin Peterson70069fc2011-12-27 16:52:20 -060081 self.assertRaises(OSError, select.epoll, flags=12356)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000082
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 Pitrou09bb89b2012-12-15 21:14:21 +010091 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 Heimes4fbc72b2008-03-22 00:47:35 +000098 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 Murray1855c212009-05-31 19:44:27 +0000108 # 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 Svetlovf7a17b42012-12-25 16:47:37 +0200126 # OSError: [Errno 9] Bad file descriptor
127 self.assertRaises(OSError, ep.register, 10000,
R. David Murray1855c212009-05-31 19:44:27 +0000128 select.EPOLLIN | select.EPOLLOUT)
129 # registering twice also raises an exception
130 ep.register(server, select.EPOLLIN | select.EPOLLOUT)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200131 self.assertRaises(OSError, ep.register, server,
R. David Murray1855c212009-05-31 19:44:27 +0000132 select.EPOLLIN | select.EPOLLOUT)
133 finally:
134 ep.close()
135
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000136 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 Svetlovf7a17b42012-12-25 16:47:37 +0200153 except OSError as e:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000154 self.assertEqual(e.args[0], errno.EBADF, e)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000155 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 Stinnerb60ac7a2014-03-25 12:50:50 +0100167 now = time.monotonic()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000168 events = ep.poll(1, 4)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100169 then = time.monotonic()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000170 self.assertFalse(then - now > 0.1, then - now)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000171
172 events.sort()
173 expected = [(client.fileno(), select.EPOLLOUT),
174 (server.fileno(), select.EPOLLOUT)]
175 expected.sort()
176
Ezio Melottib3aedd42010-11-20 19:04:17 +0000177 self.assertEqual(events, expected)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000178
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000179 events = ep.poll(timeout=2.1, maxevents=4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000180 self.assertFalse(events)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000181
182 client.send(b"Hello!")
183 server.send(b"world!!!")
184
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100185 now = time.monotonic()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000186 events = ep.poll(1, 4)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100187 then = time.monotonic()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000188 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000189
190 events.sort()
191 expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
192 (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
193 expected.sort()
194
Ezio Melottib3aedd42010-11-20 19:04:17 +0000195 self.assertEqual(events, expected)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000196
197 ep.unregister(client.fileno())
198 ep.modify(server.fileno(), select.EPOLLOUT)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100199 now = time.monotonic()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000200 events = ep.poll(1, 4)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100201 then = time.monotonic()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000202 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000203
204 expected = [(server.fileno(), select.EPOLLOUT)]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000205 self.assertEqual(events, expected)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000206
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 Stinnerb60ac7a2014-03-25 12:50:50 +0100218 now = time.monotonic()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000219 events = ep.poll(1, 4)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100220 then = time.monotonic()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000221 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000222
223 server.close()
224 ep.unregister(fd)
225
Victor Stinner13423c32013-08-22 00:19:50 +0200226 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 Stinnerdaf45552013-08-28 00:53:59 +0200250 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 Stinner13423c32013-08-22 00:19:50 +0200255
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000256if __name__ == "__main__":
Zachary Ware38c707e2015-04-13 15:00:43 -0500257 unittest.main()