Esempio n. 1
0
    def test_Pool_create(self):
        from celery.worker.hub import BoundedSemaphore

        w = Mock()
        w.hub = Mock()
        w.hub.on_init = []
        w.pool_cls = Mock()
        P = w.pool_cls.return_value = Mock()
        P.timers = {Mock(): 30}
        w.use_eventloop = True
        pool = Pool(w)
        pool.create(w)
        self.assertIsInstance(w.semaphore, BoundedSemaphore)
        self.assertTrue(w.hub.on_init)

        hub = Mock()
        w.hub.on_init[0](hub)

        cbs = w.pool.init_callbacks.call_args[1]
        w = Mock()
        cbs["on_process_up"](w)
        hub.add_reader.assert_called_with(w.sentinel, P.maintain_pool)

        cbs["on_process_down"](w)
        hub.remove.assert_called_with(w.sentinel)

        result = Mock()
        tref = result._tref

        cbs["on_timeout_cancel"](result)
        tref.cancel.assert_called_with()
        cbs["on_timeout_cancel"](result)  # no more tref

        cbs["on_timeout_set"](result, 10, 20)
        tsoft, callback = hub.timer.apply_after.call_args[0]
        callback()

        cbs["on_timeout_set"](result, 10, None)
        tsoft, callback = hub.timer.apply_after.call_args[0]
        callback()
        cbs["on_timeout_set"](result, None, 10)
        cbs["on_timeout_set"](result, None, None)

        P.did_start_ok.return_value = False
        with self.assertRaises(WorkerLostError):
            pool.on_poll_init(P, hub)
Esempio n. 2
0
    def test_Pool_create(self):
        from celery.worker.hub import BoundedSemaphore
        w = Mock()
        w.hub = Mock()
        w.hub.on_init = []
        w.pool_cls = Mock()
        P = w.pool_cls.return_value = Mock()
        P.timers = {Mock(): 30}
        w.use_eventloop = True
        pool = Pool(w)
        pool.create(w)
        self.assertIsInstance(w.semaphore, BoundedSemaphore)
        self.assertTrue(w.hub.on_init)

        hub = Mock()
        w.hub.on_init[0](hub)

        cbs = w.pool.init_callbacks.call_args[1]
        w = Mock()
        cbs['on_process_up'](w)
        hub.add_reader.assert_called_with(w.sentinel, P.maintain_pool)

        cbs['on_process_down'](w)
        hub.remove.assert_called_with(w.sentinel)

        result = Mock()
        tref = result._tref

        cbs['on_timeout_cancel'](result)
        tref.cancel.assert_called_with()
        cbs['on_timeout_cancel'](result)  # no more tref

        cbs['on_timeout_set'](result, 10, 20)
        tsoft, callback = hub.timer.apply_after.call_args[0]
        callback()

        cbs['on_timeout_set'](result, 10, None)
        tsoft, callback = hub.timer.apply_after.call_args[0]
        callback()
        cbs['on_timeout_set'](result, None, 10)
        cbs['on_timeout_set'](result, None, None)

        P.did_start_ok.return_value = False
        with self.assertRaises(WorkerLostError):
            pool.on_poll_init(P, hub)