2022-02-05 19:34:52 +01:00
|
|
|
import asyncio
|
|
|
|
from unittest import TestCase
|
2022-02-06 13:42:34 +01:00
|
|
|
from unittest.mock import PropertyMock, patch
|
2022-02-05 19:34:52 +01:00
|
|
|
|
|
|
|
from asyncio_taskpool import pool
|
|
|
|
|
|
|
|
|
|
|
|
EMPTY_LIST, EMPTY_DICT = [], {}
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTaskPoolTestCase(TestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
self._pools = getattr(pool.BaseTaskPool, '_pools')
|
|
|
|
|
|
|
|
# These three methods are called during initialization, so we mock them by default during setup
|
|
|
|
self._add_pool_patcher = patch.object(pool.BaseTaskPool, '_add_pool')
|
2022-02-06 13:08:39 +01:00
|
|
|
self.pool_size_patcher = patch.object(pool.BaseTaskPool, 'pool_size', new_callable=PropertyMock)
|
2022-02-05 19:34:52 +01:00
|
|
|
self.__str___patcher = patch.object(pool.BaseTaskPool, '__str__')
|
|
|
|
self.mock__add_pool = self._add_pool_patcher.start()
|
2022-02-06 13:08:39 +01:00
|
|
|
self.mock_pool_size = self.pool_size_patcher.start()
|
2022-02-05 19:34:52 +01:00
|
|
|
self.mock___str__ = self.__str___patcher.start()
|
|
|
|
self.mock__add_pool.return_value = self.mock_idx = 123
|
|
|
|
self.mock___str__.return_value = self.mock_str = 'foobar'
|
|
|
|
|
|
|
|
# Test pool parameters:
|
2022-02-05 22:26:02 +01:00
|
|
|
self.test_pool_size, self.test_pool_name = 420, 'test123'
|
|
|
|
self.task_pool = pool.BaseTaskPool(pool_size=self.test_pool_size, name=self.test_pool_name)
|
2022-02-05 19:34:52 +01:00
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
setattr(pool.TaskPool, '_pools', self._pools)
|
|
|
|
self._add_pool_patcher.stop()
|
2022-02-06 13:08:39 +01:00
|
|
|
self.pool_size_patcher.stop()
|
2022-02-05 19:34:52 +01:00
|
|
|
self.__str___patcher.stop()
|
|
|
|
|
|
|
|
def test__add_pool(self):
|
|
|
|
self.assertListEqual(EMPTY_LIST, self._pools)
|
|
|
|
self._add_pool_patcher.stop()
|
|
|
|
output = pool.TaskPool._add_pool(self.task_pool)
|
|
|
|
self.assertEqual(0, output)
|
|
|
|
self.assertListEqual([self.task_pool], getattr(pool.TaskPool, '_pools'))
|
|
|
|
|
|
|
|
def test_init(self):
|
2022-02-06 13:08:39 +01:00
|
|
|
self.assertIsInstance(self.task_pool._enough_room, asyncio.locks.Semaphore)
|
2022-02-05 22:28:19 +01:00
|
|
|
self.assertTrue(self.task_pool._open)
|
|
|
|
self.assertEqual(0, self.task_pool._counter)
|
2022-02-05 19:34:52 +01:00
|
|
|
self.assertDictEqual(EMPTY_DICT, self.task_pool._running)
|
|
|
|
self.assertDictEqual(EMPTY_DICT, self.task_pool._cancelled)
|
|
|
|
self.assertDictEqual(EMPTY_DICT, self.task_pool._ended)
|
2022-02-06 13:42:34 +01:00
|
|
|
self.assertEqual(0, self.task_pool._num_cancelled)
|
|
|
|
self.assertEqual(0, self.task_pool._num_ended)
|
2022-02-05 19:34:52 +01:00
|
|
|
self.assertEqual(self.mock_idx, self.task_pool._idx)
|
2022-02-05 22:28:19 +01:00
|
|
|
self.assertEqual(self.test_pool_name, self.task_pool._name)
|
2022-02-05 19:34:52 +01:00
|
|
|
self.assertIsInstance(self.task_pool._all_tasks_known_flag, asyncio.locks.Event)
|
|
|
|
self.assertTrue(self.task_pool._all_tasks_known_flag.is_set())
|
2022-02-06 16:32:42 +01:00
|
|
|
self.assertIsInstance(self.task_pool._interrupt_flag, asyncio.locks.Event)
|
|
|
|
self.assertFalse(self.task_pool._interrupt_flag.is_set())
|
2022-02-05 19:34:52 +01:00
|
|
|
self.mock__add_pool.assert_called_once_with(self.task_pool)
|
2022-02-06 13:08:39 +01:00
|
|
|
self.mock_pool_size.assert_called_once_with(self.test_pool_size)
|
2022-02-05 19:34:52 +01:00
|
|
|
self.mock___str__.assert_called_once_with()
|
|
|
|
|
|
|
|
def test___str__(self):
|
|
|
|
self.__str___patcher.stop()
|
2022-02-05 22:26:02 +01:00
|
|
|
expected_str = f'{pool.BaseTaskPool.__name__}-{self.test_pool_name}'
|
2022-02-05 19:34:52 +01:00
|
|
|
self.assertEqual(expected_str, str(self.task_pool))
|
|
|
|
setattr(self.task_pool, '_name', None)
|
|
|
|
expected_str = f'{pool.BaseTaskPool.__name__}-{self.task_pool._idx}'
|
|
|
|
self.assertEqual(expected_str, str(self.task_pool))
|
|
|
|
|
2022-02-06 13:08:39 +01:00
|
|
|
def test_pool_size(self):
|
|
|
|
self.pool_size_patcher.stop()
|
|
|
|
self.task_pool._pool_size = self.test_pool_size
|
|
|
|
self.assertEqual(self.test_pool_size, self.task_pool.pool_size)
|
|
|
|
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
self.task_pool.pool_size = -1
|
|
|
|
|
|
|
|
self.task_pool.pool_size = new_size = 69
|
|
|
|
self.assertEqual(new_size, self.task_pool._pool_size)
|
|
|
|
|
2022-02-05 19:34:52 +01:00
|
|
|
def test_is_open(self):
|
|
|
|
self.task_pool._open = foo = 'foo'
|
|
|
|
self.assertEqual(foo, self.task_pool.is_open)
|
|
|
|
|
|
|
|
def test_num_running(self):
|
|
|
|
self.task_pool._running = ['foo', 'bar', 'baz']
|
|
|
|
self.assertEqual(3, self.task_pool.num_running)
|
|
|
|
|
|
|
|
def test_num_cancelled(self):
|
2022-02-06 13:42:34 +01:00
|
|
|
self.task_pool._num_cancelled = 33
|
2022-02-05 19:34:52 +01:00
|
|
|
self.assertEqual(3, self.task_pool.num_cancelled)
|
|
|
|
|
|
|
|
def test_num_ended(self):
|
2022-02-06 13:42:34 +01:00
|
|
|
self.task_pool._num_ended = 3
|
2022-02-05 19:34:52 +01:00
|
|
|
self.assertEqual(3, self.task_pool.num_ended)
|
|
|
|
|
2022-02-06 13:42:34 +01:00
|
|
|
def test_num_finished(self):
|
|
|
|
self.task_pool._num_cancelled = cancelled = 69
|
|
|
|
self.task_pool._num_ended = ended = 420
|
|
|
|
self.task_pool._cancelled = mock_cancelled_dict = {1: 'foo', 2: 'bar'}
|
|
|
|
self.assertEqual(ended - cancelled + len(mock_cancelled_dict), self.task_pool.num_finished)
|
2022-02-05 19:34:52 +01:00
|
|
|
|
|
|
|
def test_is_full(self):
|
2022-02-06 13:08:39 +01:00
|
|
|
self.assertEqual(self.task_pool._enough_room.locked(), self.task_pool.is_full)
|
2022-02-05 19:34:52 +01:00
|
|
|
|
|
|
|
def test__task_name(self):
|
|
|
|
i = 123
|
|
|
|
self.assertEqual(f'{self.mock_str}_Task-{i}', self.task_pool._task_name(i))
|