async def test_can_get_threadpool_instance(): instance = ThreadPool.instance(0) expect(instance.pool).to_be_null() instance = ThreadPool.instance(10) expect(instance).not_to_be_null() instance2 = ThreadPool.instance(10) expect(instance2).to_equal(instance) instance3 = ThreadPool.instance(11) expect(instance3).not_to_equal(instance)
def __init__(self, server=None, config=None, importer=None, request_handler=None): self.server = server self.config = config if importer: self.modules = ContextImporter(self, importer) if importer.metrics: self.metrics = importer.metrics(config) else: self.metrics = Metrics(config) else: self.modules = None self.metrics = Metrics(config) self.app_class = "thumbor.app.ThumborServiceApp" if hasattr(self.config, "APP_CLASS"): self.app_class = self.config.APP_CLASS if (hasattr(self.server, "app_class") and self.server.app_class != "thumbor.app.ThumborServiceApp"): self.app_class = self.server.app_class self.filters_factory = FiltersFactory( self.modules.filters if self.modules else []) self.request_handler = request_handler self.thread_pool = ThreadPool.instance( getattr(config, "ENGINE_THREADPOOL_SIZE", 0)) self.headers = {}
async def test_queueing_task_when_no_pool_runs_sync(): instance = ThreadPool.instance(0) expect(instance).not_to_be_null() def add(): return 10 result = await instance.queue(add) expect(result).to_equal(10)
async def test_can_run_task_in_foreground(): instance = ThreadPool.instance(0) expect(instance).not_to_be_null() def add(): return 10 result = await instance._execute_in_foreground(add) expect(result).to_equal(10)
async def test_can_run_async(): instance = ThreadPool.instance(10) expect(instance).not_to_be_null() def add(): sleep(1) return 10 result = await instance._execute_in_pool(add) expect(result).to_equal(10)
async def test_queueing_task_when_no_pool_runs_sync_and_exception_happens(): instance = ThreadPool.instance(0) expect(instance).not_to_be_null() exception = Exception("Boom") def add(): raise exception with expect.error_to_happen(Exception, message="Boom"): await instance.queue(add)
async def test_can_run_task_in_foreground_and_exception_happens(): instance = ThreadPool.instance(0) expect(instance).not_to_be_null() exception = Exception("Boom") def add(): raise exception with expect.error_to_happen(Exception, message="Boom"): await instance._execute_in_foreground(add)
async def test_can_run_async_with_queue(): instance = ThreadPool.instance(10) expect(instance).not_to_be_null() def add(value): sleep(1) return value result = await instance.queue(add, 10) expect(result).to_equal(10)
async def test_can_cleanup_pool(): instance = ThreadPool.instance(0) instance.pool = mock.Mock() instance.cleanup() expect(instance.pool.shutdown.called).to_be_true()