Esempio n. 1
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})
        self.assertTrue(consumer.controller.pool.restart.called)
        consumer.reset_rate_limits.assert_called_with()
        consumer.update_strategies.assert_called_with()
        self.assertFalse(_reload.called)
        self.assertFalse(_import.called)
        consumer.controller.pool.restart.side_effect = NotImplementedError()
        panel.handle("pool_restart", {"reloader": _reload})
        consumer.controller.consumer = None
        panel.handle("pool_restart", {"reloader": _reload})
Esempio n. 2
0
    def test_pool_restart_relaod_modules(self):
        consumer = Consumer()
        consumer.controller = _WC()
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = panel.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        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)
    def test_pool_restart_relaod_modules(self):
        consumer = Consumer()
        consumer.controller = _WC(app=current_app)
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = panel.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        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)
Esempio n. 4
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.CELERYD_POOL_RESTARTS = True
        panel.handle('pool_restart', {'modules': ['foo', 'bar'],
                                      'reloader': _reload})

        self.assertTrue(consumer.controller.pool.restart.called)
        consumer.reset_rate_limits.assert_called_with()
        consumer.update_strategies.assert_called_with()
        self.assertFalse(_reload.called)
        self.assertItemsEqual(
            [call('bar'), call('foo')],
            _import.call_args_list,
        )
Esempio n. 5
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()
Esempio n. 6
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})
Esempio n. 7
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)
Esempio n. 8
0
 def test_query_task(self):
     consumer = Consumer(self.app)
     consumer.controller = _WC(app=self.app)
     consumer.controller.consumer = consumer
     panel = self.create_panel(consumer=consumer)
     panel.app = self.app
     req1 = Request(
         self.TaskMessage(self.mytask.name, args=(2, 2)),
         app=self.app,
     )
     worker_state.task_reserved(req1)
     try:
         assert not panel.handle('query_task', {'ids': {'1daa'}})
         ret = panel.handle('query_task', {'ids': {req1.id}})
         assert req1.id in ret
         assert ret[req1.id][0] == 'reserved'
         worker_state.active_requests.add(req1)
         try:
             ret = panel.handle('query_task', {'ids': {req1.id}})
             assert ret[req1.id][0] == 'active'
         finally:
             worker_state.active_requests.clear()
         ret = panel.handle('query_task', {'ids': {req1.id}})
         assert ret[req1.id][0] == 'reserved'
     finally:
         worker_state.reserved_requests.clear()
Esempio n. 9
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
Esempio n. 10
0
 def test_query_task(self):
     consumer = Consumer(self.app)
     consumer.controller = _WC(app=self.app)
     consumer.controller.consumer = consumer
     panel = self.create_panel(consumer=consumer)
     panel.app = self.app
     req1 = Request(
         TaskMessage(self.mytask.name, args=(2, 2)),
         app=self.app,
     )
     worker_state.task_reserved(req1)
     try:
         self.assertFalse(panel.handle('query_task', {'ids': {'1daa'}}))
         ret = panel.handle('query_task', {'ids': {req1.id}})
         self.assertIn(req1.id, ret)
         self.assertEqual(ret[req1.id][0], 'reserved')
         worker_state.active_requests.add(req1)
         try:
             ret = panel.handle('query_task', {'ids': {req1.id}})
             self.assertEqual(ret[req1.id][0], 'active')
         finally:
             worker_state.active_requests.clear()
         ret = panel.handle('query_task', {'ids': {req1.id}})
         self.assertEqual(ret[req1.id][0], 'reserved')
     finally:
         worker_state.reserved_requests.clear()
    def test_pool_restart(self):
        consumer = Consumer()
        consumer.controller = _WC(app=current_app)
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = panel.app.loader.import_from_cwd = Mock()
        _reload = Mock()

        panel.handle('pool_restart', {'reloader': _reload})
        self.assertTrue(consumer.controller.pool.restart.called)
        self.assertFalse(_reload.called)
        self.assertFalse(_import.called)
Esempio n. 12
0
    def test_pool_restart_import_modules(self):
        consumer = Consumer()
        consumer.controller = _WC(app=current_app)
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = consumer.controller.app.loader.import_from_cwd = Mock()
        _reload = Mock()

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

        self.assertTrue(consumer.controller.pool.restart.called)
        self.assertFalse(_reload.called)
        self.assertEqual([(("foo",), {}), (("bar",), {})], _import.call_args_list)
    def test_pool_restart_import_modules(self):
        consumer = Consumer()
        consumer.controller = _WC(app=current_app)
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = consumer.controller.app.loader.import_from_cwd = Mock()
        _reload = Mock()

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

        self.assertTrue(consumer.controller.pool.restart.called)
        self.assertFalse(_reload.called)
        self.assertEqual([(('foo',), {}), (('bar',), {})],
                          _import.call_args_list)
Esempio n. 14
0
    def test_pool_restart(self):
        consumer = Consumer(self.app)
        consumer.controller = _WC(app=self.app)
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=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.CELERYD_POOL_RESTARTS = True
        panel.handle('pool_restart', {'reloader': _reload})
        self.assertTrue(consumer.controller.pool.restart.called)
        self.assertFalse(_reload.called)
        self.assertFalse(_import.called)
Esempio n. 15
0
    def test_pool_restart_import_modules(self):
        consumer = Consumer(self.app)
        consumer.controller = _WC(app=self.app)
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = consumer.controller.app.loader.import_from_cwd = Mock()
        _reload = Mock()

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

        self.assertTrue(consumer.controller.pool.restart.called)
        self.assertFalse(_reload.called)
        self.assertItemsEqual(
            [call('bar'), call('foo')],
            _import.call_args_list,
        )
Esempio n. 16
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})

        self.assertTrue(consumer.controller.pool.restart.called)
        consumer.reset_rate_limits.assert_called_with()
        consumer.update_strategies.assert_called_with()
        self.assertFalse(_reload.called)
        self.assertItemsEqual([call("bar"), call("foo")], _import.call_args_list)
Esempio n. 17
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
Esempio n. 18
0
 def test_query_task(self):
     consumer = Consumer(self.app)
     consumer.controller = _WC(app=self.app)
     consumer.controller.consumer = consumer
     panel = self.create_panel(consumer=consumer)
     panel.app = self.app
     req1 = Request(TaskMessage(self.mytask.name, args=(2, 2)), app=self.app)
     worker_state.reserved_requests.add(req1)
     try:
         self.assertFalse(panel.handle("query_task", {"ids": {"1daa"}}))
         ret = panel.handle("query_task", {"ids": {req1.id}})
         self.assertIn(req1.id, ret)
         self.assertEqual(ret[req1.id][0], "reserved")
         worker_state.active_requests.add(req1)
         try:
             ret = panel.handle("query_task", {"ids": {req1.id}})
             self.assertEqual(ret[req1.id][0], "active")
         finally:
             worker_state.active_requests.clear()
         ret = panel.handle("query_task", {"ids": {req1.id}})
         self.assertEqual(ret[req1.id][0], "reserved")
     finally:
         worker_state.reserved_requests.clear()
Esempio n. 19
0
 def test_query_task(self):
     consumer = Consumer(self.app)
     consumer.controller = _WC(app=self.app)
     consumer.controller.consumer = consumer
     panel = self.create_panel(consumer=consumer)
     panel.app = self.app
     req1 = Request(self.TaskMessage(self.mytask.name, args=(2, 2)), app=self.app)
     worker_state.task_reserved(req1)
     try:
         assert not panel.handle("query_task", {"ids": {"1daa"}})
         ret = panel.handle("query_task", {"ids": {req1.id}})
         assert req1.id in ret
         assert ret[req1.id][0] == "reserved"
         worker_state.active_requests.add(req1)
         try:
             ret = panel.handle("query_task", {"ids": {req1.id}})
             assert ret[req1.id][0] == "active"
         finally:
             worker_state.active_requests.clear()
         ret = panel.handle("query_task", {"ids": {req1.id}})
         assert ret[req1.id][0] == "reserved"
     finally:
         worker_state.reserved_requests.clear()
Esempio n. 20
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.CELERYD_POOL_RESTARTS = True
        panel.handle('pool_restart', {'reloader': _reload})
        self.assertTrue(consumer.controller.pool.restart.called)
        consumer.reset_rate_limits.assert_called_with()
        consumer.update_strategies.assert_called_with()
        self.assertFalse(_reload.called)
        self.assertFalse(_import.called)
Esempio n. 21
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.CELERYD_POOL_RESTARTS = True
        panel.handle('pool_restart', {'reloader': _reload})
        self.assertTrue(consumer.controller.pool.restart.called)
        consumer.reset_rate_limits.assert_called_with()
        consumer.update_strategies.assert_called_with()
        self.assertFalse(_reload.called)
        self.assertFalse(_import.called)
    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()
Esempio n. 23
0
    def test_pool_restart_import_modules(self):
        consumer = Consumer(self.app)
        consumer.controller = _WC(app=self.app)
        consumer.controller.pool.restart = Mock()
        panel = self.create_panel(consumer=consumer)
        panel.app = self.app
        _import = consumer.controller.app.loader.import_from_cwd = Mock()
        _reload = Mock()

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

            self.assertTrue(consumer.controller.pool.restart.called)
            self.assertFalse(_reload.called)
            self.assertItemsEqual(
                [call('bar'), call('foo')],
                _import.call_args_list,
            )
        finally:
            self.app.conf.CELERYD_POOL_RESTARTS = False