Debian Codegraph @

pypy/module/select/test/test_epoll.py

raw symbols SCIP
1 import py
2 import sys
3
4 # add a larger timeout for slow ARM machines
5 import platform
6
7
8 class AppTestEpoll(object):
9 spaceconfig = {
10 "usemodules": ["select", "_socket", "posix", "time"],
11 }
12
13 def setup_class(cls):
14 # NB. we should ideally py.test.skip() if running on an old linux
15 # where the kernel doesn't support epoll()
16 if not sys.platform.startswith('linux'):
17 py.test.skip("test requires linux (assumed >= 2.6)")
18
19 def setup_method(self, meth):
20 self.w_sockets = self.space.wrap([])
21 if platform.machine().startswith('arm'):
22 self.w_timeout = self.space.wrap(0.06)
23 if platform.machine().startswith('s390x'):
24 # s390x is not slow, but it seems there is one case when epoll
25 # modify method is called that takes longer on s390x
26 self.w_timeout = self.space.wrap(0.06)
27 else:
28 self.w_timeout = self.space.wrap(0.02)
29
30 def teardown_method(self, meth):
31 for socket in self.space.unpackiterable(self.w_sockets):
32 self.space.call_method(socket, "close")
33
34 def w_socket_pair(self):
35 import _socket as socket
36
37 server_socket = socket.socket()
38 server_socket.bind(('127.0.0.1', 0))
39 server_socket.listen(1)
40 client = socket.socket()
41 client.setblocking(False)
42 raises(socket.error,
43 client.connect, ('127.0.0.1', server_socket.getsockname()[1])
44 )
45 fd, addr = server_socket._accept()
46 server = socket.socket(server_socket.family, server_socket.type,
47 server_socket.proto, fileno=fd)
48
49 self.sockets.extend([server_socket, client, server])
50 return client, server
51
52 def test_create(self):
53 import select
54
55 ep = select.epoll(16)
56 assert ep.fileno() > 0
57 assert not ep.closed
58 ep.close()
59 assert ep.closed
60 raises(ValueError, ep.fileno)
61
62 def test_with(self):
63 import select
64
65 ep = select.epoll(16)
66 assert ep.fileno() > 0
67 with ep:
68 assert not ep.closed
69 assert ep.closed
70 raises(ValueError, ep.__enter__)
71
72 def test_badcreate(self):
73 import select
74
75 raises(TypeError, select.epoll, 1, 2, 3)
76 raises(TypeError, select.epoll, 'foo')
77 raises(TypeError, select.epoll, None)
78 raises(TypeError, select.epoll, ())
79 raises(TypeError, select.epoll, ['foo'])
80 raises(TypeError, select.epoll, {})
81
82 def test_add(self):
83 import select
84
85 client, server = self.socket_pair()
86
87 ep = select.epoll(2)
88 ep.register(server, select.EPOLLIN | select.EPOLLOUT)
89 ep.register(client, select.EPOLLIN | select.EPOLLOUT)
90 ep.close()
91
92 # adding by object w/ fileno works, too.
93 ep = select.epoll(2)
94 ep.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT)
95 ep.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT)
96 ep.close()
97
98 ep = select.epoll(2)
99 # TypeError: argument must be an int, or have a fileno() method.
100 raises(TypeError, ep.register, object(), select.EPOLLIN | select.EPOLLOUT)
101 raises(TypeError, ep.register, None, select.EPOLLIN | select.EPOLLOUT)
102 # ValueError: file descriptor cannot be a negative integer (-1)
103 raises(ValueError, ep.register, -1, select.EPOLLIN | select.EPOLLOUT)
104 # IOError: [Errno 9] Bad file descriptor
105 raises(IOError, ep.register, 10000, select.EPOLLIN | select.EPOLLOUT)
106 # registering twice also raises an exception
107 ep.register(server, select.EPOLLIN | select.EPOLLOUT)
108 raises(IOError, ep.register, server, select.EPOLLIN | select.EPOLLOUT)
109 ep.close()
110
111 def test_fromfd(self):
112 import errno
113 import select
114
115 client, server = self.socket_pair()
116
117 ep1 = select.epoll(2)
118 ep2 = select.epoll.fromfd(ep1.fileno())
119
120 ep2.register(server.fileno(), select.EPOLLIN | select.EPOLLOUT)
121 ep2.register(client.fileno(), select.EPOLLIN | select.EPOLLOUT)
122
123 events1 = ep1.poll(1, 4)
124 events2 = ep2.poll(0.9, 4)
125 assert len(events1) == 2
126 assert len(events2) == 2
127 ep1.close()
128
129 exc_info = raises(IOError, ep2.poll, 1, 4)
130 assert exc_info.value.args[0] == errno.EBADF
131
132 def test_control_and_wait(self):
133 import select
134 import time
135
136 client, server = self.socket_pair()
137
138 ep = select.epoll(16)
139 ep.register(server.fileno(),
140 select.EPOLLIN | select.EPOLLOUT | select.EPOLLET
141 )
142 ep.register(client.fileno(),
143 select.EPOLLIN | select.EPOLLOUT | select.EPOLLET
144 )
145
146 now = time.time()
147 events = ep.poll(1, 4)
148 then = time.time()
149 assert then - now < 0.1
150
151 events.sort()
152 expected = [
153 (client.fileno(), select.EPOLLOUT),
154 (server.fileno(), select.EPOLLOUT)
155 ]
156 expected.sort()
157
158 assert events == expected
159 assert then - now < self.timeout
160
161 now = time.time()
162 events = ep.poll(timeout=2.1, maxevents=4)
163 then = time.time()
164 assert not events
165
166 client.send(b"Hello!")
167 server.send(b"world!!!")
168
169 now = time.time()
170 events = ep.poll(1, 4)
171 then = time.time()
172 assert then - now < self.timeout
173
174 events.sort()
175 expected = [
176 (client.fileno(), select.EPOLLIN | select.EPOLLOUT),
177 (server.fileno(), select.EPOLLIN | select.EPOLLOUT)
178 ]
179 expected.sort()
180
181 assert events == expected
182
183 ep.unregister(client.fileno())
184 ep.modify(server.fileno(), select.EPOLLOUT)
185
186 now = time.time()
187 events = ep.poll(1, 4)
188 then = time.time()
189 assert then - now < self.timeout
190
191 expected = [(server.fileno(), select.EPOLLOUT)]
192 assert events == expected
193
194 def test_errors(self):
195 import select
196
197 raises(ValueError, select.epoll, -2)
198 raises(ValueError, select.epoll().register, -1, select.EPOLLIN)
199
200 def test_unregister_closed(self):
201 import select
202 import time
203 import errno
204
205 client, server = self.socket_pair()
206
207 fd = server.fileno()
208 ep = select.epoll(16)
209 ep.register(server)
210
211 now = time.time()
212 ep.poll(1, 4)
213 then = time.time()
214 assert then - now < self.timeout
215
216 server.close()
217 with raises(OSError) as cm:
218 ep.unregister(fd)
219 assert cm.value.errno == errno.EBADF
220
221 def test_close_twice(self):
222 import select
223
224 ep = select.epoll()
225 ep.close()
226 ep.close()
227
228 def test_non_inheritable(self):
229 import select, posix
230
231 ep = select.epoll()
232 assert posix.get_inheritable(ep.fileno()) == False
233 ep.close()