[Python-checkins] bpo-42641: Enhance test_select.test_select() (GH-23782)

vstinner webhook-mailer at python.org
Tue Dec 15 12:06:47 EST 2020


https://github.com/python/cpython/commit/7f14a3756b61272cc15f24302589874b125c2f04
commit: 7f14a3756b61272cc15f24302589874b125c2f04
branch: master
author: Victor Stinner <vstinner at python.org>
committer: vstinner <vstinner at python.org>
date: 2020-12-15T18:06:36+01:00
summary:

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().

files:
A Misc/NEWS.d/next/Tests/2020-12-15-17-38-04.bpo-42641.uzwlF_.rst
M Lib/test/test_select.py

diff --git a/Lib/test/test_select.py b/Lib/test/test_select.py
index 458998a62fdf5..4ddd5fb96e0ce 100644
--- a/Lib/test/test_select.py
+++ b/Lib/test/test_select.py
@@ -1,7 +1,9 @@
 import errno
 import os
 import select
+import subprocess
 import sys
+import textwrap
 import unittest
 from test import support
 
@@ -45,16 +47,25 @@ def test_returned_list_identity(self):
         self.assertIsNot(w, x)
 
     def test_select(self):
-        cmd = 'for i in 0 1 2 3 4 5 6 7 8 9; do echo testing...; sleep 1; done'
-        with os.popen(cmd) as p:
-            for tout in (0, 1, 2, 4, 8, 16) + (None,)*10:
+        code = textwrap.dedent('''
+            import time
+            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:
-                    print('timeout =', tout)
-                rfd, wfd, xfd = select.select([p], [], [], tout)
-                if (rfd, wfd, xfd) == ([], [], []):
+                    print(f'timeout = {timeout}')
+                rfd, wfd, xfd = select.select([pipe], [], [], timeout)
+                self.assertEqual(wfd, [])
+                self.assertEqual(xfd, [])
+                if not rfd:
                     continue
-                if (rfd, wfd, xfd) == ([p], [], []):
-                    line = p.readline()
+                if rfd == [pipe]:
+                    line = pipe.readline()
                     if support.verbose:
                         print(repr(line))
                     if not line:
@@ -62,7 +73,8 @@ def test_select(self):
                             print('EOF')
                         break
                     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
     def test_select_mutated(self):
diff --git a/Misc/NEWS.d/next/Tests/2020-12-15-17-38-04.bpo-42641.uzwlF_.rst b/Misc/NEWS.d/next/Tests/2020-12-15-17-38-04.bpo-42641.uzwlF_.rst
new file mode 100644
index 0000000000000..bf890b73ee720
--- /dev/null
+++ b/Misc/NEWS.d/next/Tests/2020-12-15-17-38-04.bpo-42641.uzwlF_.rst
@@ -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.



More information about the Python-checkins mailing list