blob: 7f9547ff959316399344a3ca20e0b6e3c6c57798 [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 socket
25import errno
26import time
27import select
Christian Heimes4fbc72b2008-03-22 00:47:35 +000028import unittest
29
Benjamin Petersonee8712c2008-05-20 21:35:26 +000030from test import support
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()
36except IOError as e:
37 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))
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 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
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 Murray1855c212009-05-31 19:44:27 +0000100 # 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 Heimes4fbc72b2008-03-22 00:47:35 +0000128 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 Petersonc9c0f202009-06-30 23:06:06 +0000146 self.assertEqual(e.args[0], errno.EBADF, e)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000147 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 Petersonc9c0f202009-06-30 23:06:06 +0000162 self.assertFalse(then - now > 0.1, then - now)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000163
164 events.sort()
165 expected = [(client.fileno(), select.EPOLLOUT),
166 (server.fileno(), select.EPOLLOUT)]
167 expected.sort()
168
Ezio Melottib3aedd42010-11-20 19:04:17 +0000169 self.assertEqual(events, expected)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000170 self.assertFalse(then - now > 0.01, then - now)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000171
172 now = time.time()
173 events = ep.poll(timeout=2.1, maxevents=4)
174 then = time.time()
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000175 self.assertFalse(events)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000176
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 Petersonc9c0f202009-06-30 23:06:06 +0000183 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000184
185 events.sort()
186 expected = [(client.fileno(), select.EPOLLIN | select.EPOLLOUT),
187 (server.fileno(), select.EPOLLIN | select.EPOLLOUT)]
188 expected.sort()
189
Ezio Melottib3aedd42010-11-20 19:04:17 +0000190 self.assertEqual(events, expected)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000191
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 Petersonc9c0f202009-06-30 23:06:06 +0000197 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000198
199 expected = [(server.fileno(), select.EPOLLOUT)]
Ezio Melottib3aedd42010-11-20 19:04:17 +0000200 self.assertEqual(events, expected)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000201
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 Petersonc9c0f202009-06-30 23:06:06 +0000216 self.assertFalse(then - now > 0.01)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000217
218 server.close()
219 ep.unregister(fd)
220
221def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000222 support.run_unittest(TestEPoll)
Christian Heimes4fbc72b2008-03-22 00:47:35 +0000223
224if __name__ == "__main__":
225 test_main()