|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import asynctest |
| 3 | +import logging |
| 4 | +import asyncio |
| 5 | + |
| 6 | +from asyncpool import AsyncPool |
| 7 | + |
| 8 | + |
| 9 | +class WorkPoolTestCases(asynctest.TestCase): |
| 10 | + def __init__(self, *args, **kwargs): |
| 11 | + super().__init__(*args, **kwargs) |
| 12 | + |
| 13 | + logging.basicConfig(level=logging.INFO) |
| 14 | + self._logger = logging.Logger('') |
| 15 | + self._evt_wait = None |
| 16 | + |
| 17 | + async def setUp(self): |
| 18 | + self._evt_wait = asyncio.Event() |
| 19 | + |
| 20 | + async def tearDown(self): |
| 21 | + self._evt_wait.set() # unblock any blocked workers |
| 22 | + |
| 23 | + async def test_worker_limit(self): |
| 24 | + num_called = 0 |
| 25 | + |
| 26 | + evt_hit = asyncio.Event() |
| 27 | + |
| 28 | + async def worker(param): |
| 29 | + nonlocal num_called |
| 30 | + num_called += 1 |
| 31 | + assert param == 5 |
| 32 | + evt_hit.set() |
| 33 | + await self._evt_wait.wait() |
| 34 | + |
| 35 | + async with AsyncPool(None, 5, '', self._logger, worker) as wq: |
| 36 | + # tests that worker limit/load factor of 1 works correctly |
| 37 | + for _ in range(10): # five workers plus 5 in queue |
| 38 | + await asyncio.wait_for(wq.push(5), 1) |
| 39 | + |
| 40 | + self.assertEqual(num_called, 5) |
| 41 | + |
| 42 | + with self.assertRaises(asyncio.TimeoutError): |
| 43 | + # with load_factor==1, and all workers stuck we should timeout |
| 44 | + await asyncio.wait_for(wq.push(5), 1) |
| 45 | + |
| 46 | + self.assertEqual(wq.total_queued, 10) |
| 47 | + |
| 48 | + # unblock workers |
| 49 | + self._evt_wait.set() |
| 50 | + await asyncio.sleep(1) # clear the workers |
| 51 | + |
| 52 | + evt_hit.clear() |
| 53 | + |
| 54 | + await asyncio.wait_for(wq.push(5), 1) |
| 55 | + await asyncio.wait_for(evt_hit.wait(), 1) |
| 56 | + self.assertEqual(num_called, 11) |
| 57 | + self.assertFalse(wq.exceptions) |
| 58 | + |
| 59 | + async def test_load_factor(self): |
| 60 | + async def worker(param): |
| 61 | + await self._evt_wait.wait() |
| 62 | + |
| 63 | + async with AsyncPool(None, 5, '', self._logger, worker, 2) as wq: |
| 64 | + for _ in range(15): # 5 in-flight, + 10 in queue per load factor |
| 65 | + await asyncio.wait_for(wq.push(5), 1) |
| 66 | + |
| 67 | + with self.assertRaises(asyncio.TimeoutError): |
| 68 | + # with load_factor==1, and all workers stuck we should timeout |
| 69 | + await asyncio.wait_for(wq.push(5), 1) |
| 70 | + |
| 71 | + # unblock workers |
| 72 | + self._evt_wait.set() |
| 73 | + await asyncio.sleep(1) # let them clear the queue |
| 74 | + |
| 75 | + await asyncio.wait_for(wq.push(5), 1) |
| 76 | + self.assertFalse(wq.exceptions) |
| 77 | + |
| 78 | + async def test_task_timeout(self): |
| 79 | + async def worker(param): |
| 80 | + await self._evt_wait.wait() |
| 81 | + |
| 82 | + async with AsyncPool(None, 5, '', self._logger, worker, max_task_time=1, return_futures=True) as wq: |
| 83 | + fut = await asyncio.wait_for(wq.push(5), 1) |
| 84 | + |
| 85 | + for i in range(5): |
| 86 | + await asyncio.sleep(1) |
| 87 | + |
| 88 | + if fut.done(): |
| 89 | + e = fut.exception() |
| 90 | + self.assertIsInstance(e, asyncio.TimeoutError) |
| 91 | + self.assertTrue(wq.exceptions) |
| 92 | + return |
| 93 | + |
| 94 | + self.fail('future did not time out') |
| 95 | + |
| 96 | + async def test_join(self): |
| 97 | + key = 'blah' |
| 98 | + |
| 99 | + async def worker(param): |
| 100 | + await asyncio.sleep(1) # wait a sec before returning result |
| 101 | + return param |
| 102 | + |
| 103 | + async with AsyncPool(None, 5, '', self._logger, worker, return_futures=True) as wq: |
| 104 | + fut = await asyncio.wait_for(wq.push('blah'), 0.1) |
| 105 | + self.assertFalse(fut.done()) |
| 106 | + |
| 107 | + self.assertTrue(fut.done()) |
| 108 | + result = fut.result() |
| 109 | + self.assertEqual(result, key) |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + asynctest.main() |
0 commit comments