Пример #1
0
    def test_pool_restart_reload_modules(self):
        consumer = Consumer(self.app)
        consumer.controller = _WC(app=self.app)
        consumer.controller.consumer = consumer
        consumer.controller.pool.restart = Mock()
        consumer.reset_rate_limits = Mock(name="reset_rate_limits()")
        consumer.update_strategies = Mock(name="update_strategies()")
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = panel.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        self.app.conf.worker_pool_restarts = True
        with patch.dict(sys.modules, {"foo": None}):
            panel.handle("pool_restart", {"modules": ["foo"], "reload": False, "reloader": _reload})

            self.assertTrue(consumer.controller.pool.restart.called)
            self.assertFalse(_reload.called)
            self.assertFalse(_import.called)

            _import.reset_mock()
            _reload.reset_mock()
            consumer.controller.pool.restart.reset_mock()

            panel.handle("pool_restart", {"modules": ["foo"], "reload": True, "reloader": _reload})

            self.assertTrue(consumer.controller.pool.restart.called)
            self.assertTrue(_reload.called)
            self.assertFalse(_import.called)
Пример #2
0
 def test_create_when_eventloop(self):
     w = Mock()
     w.use_eventloop = w.pool_putlocks = w.pool_cls.uses_semaphore = True
     comp = Pool(w)
     w.pool = Mock()
     comp.create(w)
     self.assertIs(w.process_task, w._process_task_sem)
Пример #3
0
    def test_shutdown_nodes(self, slepp, gethostname, Pidfile):
        gethostname.return_value = 'e.com'
        self.prepare_pidfile_for_getpids(Pidfile)
        self.assertIsNone(self.t.shutdown_nodes([]))
        self.t.signal_node = Mock()
        node_alive = self.t.node_alive = Mock()
        self.t.node_alive.return_value = False

        callback = Mock()
        self.t.stop(['foo', 'bar', 'baz'], 'celery worker', callback=callback)
        sigs = sorted(self.t.signal_node.call_args_list)
        self.assertEqual(len(sigs), 2)
        self.assertIn(
            ('*****@*****.**', 10, signal.SIGTERM),
            {tup[0] for tup in sigs},
        )
        self.assertIn(
            ('*****@*****.**', 11, signal.SIGTERM),
            {tup[0] for tup in sigs},
        )
        self.t.signal_node.return_value = False
        callback.assert_called()
        self.t.stop(['foo', 'bar', 'baz'], 'celery worker', callback=None)

        def on_node_alive(pid):
            if node_alive.call_count > 4:
                return True
            return False
        self.t.signal_node.return_value = True
        self.t.node_alive.side_effect = on_node_alive
        self.t.stop(['foo', 'bar', 'baz'], 'celery worker', retry=True)
Пример #4
0
 def test_get_log_policy(self):
     einfo = Mock(name='einfo')
     einfo.internal = False
     self.assertIs(
         get_log_policy(self.add, einfo, Reject()),
         log_policy_reject,
     )
     self.assertIs(
         get_log_policy(self.add, einfo, Ignore()),
         log_policy_ignore,
     )
     self.add.throws = (TypeError,)
     self.assertIs(
         get_log_policy(self.add, einfo, KeyError()),
         log_policy_unexpected,
     )
     self.assertIs(
         get_log_policy(self.add, einfo, TypeError()),
         log_policy_expected,
     )
     einfo2 = Mock(name='einfo2')
     einfo2.internal = True
     self.assertIs(
         get_log_policy(self.add, einfo2, KeyError()),
         log_policy_internal,
     )
Пример #5
0
    def test__green_pidbox_node(self):
        pool = Mock()
        pool.is_green = True
        l = MyKombuConsumer(self.buffer.put, timer=self.timer, pool=pool, app=self.app)
        l.node = Mock()
        controller = find_step(l, consumer.Control)

        class BConsumer(Mock):
            def __enter__(self):
                self.consume()
                return self

            def __exit__(self, *exc_info):
                self.cancel()

        controller.box.node.listen = BConsumer()
        connections = []

        class Connection(object):
            calls = 0

            def __init__(self, obj):
                connections.append(self)
                self.obj = obj
                self.default_channel = self.channel()
                self.closed = False

            def __enter__(self):
                return self

            def __exit__(self, *exc_info):
                self.close()

            def channel(self):
                return Mock()

            def as_uri(self):
                return "dummy://"

            def drain_events(self, **kwargs):
                if not self.calls:
                    self.calls += 1
                    raise socket.timeout()
                self.obj.connection = None
                controller.box._node_shutdown.set()

            def close(self):
                self.closed = True

        l.connection = Mock()
        l.connect = lambda: Connection(obj=l)
        controller = find_step(l, consumer.Control)
        controller.box.loop(l)

        self.assertTrue(controller.box.node.listen.called)
        self.assertTrue(controller.box.consumer)
        controller.box.consumer.consume.assert_called_with()

        self.assertIsNone(l.connection)
        self.assertTrue(connections[0].closed)
Пример #6
0
    def test_getpids(self, gethostname, Pidfile):
        gethostname.return_value = 'e.com'
        self.prepare_pidfile_for_getpids(Pidfile)
        callback = Mock()

        p = NamespacedOptionParser(['foo', 'bar', 'baz'])
        nodes = self.t.getpids(p, 'celery worker', callback=callback)
        node_0, node_1 = nodes
        self.assertEqual(node_0[0], '*****@*****.**')
        self.assertEqual(
            sorted(node_0[1]),
            sorted(('celery worker', '--pidfile=foo.pid',
                    '-n [email protected]', '')),
        )
        self.assertEqual(node_0[2], 10)

        self.assertEqual(node_1[0], '*****@*****.**')
        self.assertEqual(
            sorted(node_1[1]),
            sorted(('celery worker', '--pidfile=bar.pid',
                    '-n [email protected]', '')),
        )
        self.assertEqual(node_1[2], 11)
        callback.assert_called()
        cargs, _ = callback.call_args
        self.assertEqual(cargs[0], '*****@*****.**')
        self.assertItemsEqual(
            cargs[1],
            ['celery worker', '--pidfile=baz.pid', '-n [email protected]', ''],
        )
        self.assertIsNone(cargs[2])
        self.assertIn('DOWN', self.fh.getvalue())

        # without callback, should work
        nodes = self.t.getpids(p, 'celery worker', callback=None)
Пример #7
0
 def test_on_message__task(self):
     c = self.Consumer()
     g = Gossip(c)
     self.assertTrue(g.enabled)
     message = Mock(name='message')
     message.delivery_info = {'routing_key': 'task.failed'}
     g.on_message(Mock(name='prepare'), message)
Пример #8
0
    def test_enter__exit(self):
        hub = Hub()
        P = hub.poller = Mock()
        on_close = Mock()
        hub.on_close.add(on_close)

        try:
            read_A = Mock()
            read_B = Mock()
            hub.add_reader(10, read_A)
            hub.add_reader(File(11), read_B)
            write_A = Mock()
            write_B = Mock()
            hub.add_writer(20, write_A)
            hub.add_writer(File(21), write_B)
            self.assertTrue(hub.readers)
            self.assertTrue(hub.writers)
        finally:
            hub.close()
        self.assertFalse(hub.readers)
        self.assertFalse(hub.writers)

        P.unregister.assert_has_calls([call(10), call(11), call(20), call(21)], any_order=True)

        on_close.assert_called_with(hub)
Пример #9
0
 def test_prepare_prog_name(self):
     x = CeleryCommand(app=self.app)
     main = Mock(name='__main__')
     main.__file__ = '/opt/foo.py'
     with patch.dict(sys.modules, __main__=main):
         self.assertEqual(x.prepare_prog_name('__main__.py'), '/opt/foo.py')
         self.assertEqual(x.prepare_prog_name('celery'), 'celery')
Пример #10
0
    def test_annotate_decorator(self):
        from celery.app.task import Task

        class adX(Task):
            abstract = True

            def run(self, y, z, x):
                return y, z, x

        check = Mock()

        def deco(fun):

            def _inner(*args, **kwargs):
                check(*args, **kwargs)
                return fun(*args, **kwargs)
            return _inner

        self.app.conf.task_annotations = {
            adX.name: {'@__call__': deco}
        }
        adX.bind(self.app)
        self.assertIs(adX.app, self.app)

        i = adX()
        i(2, 4, x=3)
        check.assert_called_with(i, 2, 4, x=3)

        i.annotate()
        i.annotate()
Пример #11
0
 def test_format_object(self, _format):
     x = ColorFormatter()
     x.use_color = True
     record = Mock()
     record.levelname = 'ERROR'
     record.msg = object()
     self.assertTrue(x.format(record))
Пример #12
0
 def test_module_file(self):
     m1 = Mock()
     m1.__file__ = '/opt/foo/xyz.pyc'
     self.assertEqual(module_file(m1), '/opt/foo/xyz.py')
     m2 = Mock()
     m2.__file__ = '/opt/foo/xyz.py'
     self.assertEqual(module_file(m1), '/opt/foo/xyz.py')
Пример #13
0
 def test_handle_process_exit__signalled(self):
     on_signalled = Mock(name='on_signalled')
     self.assertEqual(
         self.node.handle_process_exit(-9, on_signalled=on_signalled),
         9,
     )
     on_signalled.assert_called_with(self.node, 9)
Пример #14
0
 def test_prepare_prog_name(self):
     x = CeleryCommand(app=self.app)
     main = Mock(name="__main__")
     main.__file__ = "/opt/foo.py"
     with patch.dict(sys.modules, __main__=main):
         self.assertEqual(x.prepare_prog_name("__main__.py"), "/opt/foo.py")
         self.assertEqual(x.prepare_prog_name("celery"), "celery")
Пример #15
0
 def test_link_error(self):
     g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app)
     sig = Mock(name='sig')
     g1.link_error(sig)
     g1.tasks[0].link_error.assert_called_with(
         sig.clone().set(immutable=True),
     )
Пример #16
0
    def test_simple(self):
        self.assertTrue(jsonify(Queue('foo')))
        self.assertTrue(jsonify(['foo', 'bar', 'baz']))
        self.assertTrue(jsonify({'foo': 'bar'}))
        self.assertTrue(jsonify(datetime.utcnow()))
        self.assertTrue(jsonify(datetime.utcnow().replace(tzinfo=pytz.utc)))
        self.assertTrue(jsonify(datetime.utcnow().replace(microsecond=0)))
        self.assertTrue(jsonify(date(2012, 1, 1)))
        self.assertTrue(jsonify(time(hour=1, minute=30)))
        self.assertTrue(jsonify(time(hour=1, minute=30, microsecond=3)))
        self.assertTrue(jsonify(timedelta(seconds=30)))
        self.assertTrue(jsonify(10))
        self.assertTrue(jsonify(10.3))
        self.assertTrue(jsonify('hello'))

        unknown_type_filter = Mock()
        obj = object()
        self.assertIs(
            jsonify(obj, unknown_type_filter=unknown_type_filter),
            unknown_type_filter.return_value,
        )
        unknown_type_filter.assert_called_with(obj)

        with self.assertRaises(ValueError):
            jsonify(obj)
Пример #17
0
    def test_limit_task(self):
        c = self.get_consumer()
        c.timer = Mock()

        bucket = Mock()
        request = Mock()
        bucket.can_consume.return_value = True
        bucket.contents = deque()

        c._limit_task(request, bucket, 3)
        bucket.can_consume.assert_called_with(3)
        bucket.expected_time.assert_called_with(3)
        c.timer.call_after.assert_called_with(
            bucket.expected_time(), c._on_bucket_wakeup, (bucket, 3),
            priority=c._limit_order,
        )

        bucket.can_consume.return_value = False
        bucket.expected_time.return_value = 3.33
        limit_order = c._limit_order
        c._limit_task(request, bucket, 4)
        self.assertEqual(c._limit_order, limit_order + 1)
        bucket.can_consume.assert_called_with(4)
        c.timer.call_after.assert_called_with(
            3.33, c._on_bucket_wakeup, (bucket, 4),
            priority=c._limit_order,
        )
        bucket.expected_time.assert_called_with(4)
Пример #18
0
    def test_pool_restart_import_modules(self):
        consumer = Consumer(self.app)
        consumer.controller = _WC(app=self.app)
        consumer.controller.consumer = consumer
        consumer.controller.pool.restart = Mock()
        consumer.reset_rate_limits = Mock(name='reset_rate_limits()')
        consumer.update_strategies = Mock(name='update_strategies()')
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        assert panel.state.consumer.controller.consumer is consumer
        _import = consumer.controller.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        self.app.conf.worker_pool_restarts = True
        panel.handle('pool_restart', {'modules': ['foo', 'bar'],
                                      'reloader': _reload})

        consumer.controller.pool.restart.assert_called()
        consumer.reset_rate_limits.assert_called_with()
        consumer.update_strategies.assert_called_with()
        _reload.assert_not_called()
        self.assertItemsEqual(
            [call('bar'), call('foo')],
            _import.call_args_list,
        )
Пример #19
0
 def test_find_module(self):
     self.assertTrue(find_module('celery'))
     imp = Mock()
     imp.return_value = None
     with self.assertRaises(NotAPackage):
         find_module('foo.bar.baz', imp=imp)
     self.assertTrue(find_module('celery.worker.request'))
Пример #20
0
 def test_election__no_gossip(self):
     consumer = Mock(name='consumer')
     consumer.gossip = None
     panel = self.create_panel(consumer=consumer)
     panel.handle(
         'election', {'id': 'id', 'topic': 'topic', 'action': 'action'},
     )
Пример #21
0
    def test_pool_restart_reload_modules(self):
        consumer = Consumer(self.app)
        consumer.controller = _WC(app=self.app)
        consumer.controller.consumer = consumer
        consumer.controller.pool.restart = Mock()
        consumer.reset_rate_limits = Mock(name='reset_rate_limits()')
        consumer.update_strategies = Mock(name='update_strategies()')
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = panel.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        self.app.conf.CELERYD_POOL_RESTARTS = True
        with patch.dict(sys.modules, {'foo': None}):
            panel.handle('pool_restart', {'modules': ['foo'],
                                          'reload': False,
                                          'reloader': _reload})

            self.assertTrue(consumer.controller.pool.restart.called)
            self.assertFalse(_reload.called)
            self.assertFalse(_import.called)

            _import.reset_mock()
            _reload.reset_mock()
            consumer.controller.pool.restart.reset_mock()

            panel.handle('pool_restart', {'modules': ['foo'],
                                          'reload': True,
                                          'reloader': _reload})

            self.assertTrue(consumer.controller.pool.restart.called)
            self.assertTrue(_reload.called)
            self.assertFalse(_import.called)
Пример #22
0
    def test_pool_restart(self):
        consumer = Consumer(self.app)
        consumer.controller = _WC(app=self.app)
        consumer.controller.consumer = consumer
        consumer.controller.pool.restart = Mock()
        consumer.reset_rate_limits = Mock(name='reset_rate_limits()')
        consumer.update_strategies = Mock(name='update_strategies()')
        consumer.event_dispatcher = Mock(name='evd')
        panel = self.create_panel(consumer=consumer)
        assert panel.state.consumer.controller.consumer is consumer
        panel.app = self.app
        _import = panel.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        with self.assertRaises(ValueError):
            panel.handle('pool_restart', {'reloader': _reload})

        self.app.conf.worker_pool_restarts = True
        panel.handle('pool_restart', {'reloader': _reload})
        consumer.controller.pool.restart.assert_called()
        consumer.reset_rate_limits.assert_called_with()
        consumer.update_strategies.assert_called_with()
        _reload.assert_not_called()
        _import.assert_not_called()
        consumer.controller.pool.restart.side_effect = NotImplementedError()
        panel.handle('pool_restart', {'reloader': _reload})
        consumer.controller.consumer = None
        panel.handle('pool_restart', {'reloader': _reload})
Пример #23
0
 def test_Pool_crate_threaded(self):
     w = Mock()
     w._conninfo.connection_errors = w._conninfo.channel_errors = ()
     w.pool_cls = Mock()
     w.use_eventloop = False
     pool = components.Pool(w)
     pool.create(w)
Пример #24
0
    def test_Pool_create(self):
        from kombu.async.semaphore import LaxBoundedSemaphore
        w = Mock()
        w._conninfo.connection_errors = w._conninfo.channel_errors = ()
        w.hub = Mock()

        PoolImp = Mock()
        poolimp = PoolImp.return_value = Mock()
        poolimp._pool = [Mock(), Mock()]
        poolimp._cache = {}
        poolimp._fileno_to_inq = {}
        poolimp._fileno_to_outq = {}

        from celery.concurrency.prefork import TaskPool as _TaskPool

        class MockTaskPool(_TaskPool):
            Pool = PoolImp

            @property
            def timers(self):
                return {Mock(): 30}

        w.pool_cls = MockTaskPool
        w.use_eventloop = True
        w.consumer.restart_count = -1
        pool = components.Pool(w)
        pool.create(w)
        pool.register_with_event_loop(w, w.hub)
        self.assertIsInstance(w.semaphore, LaxBoundedSemaphore)
        P = w.pool
        P.start()
Пример #25
0
 def test_handle_process_exit__failure(self):
     on_failure = Mock(name='on_failure')
     self.assertEqual(
         self.node.handle_process_exit(9, on_failure=on_failure),
         9,
     )
     on_failure.assert_called_with(self.node, 9)
Пример #26
0
    def test_call_repeatedly(self):
        t = timer2.Timer()
        try:
            t.schedule.enter_after = Mock()

            myfun = Mock()
            myfun.__name__ = 'myfun'
            t.call_repeatedly(0.03, myfun)

            self.assertEqual(t.schedule.enter_after.call_count, 1)
            args1, _ = t.schedule.enter_after.call_args_list[0]
            sec1, tref1, _ = args1
            self.assertEqual(sec1, 0.03)
            tref1()

            self.assertEqual(t.schedule.enter_after.call_count, 2)
            args2, _ = t.schedule.enter_after.call_args_list[1]
            sec2, tref2, _ = args2
            self.assertEqual(sec2, 0.03)
            tref2.cancelled = True
            tref2()

            self.assertEqual(t.schedule.enter_after.call_count, 2)
        finally:
            t.stop()
Пример #27
0
    def test_apply_timeout(self):

            class Timeout(Exception):
                value = None

                def __init__(self, value):
                    self.__class__.value = value

                def __enter__(self):
                    return self

                def __exit__(self, *exc_info):
                    pass
            timeout_callback = Mock(name='timeout_callback')
            apply_target = Mock(name='apply_target')
            apply_timeout(
                Mock(), timeout=10, callback=Mock(name='callback'),
                timeout_callback=timeout_callback,
                apply_target=apply_target, Timeout=Timeout,
            )
            self.assertEqual(Timeout.value, 10)
            self.assertTrue(apply_target.called)

            apply_target.side_effect = Timeout(10)
            apply_timeout(
                Mock(), timeout=10, callback=Mock(),
                timeout_callback=timeout_callback,
                apply_target=apply_target, Timeout=Timeout,
            )
            timeout_callback.assert_called_with(False, 10)
Пример #28
0
 def test_dump_body_buffer(self):
     msg = Mock()
     msg.body = 'str'
     try:
         buf = buffer(msg.body)
     except NameError:
         raise SkipTest('buffer type not available')
     self.assertTrue(dump_body(msg, buf))
Пример #29
0
    def test_stop(self):
        c = Mock()
        tasks = Tasks(c)
        self.assertIsNone(c.task_consumer)
        self.assertIsNone(c.qos)

        c.task_consumer = Mock()
        tasks.stop(c)
Пример #30
0
 def test_format_raises_no_color(self, safe_str):
     x = ColorFormatter(use_color=False)
     record = Mock()
     record.levelname = 'ERROR'
     record.msg = 'HELLO'
     record.exc_text = 'error text'
     x.format(record)
     self.assertEqual(safe_str.call_count, 1)
Пример #31
0
    def test_on_message(self):
        c = self.Consumer()
        g = Gossip(c)
        self.assertTrue(g.enabled)
        prepare = Mock()
        prepare.return_value = 'worker-online', {}
        c.app.events.State.assert_called_with(
            on_node_join=g.on_node_join,
            on_node_leave=g.on_node_leave,
            max_tasks_in_memory=1,
        )
        g.update_state = Mock()
        worker = Mock()
        g.on_node_join = Mock()
        g.on_node_leave = Mock()
        g.update_state.return_value = worker, 1
        message = Mock()
        message.delivery_info = {'routing_key': 'worker-online'}
        message.headers = {'hostname': 'other'}

        handler = g.event_handlers['worker-online'] = Mock()
        g.on_message(prepare, message)
        handler.assert_called_with(message.payload)
        g.event_handlers = {}

        g.on_message(prepare, message)

        message.delivery_info = {'routing_key': 'worker-offline'}
        prepare.return_value = 'worker-offline', {}
        g.on_message(prepare, message)

        message.delivery_info = {'routing_key': 'worker-baz'}
        prepare.return_value = 'worker-baz', {}
        g.update_state.return_value = worker, 0
        g.on_message(prepare, message)

        message.headers = {'hostname': g.hostname}
        g.on_message(prepare, message)
        g.clock.forward.assert_called_with()
Пример #32
0
 def test_send_task_message__no_kwargs(self):
     self.app.amqp.send_task_message(Mock(), 'foo', self.simple_message)
Пример #33
0
 def test_active_queues__empty(self):
     consumer = Mock(name='consumer')
     panel = self.create_panel(consumer=consumer)
     consumer.task_consumer = None
     self.assertFalse(panel.handle('active_queues'))
Пример #34
0
 def __init__(self, *args, **kwargs):
     self.channel = Mock()
Пример #35
0
 def test_error(self):
     self.cmd.out = Mock()
     self.cmd.error('FOO')
     self.cmd.out.assert_called()
Пример #36
0
 def test_restore_group_no_entry(self):
     x = MongoBackend(app=self.app)
     x.collection = Mock()
     fo = x.collection.find_one = Mock()
     fo.return_value = None
     self.assertIsNone(x._restore_group('1f3fab'))
Пример #37
0
 def test_stop_already_stopped(self):
     c = Mock()
     tasks = Tasks(c)
     tasks.stop(c)
Пример #38
0
 def test_move_by_idmap(self):
     with patch('celery.contrib.migrate.move') as move:
         move_by_idmap({'123f': Queue('foo')})
         self.assertTrue(move.called)
         cb = move.call_args[0][0]
         self.assertTrue(cb({'id': '123f'}, Mock()))
Пример #39
0
    def test_migrate(self, name='testcelery'):
        x = Connection('memory://foo')
        y = Connection('memory://foo')
        # use separate state
        x.default_channel.queues = {}
        y.default_channel.queues = {}

        ex = Exchange(name, 'direct')
        q = Queue(name, exchange=ex, routing_key=name)
        q(x.default_channel).declare()
        Producer(x).publish('foo', exchange=name, routing_key=name)
        Producer(x).publish('bar', exchange=name, routing_key=name)
        Producer(x).publish('baz', exchange=name, routing_key=name)
        self.assertTrue(x.default_channel.queues)
        self.assertFalse(y.default_channel.queues)

        migrate_tasks(x, y, accept=['text/plain'], app=self.app)

        yq = q(y.default_channel)
        self.assertEqual(yq.get().body, ensure_bytes('foo'))
        self.assertEqual(yq.get().body, ensure_bytes('bar'))
        self.assertEqual(yq.get().body, ensure_bytes('baz'))

        Producer(x).publish('foo', exchange=name, routing_key=name)
        callback = Mock()
        migrate_tasks(x,
                      y,
                      callback=callback,
                      accept=['text/plain'],
                      app=self.app)
        self.assertTrue(callback.called)
        migrate = Mock()
        Producer(x).publish('baz', exchange=name, routing_key=name)
        migrate_tasks(x,
                      y,
                      callback=callback,
                      migrate=migrate,
                      accept=['text/plain'],
                      app=self.app)
        self.assertTrue(migrate.called)

        with patch('kombu.transport.virtual.Channel.queue_declare') as qd:

            def effect(*args, **kwargs):
                if kwargs.get('passive'):
                    raise ChannelError('some channel error')
                return 0, 3, 0

            qd.side_effect = effect
            migrate_tasks(x, y, app=self.app)

        x = Connection('memory://')
        x.default_channel.queues = {}
        y.default_channel.queues = {}
        callback = Mock()
        migrate_tasks(x,
                      y,
                      callback=callback,
                      accept=['text/plain'],
                      app=self.app)
        self.assertFalse(callback.called)
Пример #40
0
 def test_filter_status(self):
     with override_stdouts() as (stdout, stderr):
         filter_status(State(), {'id': '1', 'task': 'add'}, Mock())
         self.assertTrue(stdout.getvalue())
Пример #41
0
 def test_main(self, Command):
     cmd = Command.return_value = Mock()
     mainfun()
     cmd.execute_from_commandline.assert_called_with(None)
Пример #42
0
 def test_compat_command_decorator(self):
     with patch('celery.bin.celery.CeleryCommand') as CC:
         self.assertEqual(command(), CC.register_command)
         fun = Mock(name='fun')
         command(fun)
         CC.register_command.assert_called_with(fun)
Пример #43
0
 def test_on_elect_ack_win(self):
     c = self.Consumer(hostname='*****@*****.**')  # I will win
     g = Gossip(c)
     handler = g.election_handlers['topic'] = Mock()
     self.setup_election(g, c)
     handler.assert_called_with('action')
Пример #44
0
    def test_apply_timeout(self):
        class Timeout(Exception):
            value = None

            def __init__(self, value):
                self.__class__.value = value

            def __enter__(self):
                return self

            def __exit__(self, *exc_info):
                pass

        timeout_callback = Mock(name='timeout_callback')
        apply_target = Mock(name='apply_target')
        apply_timeout(
            Mock(),
            timeout=10,
            callback=Mock(name='callback'),
            timeout_callback=timeout_callback,
            apply_target=apply_target,
            Timeout=Timeout,
        )
        self.assertEqual(Timeout.value, 10)
        apply_target.assert_called()

        apply_target.side_effect = Timeout(10)
        apply_timeout(
            Mock(),
            timeout=10,
            callback=Mock(),
            timeout_callback=timeout_callback,
            apply_target=apply_target,
            Timeout=Timeout,
        )
        timeout_callback.assert_called_with(False, 10)
Пример #45
0
 def Consumer(self, hostname='*****@*****.**', pid=4312):
     c = Mock()
     c.app.connection = _amqp_connection()
     c.hostname = hostname
     c.pid = pid
     return c
Пример #46
0
 def client(self):
     return Mock()
Пример #47
0
 def test_register_with_event_loop(self):
     c = self.get_consumer()
     c.register_with_event_loop(Mock(name='loop'))
Пример #48
0
 def test_resets(self):
     parent = Mock()
     g = gPidbox(parent)
     g._resets = 100
     g.reset()
     self.assertEqual(g._resets, 101)
Пример #49
0
 def test_out(self):
     f = Mock()
     self.cmd.out('foo', f)
Пример #50
0
    def test_pool_restart_reload_modules(self):
        consumer = Consumer(self.app)
        consumer.controller = _WC(app=self.app)
        consumer.controller.consumer = consumer
        consumer.controller.pool.restart = Mock()
        consumer.reset_rate_limits = Mock(name='reset_rate_limits()')
        consumer.update_strategies = Mock(name='update_strategies()')
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = panel.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        self.app.conf.worker_pool_restarts = True
        with patch.dict(sys.modules, {'foo': None}):
            panel.handle('pool_restart', {
                'modules': ['foo'],
                'reload': False,
                'reloader': _reload
            })

            consumer.controller.pool.restart.assert_called()
            _reload.assert_not_called()
            _import.assert_not_called()

            _import.reset_mock()
            _reload.reset_mock()
            consumer.controller.pool.restart.reset_mock()

            panel.handle('pool_restart', {
                'modules': ['foo'],
                'reload': True,
                'reloader': _reload
            })

            consumer.controller.pool.restart.assert_called()
            _reload.assert_called()
            _import.assert_not_called()
Пример #51
0
 def test_main_KeyboardInterrupt(self, Command):
     cmd = Command.return_value = Mock()
     cmd.execute_from_commandline.side_effect = KeyboardInterrupt()
     mainfun()
     cmd.execute_from_commandline.assert_called_with(None)
Пример #52
0
 def open_file(filename, *args, **kwargs):
     f = files[filename]
     f.fileno = Mock()
     f.fileno.return_value = 99
     return f
Пример #53
0
 def test_conf(self):
     i = inspect(app=self.app)
     i.call = Mock(name='call')
     i.conf(with_defaults=True, foo=1)
     i.call.assert_called_with('conf', True, foo=1)
Пример #54
0
 def test_on_usage_error(self):
     x = CeleryCommand(app=self.app)
     x.error = Mock()
     x.on_usage_error(x.UsageError('foo'), command=None)
     x.error.assert_called()
     x.on_usage_error(x.UsageError('foo'), command='dummy')
Пример #55
0
 def test_objgraph(self):
     i = inspect(app=self.app)
     i.call = Mock(name='call')
     i.objgraph('Message', foo=1)
     i.call.assert_called_with('objgraph', 'Message', foo=1)
Пример #56
0
 def control(self, patch_call, *args, **kwargs):
     kwargs.setdefault('app', Mock(name='app'))
     c = control(*args, **kwargs)
     if patch_call:
         c.call = Mock(name='control.call')
     return c
Пример #57
0
 def test_task_id_eq(self):
     self.assertTrue(task_id_eq('A', {'id': 'A'}, Mock()))
     self.assertFalse(task_id_eq('A', {'id': 'B'}, Mock()))
Пример #58
0
 def test_maybe_queue(self):
     app = Mock()
     app.amqp.queues = {'foo': 313}
     self.assertEqual(_maybe_queue(app, 'foo'), 313)
     self.assertEqual(_maybe_queue(app, Queue('foo')), Queue('foo'))
Пример #59
0
 def test_task_id_in(self):
     self.assertTrue(task_id_in(['A'], {'id': 'A'}, Mock()))
     self.assertFalse(task_id_in(['A'], {'id': 'B'}, Mock()))
Пример #60
0
 def test_load_extensions_no_commands(self):
     with patch('celery.bin.celery.Extensions') as Ext:
         ext = Ext.return_value = Mock(name='Extension')
         ext.load.return_value = None
         x = CeleryCommand(app=self.app)
         x.load_extension_commands()