2016-07-27 17:55:55 +00:00
|
|
|
import asyncio
|
|
|
|
import concurrent.futures
|
2019-10-24 01:12:15 +00:00
|
|
|
import multiprocessing
|
|
|
|
import unittest
|
2016-07-27 17:55:55 +00:00
|
|
|
|
|
|
|
from uvloop import _testbase as tb
|
|
|
|
|
|
|
|
|
|
|
|
def fib(n):
|
|
|
|
if n < 2:
|
|
|
|
return 1
|
|
|
|
return fib(n - 2) + fib(n - 1)
|
|
|
|
|
|
|
|
|
|
|
|
class _TestExecutors:
|
|
|
|
|
|
|
|
def run_pool_test(self, pool_factory):
|
|
|
|
async def run():
|
|
|
|
pool = pool_factory()
|
|
|
|
with pool:
|
|
|
|
coros = []
|
|
|
|
for i in range(0, 10):
|
|
|
|
coros.append(self.loop.run_in_executor(pool, fib, i))
|
2019-10-23 21:58:46 +00:00
|
|
|
res = await asyncio.gather(*coros)
|
2016-07-27 17:55:55 +00:00
|
|
|
self.assertEqual(res, fib10)
|
2019-10-23 21:58:46 +00:00
|
|
|
await asyncio.sleep(0.01)
|
2016-07-27 17:55:55 +00:00
|
|
|
|
|
|
|
fib10 = [fib(i) for i in range(10)]
|
|
|
|
self.loop.run_until_complete(run())
|
|
|
|
|
2019-10-24 01:12:15 +00:00
|
|
|
@unittest.skipIf(
|
|
|
|
multiprocessing.get_start_method(False) == 'spawn',
|
|
|
|
'no need to test on macOS where spawn is used instead of fork')
|
2016-07-27 17:55:55 +00:00
|
|
|
def test_executors_process_pool_01(self):
|
|
|
|
self.run_pool_test(concurrent.futures.ProcessPoolExecutor)
|
|
|
|
|
|
|
|
def test_executors_process_pool_02(self):
|
|
|
|
self.run_pool_test(concurrent.futures.ThreadPoolExecutor)
|
|
|
|
|
|
|
|
|
|
|
|
class TestUVExecutors(_TestExecutors, tb.UVTestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class TestAIOExecutors(_TestExecutors, tb.AIOTestCase):
|
|
|
|
pass
|