|
1 | 1 | # Test case for the os.poll() function |
2 | 2 |
|
3 | | -import os, select, random, unittest |
| 3 | +import os |
| 4 | +import random |
| 5 | +import select |
4 | 6 | import _testcapi |
5 | | -from test.support import TESTFN, run_unittest |
| 7 | +try: |
| 8 | + import threading |
| 9 | +except ImportError: |
| 10 | + threading = None |
| 11 | +import time |
| 12 | +import unittest |
| 13 | +from test.support import TESTFN, run_unittest, reap_threads |
6 | 14 |
|
7 | 15 | try: |
8 | 16 | select.poll |
@@ -160,6 +168,36 @@ def test_poll3(self): |
160 | 168 | self.assertRaises(OverflowError, pollster.poll, _testcapi.INT_MAX + 1) |
161 | 169 | self.assertRaises(OverflowError, pollster.poll, _testcapi.UINT_MAX + 1) |
162 | 170 |
|
| 171 | + @unittest.skipUnless(threading, 'Threading required for this test.') |
| 172 | + @reap_threads |
| 173 | + def test_threaded_poll(self): |
| 174 | + r, w = os.pipe() |
| 175 | + self.addCleanup(os.close, r) |
| 176 | + self.addCleanup(os.close, w) |
| 177 | + rfds = [] |
| 178 | + for i in range(10): |
| 179 | + fd = os.dup(r) |
| 180 | + self.addCleanup(os.close, fd) |
| 181 | + rfds.append(fd) |
| 182 | + pollster = select.poll() |
| 183 | + for fd in rfds: |
| 184 | + pollster.register(fd, select.POLLIN) |
| 185 | + |
| 186 | + t = threading.Thread(target=pollster.poll) |
| 187 | + t.start() |
| 188 | + try: |
| 189 | + time.sleep(0.5) |
| 190 | + # trigger ufds array reallocation |
| 191 | + for fd in rfds: |
| 192 | + pollster.unregister(fd) |
| 193 | + pollster.register(w, select.POLLOUT) |
| 194 | + self.assertRaises(RuntimeError, pollster.poll) |
| 195 | + finally: |
| 196 | + # and make the call to poll() from the thread return |
| 197 | + os.write(w, b'spam') |
| 198 | + t.join() |
| 199 | + |
| 200 | + |
163 | 201 | def test_main(): |
164 | 202 | run_unittest(PollTests) |
165 | 203 |
|
|
0 commit comments