blob: d72740376fde2c38b58ed690122cc3407bfbda6a [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +000031from test import support
Christian Heimes4fbc72b2008-03-22 00:47:35 +000032if not hasattr(select, "epoll"):
Benjamin Petersone549ead2009-03-28 21:42:05 +000033 raise unittest.SkipTest("test works only on Linux 2.6")
Christian Heimes4fbc72b2008-03-22 00:47:35 +000034
Christian Heimesfe337bf2008-03-23 21:54:12 +000035try:
36 select.epoll()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020037except OSError as e:
Christian Heimesfe337bf2008-03-23 21:54:12 +000038 if e.errno == errno.ENOSYS:
Benjamin Petersone549ead2009-03-28 21:42:05 +000039 raise unittest.SkipTest("kernel doesn't support epoll()")
Benjamin Peterson0e613502011-12-27 15:16:34 -060040 raise
Christian Heimesfe337bf2008-03-23 21:54:12 +000041
Christian Heimes4fbc72b2008-03-22 00:47:35 +000042class TestEPoll(unittest.TestCase):
43
44 def setUp(self):
45 self.serverSocket = socket.socket()
46 self.serverSocket.bind(('127.0.0.1', 0))
Charles-François Natali6e204602014-07-23 19:28:13 +010047 self.serverSocket.listen()
Christian Heimes4fbc72b2008-03-22 00:47:35 +000048 self.connections = [self.serverSocket]
49
Christian Heimes4fbc72b2008-03-22 00:47:35 +000050 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 Svetlov0832af62012-12-18 23:10:48 +020059 except OSError as e:
Ezio Melottib3aedd42010-11-20 19:04:17 +000060 self.assertEqual(e.args[0], errno.EINPROGRESS)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000061 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 Petersonc9c0f202009-06-30 23:06:06 +000073 self.assertTrue(ep.fileno() > 0, ep.fileno())
74 self.assertTrue(not ep.closed)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000075 ep.close()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000076 self.assertTrue(ep.closed)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000077 self.assertRaises(ValueError, ep.fileno)
Benjamin Peterson70069fc2011-12-27 16:52:20 -060078 if hasattr(select, "EPOLL_CLOEXEC"):
79 select.epoll(select.EPOLL_CLOEXEC).close()
80 self.assertRaises(OSError, select.epoll, flags=12356)
Christian Heimes4fbc72b2008-03-22 00:47:35 +000081
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 Pitrou09bb89b2012-12-15 21:14:21 +010090 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 Heimes4fbc72b2008-03-22 00:47:35 +000097 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 Murray1855c212009-05-31 19:44:27 +0000107 # 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 Svetlovf7a17b42012-12-25 16:47:37 +0200125 # OSError: [Errno 9] Bad file descriptor
126 self.assertRaises(OSError, ep.register, 10000,
R. David Murray1855c212009-05-31 19:44:27 +0000127 select.EPOLLIN | select.EPOLLOUT)
128 # registering twice also raises an exception
129 ep.register(server, select.EPOLLIN | select.EPOLLOUT)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200130 self.assertRaises(OSError, ep.register, server,
R. David Murray1855c212009-05-31 19:44:27 +0000131 select.EPOLLIN | select.EPOLLOUT)
132 finally:
133 ep.close()
134
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000135 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 Svetlovf7a17b42012-12-25 16:47:37 +0200152 except OSError as e:
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000153 self.assertEqual(e.args[0], errno.EBADF, e)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000154 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
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100166 now = time.monotonic()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000167 events = ep.poll(1, 4)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100168 then = time.monotonic()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000169 self.assertFalse(then - now > 0.1, then - now)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000170
171 events.sort()
172 expected = [(client.fileno(), select.EPOLLOUT),
173 (server.fileno(), select.EPOLLOUT)]
174 expected.sort()
175
Ezio Melottib3aedd42010-11-20 19:04:17 +0000176 self.assertEqual(events, expected)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000177
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000178 events = ep.poll(timeout=2.1, maxevents=4)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000179 self.assertFalse(events)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000180
181 client.send(b"Hello!")
182 server.send(b"world!!!")
183
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100184 now = time.monotonic()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000185 events = ep.poll(1, 4)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100186 then = time.monotonic()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000187 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000188
189 events.sort()
190 expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
191 (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
192 expected.sort()
193
Ezio Melottib3aedd42010-11-20 19:04:17 +0000194 self.assertEqual(events, expected)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000195
196 ep.unregister(client.fileno())
197 ep.modify(server.fileno(), select.EPOLLOUT)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100198 now = time.monotonic()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000199 events = ep.poll(1, 4)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100200 then = time.monotonic()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000201 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000202
203 expected = [(server.fileno(), select.EPOLLOUT)]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000204 self.assertEqual(events, expected)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000205
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
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100217 now = time.monotonic()
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000218 events = ep.poll(1, 4)
Victor Stinnerb60ac7a2014-03-25 12:50:50 +0100219 then = time.monotonic()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000220 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000221
222 server.close()
223 ep.unregister(fd)
224
Victor Stinner13423c32013-08-22 00:19:50 +0200225 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 Stinnerdaf45552013-08-28 00:53:59 +0200249 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 Stinner13423c32013-08-22 00:19:50 +0200254
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000255def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000256 support.run_unittest(TestEPoll)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000257
258if __name__ == "__main__":
259 test_main()