Exemple #1
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):
         assert x.prepare_prog_name('__main__.py') == '/opt/foo.py'
         assert x.prepare_prog_name('celery') == 'celery'
Exemple #2
0
def test_A_simplejson():
    with mock.reset_modules('thorn.utils.json'):
        from thorn.utils import json
        obj = Mock(name='obj')
        encode = Mock(name='encode')
        assert json.dumps(obj, encode=encode) is encode.return_value
        encode.assert_called_with(obj, cls=json.JsonEncoder)
Exemple #3
0
def test_module_file():
    m1 = Mock()
    m1.__file__ = '/opt/foo/xyz.pyc'
    assert module_file(m1) == '/opt/foo/xyz.py'
    m2 = Mock()
    m2.__file__ = '/opt/foo/xyz.py'
    assert module_file(m1) == '/opt/foo/xyz.py'
Exemple #4
0
    def test_enter__exit(self):
        P = self.hub.poller = Mock()
        on_close = Mock()
        self.hub.on_close.add(on_close)

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

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

        on_close.assert_called_with(self.hub)
Exemple #5
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 pytest.raises(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})
Exemple #6
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),
     )
Exemple #7
0
    def test_call_repeatedly(self):
        t = Timer()
        try:
            t.schedule.enter_after = Mock()

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

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

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

            assert t.schedule.enter_after.call_count == 2
        finally:
            t.stop()
Exemple #8
0
 def test_on_message_ready(self):
     channel = Mock(name='channel')
     msg = Mock(name='msg')
     callback = Mock(name='callback')
     self.transport._callbacks = {'q1': callback}
     self.transport.on_message_ready(channel, msg, queue='q1')
     callback.assert_called_with(msg)
Exemple #9
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)
        assert 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)
Exemple #10
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)
     assert w.process_task is w._process_task_sem
Exemple #11
0
    def test_on_chord_part_return(self, restore):
        tb = CacheBackend(backend='memory://', app=self.app)

        deps = Mock()
        deps.__len__ = Mock()
        deps.__len__.return_value = 2
        restore.return_value = deps
        task = Mock()
        task.name = 'foobarbaz'
        self.app.tasks['foobarbaz'] = task
        task.request.chord = signature(task)

        result = self.app.GroupResult(
            uuid(),
            [self.app.AsyncResult(uuid()) for _ in range(3)],
        )
        task.request.group = result.id
        tb.apply_chord(result, None)

        deps.join_native.assert_not_called()
        tb.on_chord_part_return(task.request, 'SUCCESS', 10)
        deps.join_native.assert_not_called()

        tb.on_chord_part_return(task.request, 'SUCCESS', 10)
        deps.join_native.assert_called_with(propagate=True, timeout=3.0)
        deps.delete.assert_called_with()
Exemple #12
0
 def test_release_releases_connection(self):
     p = Mock()
     p.__connection__ = Mock()
     self.pool.release(p)
     p.__connection__.release.assert_called_with()
     p.__connection__ = None
     self.pool.release(p)
Exemple #13
0
 def test_release(self):
     p = Mock()
     p.channel = Mock()
     p.__connection__ = Mock()
     self.pool.release(p)
     p.__connection__.release.assert_called_with()
     assert p.channel is None
Exemple #14
0
 def test_reverse(self):
     callback = Mock()
     x = barrier(self.ps, callback=promise(callback))
     for p in self.ps:
         p()
     assert x.ready
     callback.assert_called_with()
Exemple #15
0
 def test_add_queue_when_auto_declare(self):
     consumer = self.connection.Consumer(auto_declare=True)
     q = Mock()
     q.return_value = q
     consumer.add_queue(q)
     assert q in consumer.queues
     q.declare.assert_called_with()
Exemple #16
0
 def test_format_object(self, _format):
     x = ColorFormatter()
     x.use_color = True
     record = Mock()
     record.levelname = 'ERROR'
     record.msg = object()
     assert x.format(record)
Exemple #17
0
 def test_add_queue_when_not_auto_declare(self):
     consumer = self.connection.Consumer(auto_declare=False)
     q = Mock()
     q.return_value = q
     consumer.add_queue(q)
     assert q in consumer.queues
     assert not q.declare.call_count
Exemple #18
0
    def test_annotate_decorator(self):
        from celery.app.task import Task

        class adX(Task):

            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)
        assert adX.app is self.app

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

        i.annotate()
        i.annotate()
Exemple #19
0
 def test_on_pre_save__disabled_on_object_creation(self):
     self.dispatch.use_transitions = True
     instance = Mock(Name='instance')
     instance.pk = None
     sender = Mock(name='sender')
     self.dispatch.on_pre_save(instance, sender, raw=False)
     sender.objects.get.assert_not_called()
Exemple #20
0
 def test_Pool_create_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)
Exemple #21
0
    def test_close_connection(self):
        connection = Mock()
        connection.client = Mock()
        self.transport.close_connection(connection)

        assert connection.client is None
        connection.close.assert_called_with()
Exemple #22
0
    def test_Pool_create(self):
        from kombu.asynchronous.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)
        if sys.platform != 'win32':
            assert isinstance(w.semaphore, LaxBoundedSemaphore)
            P = w.pool
            P.start()
Exemple #23
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'},
     )
Exemple #24
0
    def test_revive_connection(self):
        on_revive = Mock()
        channel = Mock()
        common.revive_connection(Mock(), channel, on_revive)
        on_revive.assert_called_with(channel)

        common.revive_connection(Mock(), channel, None)
Exemple #25
0
    def test_pool_restart_import_modules(self, _debug):
        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
        with patch('sys.modules'):
            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()
        _import.assert_has_calls([call('bar'), call('foo')], any_order=True)
        assert _import.call_count == 2
Exemple #26
0
    def test_on_poll_start(self):
        p = self.Poller()
        p._channels = []
        p.on_poll_start()
        p._register_BRPOP = Mock(name='_register_BRPOP')
        p._register_LISTEN = Mock(name='_register_LISTEN')

        chan1 = Mock(name='chan1')
        p._channels = [chan1]
        chan1.active_queues = []
        chan1.active_fanout_queues = []
        p.on_poll_start()

        chan1.active_queues = ['q1']
        chan1.active_fanout_queues = ['q2']
        chan1.qos.can_consume.return_value = False

        p.on_poll_start()
        p._register_LISTEN.assert_called_with(chan1)
        p._register_BRPOP.assert_not_called()

        chan1.qos.can_consume.return_value = True
        p._register_LISTEN.reset_mock()
        p.on_poll_start()

        p._register_BRPOP.assert_called_with(chan1)
        p._register_LISTEN.assert_called_with(chan1)
Exemple #27
0
def test_find_module():
    assert find_module('celery')
    imp = Mock()
    imp.return_value = None
    with pytest.raises(NotAPackage):
        find_module('foo.bar.baz', imp=imp)
    assert find_module('celery.worker.request')
Exemple #28
0
 def test_as_logger(self):
     with patch('amqp.utils.NullHandler') as _NullHandler:
         m = Mock(name='logger')
         m.handlers = None
         x = get_logger(m)
         assert x is m
         x.addHandler.assert_called_with(_NullHandler())
Exemple #29
0
    def test_get_many(self):
        for is_dict in True, False:
            self.b.mget_returns_dict = is_dict
            ids = {uuid(): i for i in range(10)}
            for id, i in items(ids):
                self.b.mark_as_done(id, i)
            it = self.b.get_many(list(ids), interval=0.01)
            for i, (got_id, got_state) in enumerate(it):
                assert got_state['result'] == ids[got_id]
            assert i == 9
            assert list(self.b.get_many(list(ids), interval=0.01))

            self.b._cache.clear()
            callback = Mock(name='callback')
            it = self.b.get_many(
                list(ids),
                on_message=callback,
                interval=0.05
            )
            for i, (got_id, got_state) in enumerate(it):
                assert got_state['result'] == ids[got_id]
            assert i == 9
            assert list(
                self.b.get_many(list(ids), interval=0.01)
            )
            callback.assert_has_calls([
                call(ANY) for id in ids
            ])
Exemple #30
0
 def test_register_with_event_loop(self):
     conn = Mock(name='conn')
     loop = Mock(name='loop')
     self.T.register_with_event_loop(conn, loop)
     loop.add_reader.assert_called_with(
         conn.fileno(), self.T.on_readable, conn, loop,
     )