def to_context(self, detectors=None, ignore_detector_error=False): if detectors is None: detectors = [] ThreadPool.reset() self.engine = MockEngine((self.source_width, self.source_height)) flip_horizontally = flip_vertically = False if self.target_width != "orig": flip_horizontally = self.target_width < 0 self.target_width = abs(self.target_width) if self.target_height != "orig": flip_vertically = self.target_height < 0 self.target_height = abs(self.target_height) importer = Importer(None) importer.detectors = detectors importer.storage = NoStorage config = Config() config.IGNORE_SMART_ERRORS = ignore_detector_error ctx = Context(server=None, config=config, importer=importer) ctx.modules.engine = self.engine ctx.request = RequestParameters( buffer=None, debug=False, meta=self.meta, crop={ "left": self.crop_left, "top": self.crop_top, "right": self.crop_right, "bottom": self.crop_bottom, }, adaptive=self.adaptive, full=self.full, fit_in=self.fit_in, horizontal_flip=flip_horizontally, vertical_flip=flip_vertically, width=self.target_width, height=self.target_height, halign=self.halign, valign=self.valign, focal_points=self.focal_points, smart=True, extension="JPEG", filters=[], quality=80, image="some.jpeg", stretch=self.stretch, ) ctx.request.engine = self.engine ctx.request.engine.extension = ".jpeg" return ctx
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()