def test_process_initializer(self, set_mp_process_title, ignore_signal, reset_signal): from celery import Celery from celery import signals from celery.app import _tls from celery.worker import process_initializer from celery.worker import WORKER_SIGRESET, WORKER_SIGIGNORE def on_worker_process_init(**kwargs): on_worker_process_init.called = True on_worker_process_init.called = False signals.worker_process_init.connect(on_worker_process_init) app = Celery(loader=Mock(), set_as_current=False) process_initializer(app, "awesome.worker.com") self.assertIn((tuple(WORKER_SIGIGNORE), {}), ignore_signal.call_args_list) self.assertIn((tuple(WORKER_SIGRESET), {}), reset_signal.call_args_list) self.assertTrue(app.loader.init_worker.call_count) self.assertTrue(on_worker_process_init.called) self.assertIs(_tls.current_app, app) set_mp_process_title.assert_called_with("celeryd", hostname="awesome.worker.com")
def test_process_initializer(self): from celery import Celery from celery import platforms from celery import signals from celery.app import _tls from celery.worker import process_initializer from celery.worker import WORKER_SIGRESET, WORKER_SIGIGNORE ignored_signals = [] reset_signals = [] worker_init = [False] default_app = current_app app = Celery(loader="default", set_as_current=False) class Loader(object): def init_worker(self): worker_init[0] = True app.loader = Loader() def on_worker_process_init(**kwargs): on_worker_process_init.called = True on_worker_process_init.called = False signals.worker_process_init.connect(on_worker_process_init) def set_mp_process_title(title, hostname=None): set_mp_process_title.called = (title, hostname) set_mp_process_title.called = () pignore_signal = platforms.ignore_signal preset_signal = platforms.reset_signal psetproctitle = platforms.set_mp_process_title platforms.ignore_signal = lambda sig: ignored_signals.append(sig) platforms.reset_signal = lambda sig: reset_signals.append(sig) platforms.set_mp_process_title = set_mp_process_title try: process_initializer(app, "awesome.worker.com") self.assertItemsEqual(ignored_signals, WORKER_SIGIGNORE) self.assertItemsEqual(reset_signals, WORKER_SIGRESET) self.assertTrue(worker_init[0]) self.assertTrue(on_worker_process_init.called) self.assertIs(_tls.current_app, app) self.assertTupleEqual(set_mp_process_title.called, ("celeryd", "awesome.worker.com")) finally: platforms.ignore_signal = pignore_signal platforms.reset_signal = preset_signal platforms.set_mp_process_title = psetproctitle default_app.set_current()
def test_process_initializer(self, set_mp_process_title, _signals): from celery import Celery from celery import signals from celery.app import _tls from celery.worker import process_initializer from celery.worker import WORKER_SIGRESET, WORKER_SIGIGNORE def on_worker_process_init(**kwargs): on_worker_process_init.called = True on_worker_process_init.called = False signals.worker_process_init.connect(on_worker_process_init) app = Celery(loader=Mock(), set_as_current=False) process_initializer(app, "awesome.worker.com") self.assertIn((tuple(WORKER_SIGIGNORE), {}), _signals.ignore.call_args_list) self.assertIn((tuple(WORKER_SIGRESET), {}), _signals.reset.call_args_list) self.assertTrue(app.loader.init_worker.call_count) self.assertTrue(on_worker_process_init.called) self.assertIs(_tls.current_app, app) set_mp_process_title.assert_called_with("celeryd", hostname="awesome.worker.com")