def test_failed_acquire_alive_conductor_lock(self, mock_object_create): baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) with mock.patch.object(baylock, 'conductor_alive', return_value=True): self.assertRaises(exception.OperationInProgress, baylock.acquire) mock_object_create.assert_called_once_with(self.bay.uuid, self.conductor_id)
def test_failed_acquire_current_conductor_lock(self, mock_object_create): mock_object_create.return_value = self.conductor_id baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) self.assertRaises(exception.OperationInProgress, baylock.acquire) mock_object_create.assert_called_once_with(self.bay.uuid, self.conductor_id)
def test_conductor_alive_ok(self, mock_listener_api_new): mock_listener_api = mock.MagicMock() mock_listener_api.ping_conductor.return_value = True mock_listener_api_new.return_value = mock_listener_api baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) ret = baylock.conductor_alive(self.context, self.conductor_id) self.assertIs(True, ret) self.assertEqual(1, mock_listener_api_new.call_count)
def test_successful_acquire_dead_conductor_lock(self, mock_object_create, mock_object_steal): baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) with mock.patch.object(baylock, 'conductor_alive', return_value=False): baylock.acquire() mock_object_create.assert_called_once_with(self.bay.uuid, self.conductor_id) mock_object_steal.assert_called_once_with(self.bay.uuid, 'fake-conductor-id', self.conductor_id)
def test_conductor_alive_timeout(self, mock_listener_api_new): mock_listener_api = mock.MagicMock() mock_listener_api.ping_conductor.side_effect = ( messaging.MessagingTimeout('too slow')) mock_listener_api_new.return_value = mock_listener_api baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) ret = baylock.conductor_alive(self.context, self.conductor_id) self.assertIs(False, ret) self.assertEqual(1, mock_listener_api_new.call_count)
def test_thread_lock_acquire_success_with_exception( self, mock_object_create, mock_object_release): baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) def check_thread_lock(): with baylock.thread_lock(self.bay.uuid): self.assertEqual(1, mock_object_create.call_count) raise self.TestThreadLockException self.assertRaises(self.TestThreadLockException, check_thread_lock) self.assertEqual(1, mock_object_release.call_count)
def test_thread_lock_acquire_fail_with_exception(self, mock_object_create, mock_object_release): mock_object_create.return_value = self.conductor_id baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) def check_thread_lock(): with baylock.thread_lock(self.bay.uuid): self.assertEqual(1, mock_object_create.call_count) raise exception.OperationInProgress self.assertRaises(exception.OperationInProgress, check_thread_lock) assert not mock_object_release.called
def test_failed_acquire_one_retry_only(self, mock_object_create, mock_object_steal): baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) with mock.patch.object(baylock, 'conductor_alive', return_value=False): self.assertRaises(exception.OperationInProgress, baylock.acquire) mock_object_create.assert_has_calls( [mock.call(self.bay.uuid, self.conductor_id)] * 2) mock_object_steal.assert_has_calls([ mock.call(self.bay.uuid, 'fake-conductor-id', self.conductor_id) ] * 2)
def test_successful_acquire_new_lock(self, mock_object_create): baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) baylock.acquire() mock_object_create.assert_called_once_with(self.bay.uuid, self.conductor_id)
def test_thread_lock_acquire_success_no_exception(self, mock_object_create, mock_object_release): baylock = bay_lock.BayLock(self.context, self.bay, self.conductor_id) with baylock.thread_lock(self.bay.uuid): self.assertEqual(1, mock_object_create.call_count) assert not mock_object_release.called