From 5d6e20bc2136affb26a73e5ccbb8d15867a4292c Mon Sep 17 00:00:00 2001 From: David Wilson Date: Sat, 17 Aug 2019 19:41:10 +0100 Subject: [PATCH] tests: add a few extra service tests. --- mitogen/core.py | 2 ++ mitogen/service.py | 2 +- tests/service_test.py | 46 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/mitogen/core.py b/mitogen/core.py index 30c0e948..87388620 100644 --- a/mitogen/core.py +++ b/mitogen/core.py @@ -3600,6 +3600,8 @@ class Dispatcher(object): mode, any exception that occurs is recorded, and causes all subsequent calls with the same `chain_id` to fail with the same exception. """ + _service_recv = None + def __repr__(self): return 'Dispatcher' diff --git a/mitogen/service.py b/mitogen/service.py index 168f0140..c8022c04 100644 --- a/mitogen/service.py +++ b/mitogen/service.py @@ -80,7 +80,7 @@ def get_or_create_pool(size=None, router=None): global _pool_pid my_pid = os.getpid() - if _pool is None or my_pid != _pool_pid: + if _pool is None or _pool.closed or my_pid != _pool_pid: # Avoid acquiring heavily contended lock if possible. _pool_lock.acquire() try: diff --git a/tests/service_test.py b/tests/service_test.py index 438766f7..a3e75e14 100644 --- a/tests/service_test.py +++ b/tests/service_test.py @@ -15,6 +15,13 @@ class MyService(mitogen.service.Service): self._counter += 1 return self._counter, id(self) + @mitogen.service.expose(policy=mitogen.service.AllowParents()) + @mitogen.service.arg_spec({ + 'foo': int + }) + def test_arg_spec(self, foo): + return foo + @mitogen.service.expose(policy=mitogen.service.AllowParents()) def privileged_op(self): return 'privileged!' @@ -24,7 +31,6 @@ class MyService(mitogen.service.Service): return 'unprivileged!' - class MyService2(MyService): """ A uniquely named service that lets us test framework activation and class @@ -36,6 +42,44 @@ def call_service_in(context, service_name, method_name): return context.call_service(service_name, method_name) +class CallTest(testlib.RouterMixin, testlib.TestCase): + def test_local(self): + pool = mitogen.service.get_or_create_pool(router=self.router) + self.assertEquals( + 'privileged!', + mitogen.service.call(MyService, 'privileged_op') + ) + pool.stop() + + def test_remote_bad_arg(self): + c1 = self.router.local() + self.assertRaises( + mitogen.core.CallError, + lambda: mitogen.service.call( + MyService.name(), + 'test_arg_spec', + foo='x', + call_context=c1 + ) + ) + + def test_local_unicode(self): + pool = mitogen.service.get_or_create_pool(router=self.router) + self.assertEquals( + 'privileged!', + mitogen.service.call(MyService.name(), 'privileged_op') + ) + pool.stop() + + def test_remote(self): + c1 = self.router.local() + self.assertEquals( + 'privileged!', + mitogen.service.call(MyService, 'privileged_op', + call_context=c1) + ) + + class ActivationTest(testlib.RouterMixin, testlib.TestCase): def test_parent_can_activate(self): l1 = self.router.local()