Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(86019)

Delta Between Two Patch Sets: Lib/test/test_selectors.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 5 years, 9 months ago
Right Patch Set: Created 5 years, 6 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_sax.py ('k') | Lib/test/test_shutil.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 import errno 1 import errno
2 import os
2 import random 3 import random
3 import selectors 4 import selectors
4 import signal 5 import signal
5 import socket 6 import socket
6 from test import support 7 from test import support
7 from time import sleep 8 from time import sleep
8 import unittest 9 import unittest
9 import unittest.mock 10 import unittest.mock
10 try: 11 try:
11 from time import monotonic as time 12 from time import monotonic as time
(...skipping 30 matching lines...) Expand all
42 def find_ready_matching(ready, flag): 43 def find_ready_matching(ready, flag):
43 match = [] 44 match = []
44 for key, events in ready: 45 for key, events in ready:
45 if events & flag: 46 if events & flag:
46 match.append(key.fileobj) 47 match.append(key.fileobj)
47 return match 48 return match
48 49
49 50
50 class BaseSelectorTestCase(unittest.TestCase): 51 class BaseSelectorTestCase(unittest.TestCase):
51 52
52 def test_register(self): 53 def make_socketpair(self):
53 s = self.SELECTOR()
54 self.addCleanup(s.close)
55
56 rd, wr = socketpair() 54 rd, wr = socketpair()
57 self.addCleanup(rd.close) 55 self.addCleanup(rd.close)
58 self.addCleanup(wr.close) 56 self.addCleanup(wr.close)
57 return rd, wr
58
59 def test_register(self):
60 s = self.SELECTOR()
61 self.addCleanup(s.close)
62
63 rd, wr = self.make_socketpair()
59 64
60 key = s.register(rd, selectors.EVENT_READ, "data") 65 key = s.register(rd, selectors.EVENT_READ, "data")
61 self.assertIsInstance(key, selectors.SelectorKey) 66 self.assertIsInstance(key, selectors.SelectorKey)
62 self.assertEqual(key.fileobj, rd) 67 self.assertEqual(key.fileobj, rd)
63 self.assertEqual(key.fd, rd.fileno()) 68 self.assertEqual(key.fd, rd.fileno())
64 self.assertEqual(key.events, selectors.EVENT_READ) 69 self.assertEqual(key.events, selectors.EVENT_READ)
65 self.assertEqual(key.data, "data") 70 self.assertEqual(key.data, "data")
66 71
67 # register an unknown event 72 # register an unknown event
68 self.assertRaises(ValueError, s.register, 0, 999999) 73 self.assertRaises(ValueError, s.register, 0, 999999)
69 74
70 # register an invalid FD 75 # register an invalid FD
71 self.assertRaises(ValueError, s.register, -10, selectors.EVENT_READ) 76 self.assertRaises(ValueError, s.register, -10, selectors.EVENT_READ)
72 77
73 # register twice 78 # register twice
74 self.assertRaises(KeyError, s.register, rd, selectors.EVENT_READ) 79 self.assertRaises(KeyError, s.register, rd, selectors.EVENT_READ)
75 80
76 # register the same FD, but with a different object 81 # register the same FD, but with a different object
77 self.assertRaises(KeyError, s.register, rd.fileno(), 82 self.assertRaises(KeyError, s.register, rd.fileno(),
78 selectors.EVENT_READ) 83 selectors.EVENT_READ)
79 84
80 def test_unregister(self): 85 def test_unregister(self):
81 s = self.SELECTOR() 86 s = self.SELECTOR()
82 self.addCleanup(s.close) 87 self.addCleanup(s.close)
83 88
84 rd, wr = socketpair() 89 rd, wr = self.make_socketpair()
85 self.addCleanup(rd.close)
86 self.addCleanup(wr.close)
87 90
88 s.register(rd, selectors.EVENT_READ) 91 s.register(rd, selectors.EVENT_READ)
89 s.unregister(rd) 92 s.unregister(rd)
90 93
91 # unregister an unknown file obj 94 # unregister an unknown file obj
92 self.assertRaises(KeyError, s.unregister, 999999) 95 self.assertRaises(KeyError, s.unregister, 999999)
93 96
94 # unregister twice 97 # unregister twice
95 self.assertRaises(KeyError, s.unregister, rd) 98 self.assertRaises(KeyError, s.unregister, rd)
96 99
100 def test_unregister_after_fd_close(self):
101 s = self.SELECTOR()
102 self.addCleanup(s.close)
103 rd, wr = self.make_socketpair()
104 r, w = rd.fileno(), wr.fileno()
105 s.register(r, selectors.EVENT_READ)
106 s.register(w, selectors.EVENT_WRITE)
107 rd.close()
108 wr.close()
109 s.unregister(r)
110 s.unregister(w)
111
112 @unittest.skipUnless(os.name == 'posix', "requires posix")
113 def test_unregister_after_fd_close_and_reuse(self):
114 s = self.SELECTOR()
115 self.addCleanup(s.close)
116 rd, wr = self.make_socketpair()
117 r, w = rd.fileno(), wr.fileno()
118 s.register(r, selectors.EVENT_READ)
119 s.register(w, selectors.EVENT_WRITE)
120 rd2, wr2 = self.make_socketpair()
121 rd.close()
122 wr.close()
123 os.dup2(rd2.fileno(), r)
124 os.dup2(wr2.fileno(), w)
125 self.addCleanup(os.close, r)
126 self.addCleanup(os.close, w)
127 s.unregister(r)
128 s.unregister(w)
129
130 def test_unregister_after_socket_close(self):
131 s = self.SELECTOR()
132 self.addCleanup(s.close)
133 rd, wr = self.make_socketpair()
134 s.register(rd, selectors.EVENT_READ)
135 s.register(wr, selectors.EVENT_WRITE)
136 rd.close()
137 wr.close()
138 s.unregister(rd)
139 s.unregister(wr)
140
97 def test_modify(self): 141 def test_modify(self):
98 s = self.SELECTOR() 142 s = self.SELECTOR()
99 self.addCleanup(s.close) 143 self.addCleanup(s.close)
100 144
101 rd, wr = socketpair() 145 rd, wr = self.make_socketpair()
102 self.addCleanup(rd.close)
103 self.addCleanup(wr.close)
104 146
105 key = s.register(rd, selectors.EVENT_READ) 147 key = s.register(rd, selectors.EVENT_READ)
106 148
107 # modify events 149 # modify events
108 key2 = s.modify(rd, selectors.EVENT_WRITE) 150 key2 = s.modify(rd, selectors.EVENT_WRITE)
109 self.assertNotEqual(key.events, key2.events) 151 self.assertNotEqual(key.events, key2.events)
110 self.assertEqual(key2, s.get_key(rd)) 152 self.assertEqual(key2, s.get_key(rd))
111 153
112 s.unregister(rd) 154 s.unregister(rd)
113 155
(...skipping 17 matching lines...) Expand all
131 s.unregister = unittest.mock.Mock() 173 s.unregister = unittest.mock.Mock()
132 174
133 s.modify(rd, selectors.EVENT_READ, d3) 175 s.modify(rd, selectors.EVENT_READ, d3)
134 self.assertFalse(s.register.called) 176 self.assertFalse(s.register.called)
135 self.assertFalse(s.unregister.called) 177 self.assertFalse(s.unregister.called)
136 178
137 def test_close(self): 179 def test_close(self):
138 s = self.SELECTOR() 180 s = self.SELECTOR()
139 self.addCleanup(s.close) 181 self.addCleanup(s.close)
140 182
141 rd, wr = socketpair() 183 rd, wr = self.make_socketpair()
142 self.addCleanup(rd.close)
143 self.addCleanup(wr.close)
144 184
145 s.register(rd, selectors.EVENT_READ) 185 s.register(rd, selectors.EVENT_READ)
146 s.register(wr, selectors.EVENT_WRITE) 186 s.register(wr, selectors.EVENT_WRITE)
147 187
148 s.close() 188 s.close()
149 self.assertRaises(KeyError, s.get_key, rd) 189 self.assertRaises(KeyError, s.get_key, rd)
150 self.assertRaises(KeyError, s.get_key, wr) 190 self.assertRaises(KeyError, s.get_key, wr)
151 191
152 def test_get_key(self): 192 def test_get_key(self):
153 s = self.SELECTOR() 193 s = self.SELECTOR()
154 self.addCleanup(s.close) 194 self.addCleanup(s.close)
155 195
156 rd, wr = socketpair() 196 rd, wr = self.make_socketpair()
157 self.addCleanup(rd.close)
158 self.addCleanup(wr.close)
159 197
160 key = s.register(rd, selectors.EVENT_READ, "data") 198 key = s.register(rd, selectors.EVENT_READ, "data")
161 self.assertEqual(key, s.get_key(rd)) 199 self.assertEqual(key, s.get_key(rd))
162 200
163 # unknown file obj 201 # unknown file obj
164 self.assertRaises(KeyError, s.get_key, 999999) 202 self.assertRaises(KeyError, s.get_key, 999999)
165 203
166 def test_get_map(self): 204 def test_get_map(self):
167 s = self.SELECTOR() 205 s = self.SELECTOR()
168 self.addCleanup(s.close) 206 self.addCleanup(s.close)
169 207
170 rd, wr = socketpair() 208 rd, wr = self.make_socketpair()
171 self.addCleanup(rd.close)
172 self.addCleanup(wr.close)
173 209
174 keys = s.get_map() 210 keys = s.get_map()
175 self.assertFalse(keys) 211 self.assertFalse(keys)
176 self.assertEqual(len(keys), 0) 212 self.assertEqual(len(keys), 0)
177 self.assertEqual(list(keys), []) 213 self.assertEqual(list(keys), [])
178 key = s.register(rd, selectors.EVENT_READ, "data") 214 key = s.register(rd, selectors.EVENT_READ, "data")
179 self.assertIn(rd, keys) 215 self.assertIn(rd, keys)
180 self.assertEqual(key, keys[rd]) 216 self.assertEqual(key, keys[rd])
181 self.assertEqual(len(keys), 1) 217 self.assertEqual(len(keys), 1)
182 self.assertEqual(list(keys), [rd.fileno()]) 218 self.assertEqual(list(keys), [rd.fileno()])
183 self.assertEqual(list(keys.values()), [key]) 219 self.assertEqual(list(keys.values()), [key])
184 220
185 # unknown file obj 221 # unknown file obj
186 with self.assertRaises(KeyError): 222 with self.assertRaises(KeyError):
187 keys[999999] 223 keys[999999]
188 224
189 # Read-only mapping 225 # Read-only mapping
190 with self.assertRaises(TypeError): 226 with self.assertRaises(TypeError):
191 del keys[rd] 227 del keys[rd]
192 228
193 def test_select(self): 229 def test_select(self):
194 s = self.SELECTOR() 230 s = self.SELECTOR()
195 self.addCleanup(s.close) 231 self.addCleanup(s.close)
196 232
197 rd, wr = socketpair() 233 rd, wr = self.make_socketpair()
198 self.addCleanup(rd.close)
199 self.addCleanup(wr.close)
200 234
201 s.register(rd, selectors.EVENT_READ) 235 s.register(rd, selectors.EVENT_READ)
202 wr_key = s.register(wr, selectors.EVENT_WRITE) 236 wr_key = s.register(wr, selectors.EVENT_WRITE)
203 237
204 result = s.select() 238 result = s.select()
205 for key, events in result: 239 for key, events in result:
206 self.assertIsInstance(key, selectors.SelectorKey) 240 self.assertIsInstance(key, selectors.SelectorKey)
207 self.assertTrue(events) 241 self.assertTrue(events)
208 self.assertFalse(events & ~(selectors.EVENT_READ | 242 self.assertFalse(events & ~(selectors.EVENT_READ |
209 selectors.EVENT_WRITE)) 243 selectors.EVENT_WRITE))
210 244
211 self.assertEqual([(wr_key, selectors.EVENT_WRITE)], result) 245 self.assertEqual([(wr_key, selectors.EVENT_WRITE)], result)
212 246
213 def test_context_manager(self): 247 def test_context_manager(self):
214 s = self.SELECTOR() 248 s = self.SELECTOR()
215 self.addCleanup(s.close) 249 self.addCleanup(s.close)
216 250
217 rd, wr = socketpair() 251 rd, wr = self.make_socketpair()
218 self.addCleanup(rd.close)
219 self.addCleanup(wr.close)
220 252
221 with s as sel: 253 with s as sel:
222 sel.register(rd, selectors.EVENT_READ) 254 sel.register(rd, selectors.EVENT_READ)
223 sel.register(wr, selectors.EVENT_WRITE) 255 sel.register(wr, selectors.EVENT_WRITE)
224 256
225 self.assertRaises(KeyError, s.get_key, rd) 257 self.assertRaises(KeyError, s.get_key, rd)
226 self.assertRaises(KeyError, s.get_key, wr) 258 self.assertRaises(KeyError, s.get_key, wr)
227 259
228 def test_fileno(self): 260 def test_fileno(self):
229 s = self.SELECTOR() 261 s = self.SELECTOR()
(...skipping 10 matching lines...) Expand all
240 272
241 NUM_SOCKETS = 12 273 NUM_SOCKETS = 12
242 MSG = b" This is a test." 274 MSG = b" This is a test."
243 MSG_LEN = len(MSG) 275 MSG_LEN = len(MSG)
244 readers = [] 276 readers = []
245 writers = [] 277 writers = []
246 r2w = {} 278 r2w = {}
247 w2r = {} 279 w2r = {}
248 280
249 for i in range(NUM_SOCKETS): 281 for i in range(NUM_SOCKETS):
250 rd, wr = socketpair() 282 rd, wr = self.make_socketpair()
251 self.addCleanup(rd.close)
252 self.addCleanup(wr.close)
253 s.register(rd, selectors.EVENT_READ) 283 s.register(rd, selectors.EVENT_READ)
254 s.register(wr, selectors.EVENT_WRITE) 284 s.register(wr, selectors.EVENT_WRITE)
255 readers.append(rd) 285 readers.append(rd)
256 writers.append(wr) 286 writers.append(wr)
257 r2w[rd] = wr 287 r2w[rd] = wr
258 w2r[wr] = rd 288 w2r[wr] = rd
259 289
260 bufs = [] 290 bufs = []
261 291
262 while writers: 292 while writers:
(...skipping 23 matching lines...) Expand all
286 s.unregister(r2w[rd]) 316 s.unregister(r2w[rd])
287 s.unregister(rd) 317 s.unregister(rd)
288 writers.remove(r2w[rd]) 318 writers.remove(r2w[rd])
289 319
290 self.assertEqual(bufs, [MSG] * NUM_SOCKETS) 320 self.assertEqual(bufs, [MSG] * NUM_SOCKETS)
291 321
292 def test_timeout(self): 322 def test_timeout(self):
293 s = self.SELECTOR() 323 s = self.SELECTOR()
294 self.addCleanup(s.close) 324 self.addCleanup(s.close)
295 325
296 rd, wr = socketpair() 326 rd, wr = self.make_socketpair()
297 self.addCleanup(rd.close)
298 self.addCleanup(wr.close)
299 327
300 s.register(wr, selectors.EVENT_WRITE) 328 s.register(wr, selectors.EVENT_WRITE)
301 t = time() 329 t = time()
302 self.assertEqual(1, len(s.select(0))) 330 self.assertEqual(1, len(s.select(0)))
303 self.assertEqual(1, len(s.select(-1))) 331 self.assertEqual(1, len(s.select(-1)))
304 self.assertLess(time() - t, 0.5) 332 self.assertLess(time() - t, 0.5)
305 333
306 s.unregister(wr) 334 s.unregister(wr)
307 s.register(rd, selectors.EVENT_READ) 335 s.register(rd, selectors.EVENT_READ)
308 t = time() 336 t = time()
309 self.assertFalse(s.select(0)) 337 self.assertFalse(s.select(0))
310 self.assertFalse(s.select(-1)) 338 self.assertFalse(s.select(-1))
311 self.assertLess(time() - t, 0.5) 339 self.assertLess(time() - t, 0.5)
312 340
313 t0 = time() 341 t0 = time()
314 self.assertFalse(s.select(1)) 342 self.assertFalse(s.select(1))
315 t1 = time() 343 t1 = time()
316 self.assertTrue(0.5 < t1 - t0 < 1.5, t1 - t0) 344 dt = t1 - t0
345 self.assertTrue(0.8 <= dt <= 1.6, dt)
317 346
318 @unittest.skipUnless(hasattr(signal, "alarm"), 347 @unittest.skipUnless(hasattr(signal, "alarm"),
319 "signal.alarm() required for this test") 348 "signal.alarm() required for this test")
320 def test_select_interrupt(self): 349 def test_select_interrupt(self):
321 s = self.SELECTOR() 350 s = self.SELECTOR()
322 self.addCleanup(s.close) 351 self.addCleanup(s.close)
323 352
324 rd, wr = socketpair() 353 rd, wr = self.make_socketpair()
325 self.addCleanup(rd.close)
326 self.addCleanup(wr.close)
327 354
328 orig_alrm_handler = signal.signal(signal.SIGALRM, lambda *args: None) 355 orig_alrm_handler = signal.signal(signal.SIGALRM, lambda *args: None)
329 self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler) 356 self.addCleanup(signal.signal, signal.SIGALRM, orig_alrm_handler)
330 self.addCleanup(signal.alarm, 0) 357 self.addCleanup(signal.alarm, 0)
331 358
332 signal.alarm(1) 359 signal.alarm(1)
333 360
334 s.register(rd, selectors.EVENT_READ) 361 s.register(rd, selectors.EVENT_READ)
335 t = time() 362 t = time()
336 self.assertFalse(s.select(2)) 363 self.assertFalse(s.select(2))
337 self.assertLess(time() - t, 2.5) 364 self.assertLess(time() - t, 2.5)
365
366 def test_resolution(self):
367 s = self.SELECTOR()
368 self.assertIsInstance(s.resolution, (int, float))
369 self.assertGreater(s.resolution, 0.0)
338 370
339 371
340 class ScalableSelectorMixIn: 372 class ScalableSelectorMixIn:
341 373
342 # see issue #18963 for why it's skipped on older OS X versions 374 # see issue #18963 for why it's skipped on older OS X versions
343 @support.requires_mac_ver(10, 5) 375 @support.requires_mac_ver(10, 5)
344 @unittest.skipUnless(resource, "Test needs resource module") 376 @unittest.skipUnless(resource, "Test needs resource module")
345 def test_above_fd_setsize(self): 377 def test_above_fd_setsize(self):
346 # A scalable implementation should have no problem with more than 378 # A scalable implementation should have no problem with more than
347 # FD_SETSIZE file descriptors. Since we don't know the value, we just 379 # FD_SETSIZE file descriptors. Since we don't know the value, we just
348 # try to set the soft RLIMIT_NOFILE to the hard RLIMIT_NOFILE ceiling. 380 # try to set the soft RLIMIT_NOFILE to the hard RLIMIT_NOFILE ceiling.
349 soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) 381 soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
350 try: 382 try:
351 resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard)) 383 resource.setrlimit(resource.RLIMIT_NOFILE, (hard, hard))
352 self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE, 384 self.addCleanup(resource.setrlimit, resource.RLIMIT_NOFILE,
353 (soft, hard)) 385 (soft, hard))
354 NUM_FDS = hard 386 NUM_FDS = hard
355 except (OSError, ValueError): 387 except (OSError, ValueError):
356 NUM_FDS = soft 388 NUM_FDS = soft
357 389
358 # guard for already allocated FDs (stdin, stdout...) 390 # guard for already allocated FDs (stdin, stdout...)
359 NUM_FDS -= 32 391 NUM_FDS -= 32
360 392
361 s = self.SELECTOR() 393 s = self.SELECTOR()
362 self.addCleanup(s.close) 394 self.addCleanup(s.close)
363 395
364 for i in range(NUM_FDS // 2): 396 for i in range(NUM_FDS // 2):
365 try: 397 try:
366 rd, wr = socketpair() 398 rd, wr = self.make_socketpair()
367 except OSError: 399 except OSError:
368 # too many FDs, skip - note that we should only catch EMFILE 400 # too many FDs, skip - note that we should only catch EMFILE
369 # here, but apparently *BSD and Solaris can fail upon connect() 401 # here, but apparently *BSD and Solaris can fail upon connect()
370 # or bind() with EADDRNOTAVAIL, so let's be safe 402 # or bind() with EADDRNOTAVAIL, so let's be safe
371 self.skipTest("FD limit reached") 403 self.skipTest("FD limit reached")
372
373 self.addCleanup(rd.close)
374 self.addCleanup(wr.close)
375 404
376 try: 405 try:
377 s.register(rd, selectors.EVENT_READ) 406 s.register(rd, selectors.EVENT_READ)
378 s.register(wr, selectors.EVENT_WRITE) 407 s.register(wr, selectors.EVENT_WRITE)
379 except OSError as e: 408 except OSError as e:
380 if e.errno == errno.ENOSPC: 409 if e.errno == errno.ENOSPC:
381 # this can be raised by epoll if we go over 410 # this can be raised by epoll if we go over
382 # fs.epoll.max_user_watches sysctl 411 # fs.epoll.max_user_watches sysctl
383 self.skipTest("FD limit reached") 412 self.skipTest("FD limit reached")
384 raise 413 raise
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 def test_main(): 449 def test_main():
421 tests = [DefaultSelectorTestCase, SelectSelectorTestCase, 450 tests = [DefaultSelectorTestCase, SelectSelectorTestCase,
422 PollSelectorTestCase, EpollSelectorTestCase, 451 PollSelectorTestCase, EpollSelectorTestCase,
423 KqueueSelectorTestCase] 452 KqueueSelectorTestCase]
424 support.run_unittest(*tests) 453 support.run_unittest(*tests)
425 support.reap_children() 454 support.reap_children()
426 455
427 456
428 if __name__ == "__main__": 457 if __name__ == "__main__":
429 test_main() 458 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+