bpo-42641: Enhance test_select.test_select() (GH-23782)

Enhance test_select.test_select(): it now takes 500 ms rather than 10
seconds.

* Use Python rather than a shell as the child process to make the
  test more portable.
* Use a sleep of 50 ms per line rather than 1 second.
* Use subprocess.Popen rather than os.popen().
This commit is contained in:
Victor Stinner 2020-12-15 18:06:36 +01:00 committed by GitHub
parent c8a10d2fab
commit 7f14a3756b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 9 deletions

View File

@ -1,7 +1,9 @@
import errno import errno
import os import os
import select import select
import subprocess
import sys import sys
import textwrap
import unittest import unittest
from test import support from test import support
@ -45,16 +47,25 @@ class SelectTestCase(unittest.TestCase):
self.assertIsNot(w, x) self.assertIsNot(w, x)
def test_select(self): def test_select(self):
cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done' code = textwrap.dedent('''
with os.popen(cmd) as p: import time
for tout in (0, 1, 2, 4, 8, 16) + (None,)*10: for i in range(10):
print("testing...", flush=True)
time.sleep(0.050)
''')
cmd = [sys.executable, '-I', '-c', code]
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc:
pipe = proc.stdout
for timeout in (0, 1, 2, 4, 8, 16) + (None,)*10:
if support.verbose: if support.verbose:
print('timeout =', tout) print(f'timeout = {timeout}')
rfd, wfd, xfd = select.select([p], [], [], tout) rfd, wfd, xfd = select.select([pipe], [], [], timeout)
if (rfd, wfd, xfd) == ([], [], []): self.assertEqual(wfd, [])
self.assertEqual(xfd, [])
if not rfd:
continue continue
if (rfd, wfd, xfd) == ([p], [], []): if rfd == [pipe]:
line = p.readline() line = pipe.readline()
if support.verbose: if support.verbose:
print(repr(line)) print(repr(line))
if not line: if not line:
@ -62,7 +73,8 @@ class SelectTestCase(unittest.TestCase):
print('EOF') print('EOF')
break break
continue continue
self.fail('Unexpected return values from select():', rfd, wfd, xfd) self.fail('Unexpected return values from select():',
rfd, wfd, xfd)
# Issue 16230: Crash on select resized list # Issue 16230: Crash on select resized list
def test_select_mutated(self): def test_select_mutated(self):

View File

@ -0,0 +1,2 @@
Enhance ``test_select.test_select()``: it now takes 500 ms rather than 10
seconds. Use Python rather than a shell to make the test more portable.