1 | n/a | import errno |
---|
2 | n/a | import os |
---|
3 | n/a | import random |
---|
4 | n/a | import selectors |
---|
5 | n/a | import signal |
---|
6 | n/a | import socket |
---|
7 | n/a | import sys |
---|
8 | n/a | from test import support |
---|
9 | n/a | from time import sleep |
---|
10 | n/a | import unittest |
---|
11 | n/a | import unittest.mock |
---|
12 | n/a | import tempfile |
---|
13 | n/a | from time import monotonic as time |
---|
14 | n/a | try: |
---|
15 | n/a | import resource |
---|
16 | n/a | except ImportError: |
---|
17 | n/a | resource = None |
---|
18 | n/a | |
---|
19 | n/a | |
---|
20 | n/a | if hasattr(socket, 'socketpair'): |
---|
21 | n/a | socketpair = socket.socketpair |
---|
22 | n/a | else: |
---|
23 | n/a | def socketpair(family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0): |
---|
24 | n/a | with socket.socket(family, type, proto) as l: |
---|
25 | n/a | l.bind((support.HOST, 0)) |
---|
26 | n/a | l.listen() |
---|
27 | n/a | c = socket.socket(family, type, proto) |
---|
28 | n/a | try: |
---|
29 | n/a | c.connect(l.getsockname()) |
---|
30 | n/a | caddr = c.getsockname() |
---|
31 | n/a | while True: |
---|
32 | n/a | a, addr = l.accept() |
---|
33 | n/a | # check that we've got the correct client |
---|
34 | n/a | if addr == caddr: |
---|
35 | n/a | return c, a |
---|
36 | n/a | a.close() |
---|
37 | n/a | except OSError: |
---|
38 | n/a | c.close() |
---|
39 | n/a | raise |
---|
40 | n/a | |
---|
41 | n/a | |
---|
42 | n/a | def find_ready_matching(ready, flag): |
---|
43 | n/a | match = [] |
---|
44 | n/a | for key, events in ready: |
---|
45 | n/a | if events & flag: |
---|
46 | n/a | match.append(key.fileobj) |
---|
47 | n/a | return match |
---|
48 | n/a | |
---|
49 | n/a | |
---|
50 | n/a | class BaseSelectorTestCase(unittest.TestCase): |
---|
51 | n/a | |
---|
52 | n/a | def make_socketpair(self): |
---|
53 | n/a | rd, wr = socketpair() |
---|
54 | n/a | self.addCleanup(rd.close) |
---|
55 | n/a | self.addCleanup(wr.close) |
---|
56 | n/a | return rd, wr |
---|
57 | n/a | |
---|
58 | n/a | def test_register(self): |
---|
59 | n/a | s = self.SELECTOR() |
---|
60 | n/a | self.addCleanup(s.close) |
---|
61 | n/a | |
---|
62 | n/a | rd, wr = self.make_socketpair() |
---|
63 | n/a | |
---|
64 | n/a | key = s.register(rd, selectors.EVENT_READ, "data") |
---|
65 | n/a | self.assertIsInstance(key, selectors.SelectorKey) |
---|
66 | n/a | self.assertEqual(key.fileobj, rd) |
---|
67 | n/a | self.assertEqual(key.fd, rd.fileno()) |
---|
68 | n/a | self.assertEqual(key.events, selectors.EVENT_READ) |
---|
69 | n/a | self.assertEqual(key.data, "data") |
---|
70 | n/a | |
---|
71 | n/a | # register an unknown event |
---|
72 | n/a | self.assertRaises(ValueError, s.register, 0, 999999) |
---|
73 | n/a | |
---|
74 | n/a | # register an invalid FD |
---|
75 | n/a | self.assertRaises(ValueError, s.register, -10, selectors.EVENT_READ) |
---|
76 | n/a | |
---|
77 | n/a | # register twice |
---|
78 | n/a | self.assertRaises(KeyError, s.register, rd, selectors.EVENT_READ) |
---|
79 | n/a | |
---|
80 | n/a | # register the same FD, but with a different object |
---|
81 | n/a | self.assertRaises(KeyError, s.register, rd.fileno(), |
---|
82 | n/a | selectors.EVENT_READ) |
---|
83 | n/a | |
---|
84 | n/a | def test_unregister(self): |
---|
85 | n/a | s = self.SELECTOR() |
---|
86 | n/a | self.addCleanup(s.close) |
---|
87 | n/a | |
---|
88 | n/a | rd, wr = self.make_socketpair() |
---|
89 | n/a | |
---|
90 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
91 | n/a | s.unregister(rd) |
---|
92 | n/a | |
---|
93 | n/a | # unregister an unknown file obj |
---|
94 | n/a | self.assertRaises(KeyError, s.unregister, 999999) |
---|
95 | n/a | |
---|
96 | n/a | # unregister twice |
---|
97 | n/a | self.assertRaises(KeyError, s.unregister, rd) |
---|
98 | n/a | |
---|
99 | n/a | def test_unregister_after_fd_close(self): |
---|
100 | n/a | s = self.SELECTOR() |
---|
101 | n/a | self.addCleanup(s.close) |
---|
102 | n/a | rd, wr = self.make_socketpair() |
---|
103 | n/a | r, w = rd.fileno(), wr.fileno() |
---|
104 | n/a | s.register(r, selectors.EVENT_READ) |
---|
105 | n/a | s.register(w, selectors.EVENT_WRITE) |
---|
106 | n/a | rd.close() |
---|
107 | n/a | wr.close() |
---|
108 | n/a | s.unregister(r) |
---|
109 | n/a | s.unregister(w) |
---|
110 | n/a | |
---|
111 | n/a | @unittest.skipUnless(os.name == 'posix', "requires posix") |
---|
112 | n/a | def test_unregister_after_fd_close_and_reuse(self): |
---|
113 | n/a | s = self.SELECTOR() |
---|
114 | n/a | self.addCleanup(s.close) |
---|
115 | n/a | rd, wr = self.make_socketpair() |
---|
116 | n/a | r, w = rd.fileno(), wr.fileno() |
---|
117 | n/a | s.register(r, selectors.EVENT_READ) |
---|
118 | n/a | s.register(w, selectors.EVENT_WRITE) |
---|
119 | n/a | rd2, wr2 = self.make_socketpair() |
---|
120 | n/a | rd.close() |
---|
121 | n/a | wr.close() |
---|
122 | n/a | os.dup2(rd2.fileno(), r) |
---|
123 | n/a | os.dup2(wr2.fileno(), w) |
---|
124 | n/a | self.addCleanup(os.close, r) |
---|
125 | n/a | self.addCleanup(os.close, w) |
---|
126 | n/a | s.unregister(r) |
---|
127 | n/a | s.unregister(w) |
---|
128 | n/a | |
---|
129 | n/a | def test_unregister_after_socket_close(self): |
---|
130 | n/a | s = self.SELECTOR() |
---|
131 | n/a | self.addCleanup(s.close) |
---|
132 | n/a | rd, wr = self.make_socketpair() |
---|
133 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
134 | n/a | s.register(wr, selectors.EVENT_WRITE) |
---|
135 | n/a | rd.close() |
---|
136 | n/a | wr.close() |
---|
137 | n/a | s.unregister(rd) |
---|
138 | n/a | s.unregister(wr) |
---|
139 | n/a | |
---|
140 | n/a | def test_modify(self): |
---|
141 | n/a | s = self.SELECTOR() |
---|
142 | n/a | self.addCleanup(s.close) |
---|
143 | n/a | |
---|
144 | n/a | rd, wr = self.make_socketpair() |
---|
145 | n/a | |
---|
146 | n/a | key = s.register(rd, selectors.EVENT_READ) |
---|
147 | n/a | |
---|
148 | n/a | # modify events |
---|
149 | n/a | key2 = s.modify(rd, selectors.EVENT_WRITE) |
---|
150 | n/a | self.assertNotEqual(key.events, key2.events) |
---|
151 | n/a | self.assertEqual(key2, s.get_key(rd)) |
---|
152 | n/a | |
---|
153 | n/a | s.unregister(rd) |
---|
154 | n/a | |
---|
155 | n/a | # modify data |
---|
156 | n/a | d1 = object() |
---|
157 | n/a | d2 = object() |
---|
158 | n/a | |
---|
159 | n/a | key = s.register(rd, selectors.EVENT_READ, d1) |
---|
160 | n/a | key2 = s.modify(rd, selectors.EVENT_READ, d2) |
---|
161 | n/a | self.assertEqual(key.events, key2.events) |
---|
162 | n/a | self.assertNotEqual(key.data, key2.data) |
---|
163 | n/a | self.assertEqual(key2, s.get_key(rd)) |
---|
164 | n/a | self.assertEqual(key2.data, d2) |
---|
165 | n/a | |
---|
166 | n/a | # modify unknown file obj |
---|
167 | n/a | self.assertRaises(KeyError, s.modify, 999999, selectors.EVENT_READ) |
---|
168 | n/a | |
---|
169 | n/a | # modify use a shortcut |
---|
170 | n/a | d3 = object() |
---|
171 | n/a | s.register = unittest.mock.Mock() |
---|
172 | n/a | s.unregister = unittest.mock.Mock() |
---|
173 | n/a | |
---|
174 | n/a | s.modify(rd, selectors.EVENT_READ, d3) |
---|
175 | n/a | self.assertFalse(s.register.called) |
---|
176 | n/a | self.assertFalse(s.unregister.called) |
---|
177 | n/a | |
---|
178 | n/a | def test_close(self): |
---|
179 | n/a | s = self.SELECTOR() |
---|
180 | n/a | self.addCleanup(s.close) |
---|
181 | n/a | |
---|
182 | n/a | mapping = s.get_map() |
---|
183 | n/a | rd, wr = self.make_socketpair() |
---|
184 | n/a | |
---|
185 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
186 | n/a | s.register(wr, selectors.EVENT_WRITE) |
---|
187 | n/a | |
---|
188 | n/a | s.close() |
---|
189 | n/a | self.assertRaises(RuntimeError, s.get_key, rd) |
---|
190 | n/a | self.assertRaises(RuntimeError, s.get_key, wr) |
---|
191 | n/a | self.assertRaises(KeyError, mapping.__getitem__, rd) |
---|
192 | n/a | self.assertRaises(KeyError, mapping.__getitem__, wr) |
---|
193 | n/a | |
---|
194 | n/a | def test_get_key(self): |
---|
195 | n/a | s = self.SELECTOR() |
---|
196 | n/a | self.addCleanup(s.close) |
---|
197 | n/a | |
---|
198 | n/a | rd, wr = self.make_socketpair() |
---|
199 | n/a | |
---|
200 | n/a | key = s.register(rd, selectors.EVENT_READ, "data") |
---|
201 | n/a | self.assertEqual(key, s.get_key(rd)) |
---|
202 | n/a | |
---|
203 | n/a | # unknown file obj |
---|
204 | n/a | self.assertRaises(KeyError, s.get_key, 999999) |
---|
205 | n/a | |
---|
206 | n/a | def test_get_map(self): |
---|
207 | n/a | s = self.SELECTOR() |
---|
208 | n/a | self.addCleanup(s.close) |
---|
209 | n/a | |
---|
210 | n/a | rd, wr = self.make_socketpair() |
---|
211 | n/a | |
---|
212 | n/a | keys = s.get_map() |
---|
213 | n/a | self.assertFalse(keys) |
---|
214 | n/a | self.assertEqual(len(keys), 0) |
---|
215 | n/a | self.assertEqual(list(keys), []) |
---|
216 | n/a | key = s.register(rd, selectors.EVENT_READ, "data") |
---|
217 | n/a | self.assertIn(rd, keys) |
---|
218 | n/a | self.assertEqual(key, keys[rd]) |
---|
219 | n/a | self.assertEqual(len(keys), 1) |
---|
220 | n/a | self.assertEqual(list(keys), [rd.fileno()]) |
---|
221 | n/a | self.assertEqual(list(keys.values()), [key]) |
---|
222 | n/a | |
---|
223 | n/a | # unknown file obj |
---|
224 | n/a | with self.assertRaises(KeyError): |
---|
225 | n/a | keys[999999] |
---|
226 | n/a | |
---|
227 | n/a | # Read-only mapping |
---|
228 | n/a | with self.assertRaises(TypeError): |
---|
229 | n/a | del keys[rd] |
---|
230 | n/a | |
---|
231 | n/a | def test_select(self): |
---|
232 | n/a | s = self.SELECTOR() |
---|
233 | n/a | self.addCleanup(s.close) |
---|
234 | n/a | |
---|
235 | n/a | rd, wr = self.make_socketpair() |
---|
236 | n/a | |
---|
237 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
238 | n/a | wr_key = s.register(wr, selectors.EVENT_WRITE) |
---|
239 | n/a | |
---|
240 | n/a | result = s.select() |
---|
241 | n/a | for key, events in result: |
---|
242 | n/a | self.assertTrue(isinstance(key, selectors.SelectorKey)) |
---|
243 | n/a | self.assertTrue(events) |
---|
244 | n/a | self.assertFalse(events & ~(selectors.EVENT_READ | |
---|
245 | n/a | selectors.EVENT_WRITE)) |
---|
246 | n/a | |
---|
247 | n/a | self.assertEqual([(wr_key, selectors.EVENT_WRITE)], result) |
---|
248 | n/a | |
---|
249 | n/a | def test_context_manager(self): |
---|
250 | n/a | s = self.SELECTOR() |
---|
251 | n/a | self.addCleanup(s.close) |
---|
252 | n/a | |
---|
253 | n/a | rd, wr = self.make_socketpair() |
---|
254 | n/a | |
---|
255 | n/a | with s as sel: |
---|
256 | n/a | sel.register(rd, selectors.EVENT_READ) |
---|
257 | n/a | sel.register(wr, selectors.EVENT_WRITE) |
---|
258 | n/a | |
---|
259 | n/a | self.assertRaises(RuntimeError, s.get_key, rd) |
---|
260 | n/a | self.assertRaises(RuntimeError, s.get_key, wr) |
---|
261 | n/a | |
---|
262 | n/a | def test_fileno(self): |
---|
263 | n/a | s = self.SELECTOR() |
---|
264 | n/a | self.addCleanup(s.close) |
---|
265 | n/a | |
---|
266 | n/a | if hasattr(s, 'fileno'): |
---|
267 | n/a | fd = s.fileno() |
---|
268 | n/a | self.assertTrue(isinstance(fd, int)) |
---|
269 | n/a | self.assertGreaterEqual(fd, 0) |
---|
270 | n/a | |
---|
271 | n/a | def test_selector(self): |
---|
272 | n/a | s = self.SELECTOR() |
---|
273 | n/a | self.addCleanup(s.close) |
---|
274 | n/a | |
---|
275 | n/a | NUM_SOCKETS = 12 |
---|
276 | n/a | MSG = b" This is a test." |
---|
277 | n/a | MSG_LEN = len(MSG) |
---|
278 | n/a | readers = [] |
---|
279 | n/a | writers = [] |
---|
280 | n/a | r2w = {} |
---|
281 | n/a | w2r = {} |
---|
282 | n/a | |
---|
283 | n/a | for i in range(NUM_SOCKETS): |
---|
284 | n/a | rd, wr = self.make_socketpair() |
---|
285 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
286 | n/a | s.register(wr, selectors.EVENT_WRITE) |
---|
287 | n/a | readers.append(rd) |
---|
288 | n/a | writers.append(wr) |
---|
289 | n/a | r2w[rd] = wr |
---|
290 | n/a | w2r[wr] = rd |
---|
291 | n/a | |
---|
292 | n/a | bufs = [] |
---|
293 | n/a | |
---|
294 | n/a | while writers: |
---|
295 | n/a | ready = s.select() |
---|
296 | n/a | ready_writers = find_ready_matching(ready, selectors.EVENT_WRITE) |
---|
297 | n/a | if not ready_writers: |
---|
298 | n/a | self.fail("no sockets ready for writing") |
---|
299 | n/a | wr = random.choice(ready_writers) |
---|
300 | n/a | wr.send(MSG) |
---|
301 | n/a | |
---|
302 | n/a | for i in range(10): |
---|
303 | n/a | ready = s.select() |
---|
304 | n/a | ready_readers = find_ready_matching(ready, |
---|
305 | n/a | selectors.EVENT_READ) |
---|
306 | n/a | if ready_readers: |
---|
307 | n/a | break |
---|
308 | n/a | # there might be a delay between the write to the write end and |
---|
309 | n/a | # the read end is reported ready |
---|
310 | n/a | sleep(0.1) |
---|
311 | n/a | else: |
---|
312 | n/a | self.fail("no sockets ready for reading") |
---|
313 | n/a | self.assertEqual([w2r[wr]], ready_readers) |
---|
314 | n/a | rd = ready_readers[0] |
---|
315 | n/a | buf = rd.recv(MSG_LEN) |
---|
316 | n/a | self.assertEqual(len(buf), MSG_LEN) |
---|
317 | n/a | bufs.append(buf) |
---|
318 | n/a | s.unregister(r2w[rd]) |
---|
319 | n/a | s.unregister(rd) |
---|
320 | n/a | writers.remove(r2w[rd]) |
---|
321 | n/a | |
---|
322 | n/a | self.assertEqual(bufs, [MSG] * NUM_SOCKETS) |
---|
323 | n/a | |
---|
324 | n/a | @unittest.skipIf(sys.platform == 'win32', |
---|
325 | n/a | 'select.select() cannot be used with empty fd sets') |
---|
326 | n/a | def test_empty_select(self): |
---|
327 | n/a | # Issue #23009: Make sure EpollSelector.select() works when no FD is |
---|
328 | n/a | # registered. |
---|
329 | n/a | s = self.SELECTOR() |
---|
330 | n/a | self.addCleanup(s.close) |
---|
331 | n/a | self.assertEqual(s.select(timeout=0), []) |
---|
332 | n/a | |
---|
333 | n/a | def test_timeout(self): |
---|
334 | n/a | s = self.SELECTOR() |
---|
335 | n/a | self.addCleanup(s.close) |
---|
336 | n/a | |
---|
337 | n/a | rd, wr = self.make_socketpair() |
---|
338 | n/a | |
---|
339 | n/a | s.register(wr, selectors.EVENT_WRITE) |
---|
340 | n/a | t = time() |
---|
341 | n/a | self.assertEqual(1, len(s.select(0))) |
---|
342 | n/a | self.assertEqual(1, len(s.select(-1))) |
---|
343 | n/a | self.assertLess(time() - t, 0.5) |
---|
344 | n/a | |
---|
345 | n/a | s.unregister(wr) |
---|
346 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
347 | n/a | t = time() |
---|
348 | n/a | self.assertFalse(s.select(0)) |
---|
349 | n/a | self.assertFalse(s.select(-1)) |
---|
350 | n/a | self.assertLess(time() - t, 0.5) |
---|
351 | n/a | |
---|
352 | n/a | t0 = time() |
---|
353 | n/a | self.assertFalse(s.select(1)) |
---|
354 | n/a | t1 = time() |
---|
355 | n/a | dt = t1 - t0 |
---|
356 | n/a | # Tolerate 2.0 seconds for very slow buildbots |
---|
357 | n/a | self.assertTrue(0.8 <= dt <= 2.0, dt) |
---|
358 | n/a | |
---|
359 | n/a | @unittest.skipUnless(hasattr(signal, "alarm"), |
---|
360 | n/a | "signal.alarm() required for this test") |
---|
361 | n/a | def test_select_interrupt_exc(self): |
---|
362 | n/a | s = self.SELECTOR() |
---|
363 | n/a | self.addCleanup(s.close) |
---|
364 | n/a | |
---|
365 | n/a | rd, wr = self.make_socketpair() |
---|
366 | n/a | |
---|
367 | n/a | class InterruptSelect(Exception): |
---|
368 | n/a | pass |
---|
369 | n/a | |
---|
370 | n/a | def handler(*args): |
---|
371 | n/a | raise InterruptSelect |
---|
372 | n/a | |
---|
373 | n/a | orig_alrm_handler = signal.signal(signal.SIGALRM, handler) |
---|
374 | n/a | self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) |
---|
375 | n/a | self.addCleanup(signal.alarm, 0) |
---|
376 | n/a | |
---|
377 | n/a | signal.alarm(1) |
---|
378 | n/a | |
---|
379 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
380 | n/a | t = time() |
---|
381 | n/a | # select() is interrupted by a signal which raises an exception |
---|
382 | n/a | with self.assertRaises(InterruptSelect): |
---|
383 | n/a | s.select(30) |
---|
384 | n/a | # select() was interrupted before the timeout of 30 seconds |
---|
385 | n/a | self.assertLess(time() - t, 5.0) |
---|
386 | n/a | |
---|
387 | n/a | @unittest.skipUnless(hasattr(signal, "alarm"), |
---|
388 | n/a | "signal.alarm() required for this test") |
---|
389 | n/a | def test_select_interrupt_noraise(self): |
---|
390 | n/a | s = self.SELECTOR() |
---|
391 | n/a | self.addCleanup(s.close) |
---|
392 | n/a | |
---|
393 | n/a | rd, wr = self.make_socketpair() |
---|
394 | n/a | |
---|
395 | n/a | orig_alrm_handler = signal.signal(signal.SIGALRM, lambda *args: None) |
---|
396 | n/a | self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) |
---|
397 | n/a | self.addCleanup(signal.alarm, 0) |
---|
398 | n/a | |
---|
399 | n/a | signal.alarm(1) |
---|
400 | n/a | |
---|
401 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
402 | n/a | t = time() |
---|
403 | n/a | # select() is interrupted by a signal, but the signal handler doesn't |
---|
404 | n/a | # raise an exception, so select() should by retries with a recomputed |
---|
405 | n/a | # timeout |
---|
406 | n/a | self.assertFalse(s.select(1.5)) |
---|
407 | n/a | self.assertGreaterEqual(time() - t, 1.0) |
---|
408 | n/a | |
---|
409 | n/a | |
---|
410 | n/a | class ScalableSelectorMixIn: |
---|
411 | n/a | |
---|
412 | n/a | # see issue #18963 for why it's skipped on older OS X versions |
---|
413 | n/a | @support.requires_mac_ver(10, 5) |
---|
414 | n/a | @unittest.skipUnless(resource, "Test needs resource module") |
---|
415 | n/a | def test_above_fd_setsize(self): |
---|
416 | n/a | # A scalable implementation should have no problem with more than |
---|
417 | n/a | # FD_SETSIZE file descriptors. Since we don't know the value, we just |
---|
418 | n/a | # try to set the soft RLIMIT_NOFILE to the hard RLIMIT_NOFILE ceiling. |
---|
419 | n/a | soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) |
---|
420 | n/a | try: |
---|
421 | n/a | resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) |
---|
422 | n/a | self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE, |
---|
423 | n/a | (soft, hard)) |
---|
424 | n/a | NUM_FDS = min(hard, 2**16) |
---|
425 | n/a | except (OSError, ValueError): |
---|
426 | n/a | NUM_FDS = soft |
---|
427 | n/a | |
---|
428 | n/a | # guard for already allocated FDs (stdin, stdout...) |
---|
429 | n/a | NUM_FDS -= 32 |
---|
430 | n/a | |
---|
431 | n/a | s = self.SELECTOR() |
---|
432 | n/a | self.addCleanup(s.close) |
---|
433 | n/a | |
---|
434 | n/a | for i in range(NUM_FDS // 2): |
---|
435 | n/a | try: |
---|
436 | n/a | rd, wr = self.make_socketpair() |
---|
437 | n/a | except OSError: |
---|
438 | n/a | # too many FDs, skip - note that we should only catch EMFILE |
---|
439 | n/a | # here, but apparently *BSD and Solaris can fail upon connect() |
---|
440 | n/a | # or bind() with EADDRNOTAVAIL, so let's be safe |
---|
441 | n/a | self.skipTest("FD limit reached") |
---|
442 | n/a | |
---|
443 | n/a | try: |
---|
444 | n/a | s.register(rd, selectors.EVENT_READ) |
---|
445 | n/a | s.register(wr, selectors.EVENT_WRITE) |
---|
446 | n/a | except OSError as e: |
---|
447 | n/a | if e.errno == errno.ENOSPC: |
---|
448 | n/a | # this can be raised by epoll if we go over |
---|
449 | n/a | # fs.epoll.max_user_watches sysctl |
---|
450 | n/a | self.skipTest("FD limit reached") |
---|
451 | n/a | raise |
---|
452 | n/a | |
---|
453 | n/a | self.assertEqual(NUM_FDS // 2, len(s.select())) |
---|
454 | n/a | |
---|
455 | n/a | |
---|
456 | n/a | class DefaultSelectorTestCase(BaseSelectorTestCase): |
---|
457 | n/a | |
---|
458 | n/a | SELECTOR = selectors.DefaultSelector |
---|
459 | n/a | |
---|
460 | n/a | |
---|
461 | n/a | class SelectSelectorTestCase(BaseSelectorTestCase): |
---|
462 | n/a | |
---|
463 | n/a | SELECTOR = selectors.SelectSelector |
---|
464 | n/a | |
---|
465 | n/a | |
---|
466 | n/a | @unittest.skipUnless(hasattr(selectors, 'PollSelector'), |
---|
467 | n/a | "Test needs selectors.PollSelector") |
---|
468 | n/a | class PollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn): |
---|
469 | n/a | |
---|
470 | n/a | SELECTOR = getattr(selectors, 'PollSelector', None) |
---|
471 | n/a | |
---|
472 | n/a | |
---|
473 | n/a | @unittest.skipUnless(hasattr(selectors, 'EpollSelector'), |
---|
474 | n/a | "Test needs selectors.EpollSelector") |
---|
475 | n/a | class EpollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn): |
---|
476 | n/a | |
---|
477 | n/a | SELECTOR = getattr(selectors, 'EpollSelector', None) |
---|
478 | n/a | |
---|
479 | n/a | def test_register_file(self): |
---|
480 | n/a | # epoll(7) returns EPERM when given a file to watch |
---|
481 | n/a | s = self.SELECTOR() |
---|
482 | n/a | with tempfile.NamedTemporaryFile() as f: |
---|
483 | n/a | with self.assertRaises(IOError): |
---|
484 | n/a | s.register(f, selectors.EVENT_READ) |
---|
485 | n/a | # the SelectorKey has been removed |
---|
486 | n/a | with self.assertRaises(KeyError): |
---|
487 | n/a | s.get_key(f) |
---|
488 | n/a | |
---|
489 | n/a | |
---|
490 | n/a | @unittest.skipUnless(hasattr(selectors, 'KqueueSelector'), |
---|
491 | n/a | "Test needs selectors.KqueueSelector)") |
---|
492 | n/a | class KqueueSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn): |
---|
493 | n/a | |
---|
494 | n/a | SELECTOR = getattr(selectors, 'KqueueSelector', None) |
---|
495 | n/a | |
---|
496 | n/a | def test_register_bad_fd(self): |
---|
497 | n/a | # a file descriptor that's been closed should raise an OSError |
---|
498 | n/a | # with EBADF |
---|
499 | n/a | s = self.SELECTOR() |
---|
500 | n/a | bad_f = support.make_bad_fd() |
---|
501 | n/a | with self.assertRaises(OSError) as cm: |
---|
502 | n/a | s.register(bad_f, selectors.EVENT_READ) |
---|
503 | n/a | self.assertEqual(cm.exception.errno, errno.EBADF) |
---|
504 | n/a | # the SelectorKey has been removed |
---|
505 | n/a | with self.assertRaises(KeyError): |
---|
506 | n/a | s.get_key(bad_f) |
---|
507 | n/a | |
---|
508 | n/a | |
---|
509 | n/a | @unittest.skipUnless(hasattr(selectors, 'DevpollSelector'), |
---|
510 | n/a | "Test needs selectors.DevpollSelector") |
---|
511 | n/a | class DevpollSelectorTestCase(BaseSelectorTestCase, ScalableSelectorMixIn): |
---|
512 | n/a | |
---|
513 | n/a | SELECTOR = getattr(selectors, 'DevpollSelector', None) |
---|
514 | n/a | |
---|
515 | n/a | |
---|
516 | n/a | |
---|
517 | n/a | def test_main(): |
---|
518 | n/a | tests = [DefaultSelectorTestCase, SelectSelectorTestCase, |
---|
519 | n/a | PollSelectorTestCase, EpollSelectorTestCase, |
---|
520 | n/a | KqueueSelectorTestCase, DevpollSelectorTestCase] |
---|
521 | n/a | support.run_unittest(*tests) |
---|
522 | n/a | support.reap_children() |
---|
523 | n/a | |
---|
524 | n/a | |
---|
525 | n/a | if __name__ == "__main__": |
---|
526 | n/a | test_main() |
---|