Pārlūkot izejas kodu

100% coverage for concurrency.threads + bin.celery

Ask Solem 13 gadi atpakaļ
vecāks
revīzija
6bc4a34e42

+ 1 - 0
celery/bin/celery.py

@@ -513,6 +513,7 @@ report = command(report)
 class CeleryCommand(BaseCommand):
     commands = commands
     enable_config_from_cmdline = True
+    prog_name = "celery"
 
     def execute(self, command, argv=None):
         try:

+ 104 - 1
celery/tests/test_bin/test_celery.py

@@ -18,7 +18,6 @@ from celery.bin.celery import (
     inspect,
     status,
     migrate,
-    shell,
     help,
     report,
     CeleryCommand,
@@ -238,3 +237,107 @@ class test_help(AppCase):
         self.assertTrue(out.getvalue())
         self.assertTrue(h.usage("help"))
         h.parser.print_help.assert_called_with()
+
+
+class test_CeleryCommand(AppCase):
+
+    def test_execute_from_commandline(self):
+        x = CeleryCommand(app=self.app)
+        x.handle_argv = Mock()
+        x.handle_argv.return_value = 1
+        with self.assertRaises(SystemExit):
+            x.execute_from_commandline()
+
+        x.handle_argv.return_value = True
+        with self.assertRaises(SystemExit):
+            x.execute_from_commandline()
+
+        x.handle_argv.side_effect = KeyboardInterrupt()
+        with self.assertRaises(SystemExit):
+            x.execute_from_commandline()
+
+    def test_determine_exit_status(self):
+        self.assertEqual(determine_exit_status("true"), EX_OK)
+        self.assertEqual(determine_exit_status(""), EX_FAILURE)
+
+    def test_remove_options_at_beginning(self):
+        x = CeleryCommand(app=self.app)
+        self.assertEqual(x.remove_options_at_beginning(None), [])
+        self.assertEqual(x.remove_options_at_beginning(["-c 3", "--foo"]), [])
+        self.assertEqual(x.remove_options_at_beginning(["--foo", "-c 3"]), [])
+        self.assertEqual(x.remove_options_at_beginning(
+            ["foo", "--foo=1"]), ["foo", "--foo=1"])
+
+    def test_handle_argv(self):
+        x = CeleryCommand(app=self.app)
+        x.execute = Mock()
+        x.handle_argv("celery", [])
+        x.execute.assert_called_with("help", ["help"])
+
+        x.handle_argv("celery", ["start", "foo"])
+        x.execute.assert_called_with("start", ["start", "foo"])
+
+    def test_execute(self):
+        x = CeleryCommand(app=self.app)
+        Help = x.commands["help"] = Mock()
+        help = Help.return_value = Mock()
+        x.execute("fooox", ["a"])
+        help.run_from_argv.assert_called_with(x.prog_name, ["help"])
+        help.reset()
+        x.execute("help", ["help"])
+        help.run_from_argv.assert_called_with(x.prog_name, ["help"])
+
+        Dummy = x.commands["dummy"] = Mock()
+        dummy = Dummy.return_value = Mock()
+        dummy.run_from_argv.side_effect = Error("foo", status="EX_FAILURE")
+        help.reset()
+        x.execute("dummy", ["dummy"])
+        dummy.run_from_argv.assert_called_with(x.prog_name, ["dummy"])
+        help.run_from_argv.assert_called_with(x.prog_name, ["dummy"])
+
+
+class test_inspect(AppCase):
+
+    def test_usage(self):
+        self.assertTrue(inspect(app=self.app).usage("foo"))
+
+    @patch("celery.app.control.Control.inspect")
+    def test_run(self, real):
+        out = WhateverIO()
+        i = inspect(app=self.app, stdout=out)
+        with self.assertRaises(Error):
+            i.run()
+        with self.assertRaises(Error):
+            i.run("help")
+        with self.assertRaises(Error):
+            i.run("xyzzybaz")
+
+        i.run("ping")
+        self.assertTrue(real.called)
+        i.run("ping", destination="foo,bar")
+        self.assertEqual(real.call_args[1]["destination"], ["foo", "bar"])
+        self.assertEqual(real.call_args[1]["timeout"], 0.2)
+        callback = real.call_args[1]["callback"]
+
+        callback({"foo": {"ok": "pong"}})
+        self.assertIn("OK", out.getvalue())
+
+        instance = real.return_value = Mock()
+        instance.ping.return_value = None
+        with self.assertRaises(Error):
+            i.run("ping")
+
+        out.seek(0)
+        out.truncate()
+        i.quiet = True
+        i.say('<-', "hello")
+        self.assertFalse(out.getvalue())
+
+
+class test_main(AppCase):
+
+    @patch("celery.bin.celery.CeleryCommand")
+    def test_main(self, Command):
+        command = Command.return_value = Mock()
+        main()
+        command.execute_from_commandline.assert_called_with()

+ 76 - 0
celery/tests/test_concurrency/test_threads.py

@@ -0,0 +1,76 @@
+from __future__ import absolute_import
+from __future__ import with_statement
+
+import sys
+
+from contextlib import contextmanager
+from mock import Mock
+from types import ModuleType
+
+from celery.concurrency.threads import NullDict, TaskPool, apply_target
+
+from celery.tests.utils import Case, mask_modules
+
+
+class test_NullDict(Case):
+
+    def test_setitem(self):
+        x = NullDict()
+        x["foo"] = 1
+        with self.assertRaises(KeyError):
+            x["foo"]
+
+
+@contextmanager
+def threadpool_module():
+
+    prev = sys.modules.get("threadpool")
+    tp = sys.modules["threadpool"] = ModuleType("threadpool")
+    tp.WorkRequest = Mock()
+    tp.ThreadPool = Mock()
+    yield tp
+    if prev:
+        sys.modules["threadpool"] = prev
+
+
+class test_TaskPool(Case):
+
+    def test_without_threadpool(self):
+
+        with mask_modules("threadpool"):
+            with self.assertRaises(ImportError):
+                TaskPool()
+
+    def test_with_threadpool(self):
+        with threadpool_module():
+            x = TaskPool()
+            self.assertTrue(x.ThreadPool)
+            self.assertTrue(x.WorkRequest)
+
+    def test_on_start(self):
+        with threadpool_module():
+            x = TaskPool()
+            x.on_start()
+            self.assertTrue(x._pool)
+            self.assertIsInstance(x._pool.workRequests, NullDict)
+
+    def test_on_stop(self):
+        with threadpool_module():
+            x = TaskPool()
+            x.on_start()
+            x.on_stop()
+            x._pool.dismissWorkers.assert_called_with(x.limit, do_join=True)
+
+    def test_on_apply(self):
+        with threadpool_module():
+            x = TaskPool()
+            x.on_start()
+            callback = Mock()
+            accept_callback = Mock()
+            target = Mock()
+            req = x.on_apply(target, args=(1, 2), kwargs={"a": 10},
+                callback=callback, accept_callback=accept_callback)
+            x.WorkRequest.assert_called_with(apply_target, (
+                target, (1, 2), {"a": 10}, callback, accept_callback))
+            x._pool.putRequest.assert_called_with(req)
+            x._pool._results_queue.queue.clear.assert_called_with()

+ 0 - 1
setup.cfg

@@ -10,7 +10,6 @@ cover3-exclude = celery
                  celery.backends.database.a805d4bd
                  celery.backends.database.dfd042c7
                  celery.contrib*
-                 celery.concurrency.threads
                  celery.concurrency.gevent
                  celery.backends.mongodb
                  celery.backends.cassandra