diff options
author | Joshua Harlow <harlowja@yahoo-inc.com> | 2014-09-10 16:39:30 -0700 |
---|---|---|
committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2014-09-27 21:56:05 +0000 |
commit | 5780a5d77e70443dcf785c03814cf902c6d073cd (patch) | |
tree | 60a3708d201b5eb11be92f355538d70e47f8c960 | |
parent | ce620c399a9e5cf2fd438d0193ba0d83da667a88 (diff) | |
download | taskflow-5780a5d77e70443dcf785c03814cf902c6d073cd.tar.gz |
Use the features that the oslotest mock base class provides
Instead of having our own mock subclass that has similar functions
as the oslotest base mocking class just use the base class functions
where we can and add on our own customizations as we choose to.
This change moves to using the base classes fixtures and also adjusts
the customized subclass method names to match closer to the rest of
the unittest classes method name style (camel-case not underscores).
Change-Id: If24530c0381d7fb99797acaa582d3be1d7054185
-rw-r--r-- | taskflow/test.py | 24 | ||||
-rw-r--r-- | taskflow/tests/unit/worker_based/test_dispatcher.py | 2 | ||||
-rw-r--r-- | taskflow/tests/unit/worker_based/test_engine.py | 2 | ||||
-rw-r--r-- | taskflow/tests/unit/worker_based/test_executor.py | 8 | ||||
-rw-r--r-- | taskflow/tests/unit/worker_based/test_message_pump.py | 2 | ||||
-rw-r--r-- | taskflow/tests/unit/worker_based/test_pipeline.py | 2 | ||||
-rw-r--r-- | taskflow/tests/unit/worker_based/test_proxy.py | 16 | ||||
-rw-r--r-- | taskflow/tests/unit/worker_based/test_server.py | 6 | ||||
-rw-r--r-- | taskflow/tests/unit/worker_based/test_worker.py | 8 |
9 files changed, 34 insertions, 36 deletions
diff --git a/taskflow/test.py b/taskflow/test.py index 3d94df5..f894b3c 100644 --- a/taskflow/test.py +++ b/taskflow/test.py @@ -16,6 +16,7 @@ import fixtures from oslotest import base +from oslotest import mockpatch import six try: from six.moves import mock @@ -189,25 +190,23 @@ class TestCase(base.BaseTestCase): self.assertThat(seq2, matcher) -class MockTestCase(base.BaseTestCase): +class MockTestCase(TestCase): def setUp(self): super(MockTestCase, self).setUp() self.master_mock = mock.Mock(name='master_mock') - def _patch(self, target, autospec=True, **kwargs): + def patch(self, target, autospec=True, **kwargs): """Patch target and attach it to the master mock.""" - patcher = mock.patch(target, autospec=autospec, **kwargs) - mocked = patcher.start() - self.addCleanup(patcher.stop) - + f = self.useFixture(mockpatch.Patch(target, + autospec=autospec, **kwargs)) + mocked = f.mock attach_as = kwargs.pop('attach_as', None) if attach_as is not None: self.master_mock.attach_mock(mocked, attach_as) - return mocked - def _patch_class(self, module, name, autospec=True, attach_as=None): + def patchClass(self, module, name, autospec=True, attach_as=None): """Patches a modules class. This will create a class instance mock (using the provided name to @@ -219,9 +218,9 @@ class MockTestCase(base.BaseTestCase): else: instance_mock = mock.Mock() - patcher = mock.patch.object(module, name, autospec=autospec) - class_mock = patcher.start() - self.addCleanup(patcher.stop) + f = self.useFixture(mockpatch.PatchObject(module, name, + autospec=autospec)) + class_mock = f.mock class_mock.return_value = instance_mock if attach_as is None: @@ -233,8 +232,7 @@ class MockTestCase(base.BaseTestCase): self.master_mock.attach_mock(class_mock, attach_class_as) self.master_mock.attach_mock(instance_mock, attach_instance_as) - return class_mock, instance_mock - def _reset_master_mock(self): + def resetMasterMock(self): self.master_mock.reset_mock() diff --git a/taskflow/tests/unit/worker_based/test_dispatcher.py b/taskflow/tests/unit/worker_based/test_dispatcher.py index 9b121d5..a7bf2d5 100644 --- a/taskflow/tests/unit/worker_based/test_dispatcher.py +++ b/taskflow/tests/unit/worker_based/test_dispatcher.py @@ -34,7 +34,7 @@ def mock_acked_message(ack_ok=True, **kwargs): return msg -class TestDispatcher(test.MockTestCase): +class TestDispatcher(test.TestCase): def test_creation(self): on_hello = mock.MagicMock() handlers = {'hello': on_hello} diff --git a/taskflow/tests/unit/worker_based/test_engine.py b/taskflow/tests/unit/worker_based/test_engine.py index ea322de..f274a82 100644 --- a/taskflow/tests/unit/worker_based/test_engine.py +++ b/taskflow/tests/unit/worker_based/test_engine.py @@ -31,7 +31,7 @@ class TestWorkerBasedActionEngine(test.MockTestCase): self.topics = ['test-topic1', 'test-topic2'] # patch classes - self.executor_mock, self.executor_inst_mock = self._patch_class( + self.executor_mock, self.executor_inst_mock = self.patchClass( engine.executor, 'WorkerTaskExecutor', attach_as='executor') def test_creation_default(self): diff --git a/taskflow/tests/unit/worker_based/test_executor.py b/taskflow/tests/unit/worker_based/test_executor.py index c55c1c6..59681bb 100644 --- a/taskflow/tests/unit/worker_based/test_executor.py +++ b/taskflow/tests/unit/worker_based/test_executor.py @@ -45,9 +45,9 @@ class TestWorkerTaskExecutor(test.MockTestCase): self.proxy_started_event = threading.Event() # patch classes - self.proxy_mock, self.proxy_inst_mock = self._patch_class( + self.proxy_mock, self.proxy_inst_mock = self.patchClass( executor.proxy, 'Proxy') - self.request_mock, self.request_inst_mock = self._patch_class( + self.request_mock, self.request_inst_mock = self.patchClass( executor.pr, 'Request', autospec=False) # other mocking @@ -56,7 +56,7 @@ class TestWorkerTaskExecutor(test.MockTestCase): self.request_inst_mock.uuid = self.task_uuid self.request_inst_mock.expired = False self.request_inst_mock.task_cls = self.task.name - self.wait_for_any_mock = self._patch( + self.wait_for_any_mock = self.patch( 'taskflow.engines.worker_based.executor.async_utils.wait_for_any') self.message_mock = mock.MagicMock(name='message') self.message_mock.properties = {'correlation_id': self.task_uuid, @@ -78,7 +78,7 @@ class TestWorkerTaskExecutor(test.MockTestCase): executor_kwargs.update(kwargs) ex = executor.WorkerTaskExecutor(**executor_kwargs) if reset_master_mock: - self._reset_master_mock() + self.resetMasterMock() return ex def test_creation(self): diff --git a/taskflow/tests/unit/worker_based/test_message_pump.py b/taskflow/tests/unit/worker_based/test_message_pump.py index e2102b7..008ad72 100644 --- a/taskflow/tests/unit/worker_based/test_message_pump.py +++ b/taskflow/tests/unit/worker_based/test_message_pump.py @@ -29,7 +29,7 @@ BARRIER_WAIT_TIMEOUT = 1.0 POLLING_INTERVAL = 0.01 -class TestMessagePump(test.MockTestCase): +class TestMessagePump(test.TestCase): def test_notify(self): barrier = threading.Event() diff --git a/taskflow/tests/unit/worker_based/test_pipeline.py b/taskflow/tests/unit/worker_based/test_pipeline.py index 8809785..ae11efd 100644 --- a/taskflow/tests/unit/worker_based/test_pipeline.py +++ b/taskflow/tests/unit/worker_based/test_pipeline.py @@ -32,7 +32,7 @@ WAIT_TIMEOUT = 1.0 POLLING_INTERVAL = 0.01 -class TestPipeline(test.MockTestCase): +class TestPipeline(test.TestCase): def _fetch_server(self, task_classes): endpoints = [] for cls in task_classes: diff --git a/taskflow/tests/unit/worker_based/test_proxy.py b/taskflow/tests/unit/worker_based/test_proxy.py index 3c07596..de5f3ab 100644 --- a/taskflow/tests/unit/worker_based/test_proxy.py +++ b/taskflow/tests/unit/worker_based/test_proxy.py @@ -34,13 +34,13 @@ class TestProxy(test.MockTestCase): self.de_period = proxy.DRAIN_EVENTS_PERIOD # patch classes - self.conn_mock, self.conn_inst_mock = self._patch_class( + self.conn_mock, self.conn_inst_mock = self.patchClass( proxy.kombu, 'Connection') - self.exchange_mock, self.exchange_inst_mock = self._patch_class( + self.exchange_mock, self.exchange_inst_mock = self.patchClass( proxy.kombu, 'Exchange') - self.queue_mock, self.queue_inst_mock = self._patch_class( + self.queue_mock, self.queue_inst_mock = self.patchClass( proxy.kombu, 'Queue') - self.producer_mock, self.producer_inst_mock = self._patch_class( + self.producer_mock, self.producer_inst_mock = self.patchClass( proxy.kombu, 'Producer') # connection mocking @@ -48,14 +48,14 @@ class TestProxy(test.MockTestCase): socket.timeout, socket.timeout, KeyboardInterrupt] # connections mocking - self.connections_mock = self._patch( + self.connections_mock = self.patch( "taskflow.engines.worker_based.proxy.kombu.connections", attach_as='connections') self.connections_mock.__getitem__().acquire().__enter__.return_value =\ self.conn_inst_mock # producers mocking - self.producers_mock = self._patch( + self.producers_mock = self.patch( "taskflow.engines.worker_based.proxy.kombu.producers", attach_as='producers') self.producers_mock.__getitem__().acquire().__enter__.return_value =\ @@ -70,7 +70,7 @@ class TestProxy(test.MockTestCase): self.master_mock.attach_mock(self.on_wait_mock, 'on_wait') # reset master mock - self._reset_master_mock() + self.resetMasterMock() def _queue_name(self, topic): return "%s_%s" % (self.exchange_name, topic) @@ -99,7 +99,7 @@ class TestProxy(test.MockTestCase): proxy_kwargs.update(kwargs) p = proxy.Proxy(**proxy_kwargs) if reset_master_mock: - self._reset_master_mock() + self.resetMasterMock() return p def test_creation(self): diff --git a/taskflow/tests/unit/worker_based/test_server.py b/taskflow/tests/unit/worker_based/test_server.py index 2759c49..40fc29b 100644 --- a/taskflow/tests/unit/worker_based/test_server.py +++ b/taskflow/tests/unit/worker_based/test_server.py @@ -42,9 +42,9 @@ class TestServer(test.MockTestCase): ep.Endpoint(task_cls=utils.ProgressingTask)] # patch classes - self.proxy_mock, self.proxy_inst_mock = self._patch_class( + self.proxy_mock, self.proxy_inst_mock = self.patchClass( server.proxy, 'Proxy') - self.response_mock, self.response_inst_mock = self._patch_class( + self.response_mock, self.response_inst_mock = self.patchClass( server.pr, 'Response') # other mocking @@ -66,7 +66,7 @@ class TestServer(test.MockTestCase): server_kwargs.update(kwargs) s = server.Server(**server_kwargs) if reset_master_mock: - self._reset_master_mock() + self.resetMasterMock() return s def make_request(self, **kwargs): diff --git a/taskflow/tests/unit/worker_based/test_worker.py b/taskflow/tests/unit/worker_based/test_worker.py index 1777098..9d08db2 100644 --- a/taskflow/tests/unit/worker_based/test_worker.py +++ b/taskflow/tests/unit/worker_based/test_worker.py @@ -35,13 +35,13 @@ class TestWorker(test.MockTestCase): self.endpoint_count = 21 # patch classes - self.executor_mock, self.executor_inst_mock = self._patch_class( + self.executor_mock, self.executor_inst_mock = self.patchClass( worker.futures, 'ThreadPoolExecutor', attach_as='executor') - self.server_mock, self.server_inst_mock = self._patch_class( + self.server_mock, self.server_inst_mock = self.patchClass( worker.server, 'Server') # other mocking - self.threads_count_mock = self._patch( + self.threads_count_mock = self.patch( 'taskflow.engines.worker_based.worker.tu.get_optimal_thread_count') self.threads_count_mock.return_value = self.threads_count @@ -53,7 +53,7 @@ class TestWorker(test.MockTestCase): worker_kwargs.update(kwargs) w = worker.Worker(**worker_kwargs) if reset_master_mock: - self._reset_master_mock() + self.resetMasterMock() return w def test_creation(self): |