Example #1
0
 def test_with_context(self):
     with ConsulServiceController().start_service() as service:
         consul_client = service.create_consul_client()
         lock_manager = ConsulLockManager(consul_client=consul_client)
         with lock_manager.acquire(KEY_1) as lock_information:
             self.assertEqual(lock_information, lock_manager.find(KEY_1))
         self.assertIsNone(lock_manager.find(KEY_1))
Example #2
0
def double_action(
    first_action: LockActionCallable,
    second_action: LockActionCallable,
    second_action_timeout: float = DEFAULT_LOCK_ACQUIRE_TIMEOUT,
    first_action_callback: Callable[[CaptureResult], Any] = None
) -> Tuple[CaptureResult, CaptureResult]:
    """
    Do two consecutive actions.
    :param first_action: first action to execute
    :param second_action: second action to execute
    :param second_action_timeout: timeout in seconds of the second action
    :param first_action_callback: optional callback after first action that is given the result of the first action
    :return: tuple where the first value is the captured result of the first action and the second is the captured
    result of the second action
    :raises TestActionTimeoutError: raised if the second action times out
    """
    with ConsulServiceController().start_service() as service:
        first_action_result = first_action(KEY_1, service)
        if first_action_callback:
            first_action_callback(first_action_result)

        @timeout_decorator.timeout(second_action_timeout,
                                   timeout_exception=TestActionTimeoutError)
        def second_action_with_timeout() -> CaptureResult:
            return second_action(KEY_1, service)

        try:
            return first_action_result, second_action_with_timeout()
        except TestActionTimeoutError as e:
            return first_action_result, CaptureResult(exception=e)
Example #3
0
 def test_find_regex_when_no_locks(self):
     with ConsulServiceController().start_service() as service:
         lock_manager = ConsulLockManager(
             consul_client=service.create_consul_client())
         found_locks = lock_manager.find_regex(
             f"{KEY_1}{KEY_DIRECTORY_SEPARATOR}[0-9]+")
         self.assertEqual(0, len(found_locks))
Example #4
0
 def test_unlock_all(self):
     test_keys = [f"{KEY_1}_{i}" for i in range(5)]
     with ConsulServiceController().start_service() as service:
         lock_manager = ConsulLockManager(
             consul_client=service.create_consul_client())
         for key in test_keys:
             lock = lock_manager.acquire(key)
             assert isinstance(lock, ConsulLockInformation)
         unlock_results = lock_manager.release_all(test_keys)
     for unlock_result in unlock_results:
         self.assertTrue(unlock_result)
Example #5
0
def single_action(
        action: LockActionCallable,
        action_timeout: float = DEFAULT_LOCK_ACQUIRE_TIMEOUT) -> CaptureResult:
    """
    TODO
    :param action:
    :param action_timeout:
    :return:
    """
    with ConsulServiceController().start_service() as service:

        @timeout_decorator.timeout(action_timeout,
                                   timeout_exception=TestActionTimeoutError)
        def wrapped_action() -> CaptureResult:
            return action(KEY_1, service)

        return wrapped_action()
Example #6
0
def acquire_locks(
    locker: LockerCallable,
    keys: List[str] = None,
    on_complete: Callable[[ConsulDockerisedService], None] = None
) -> List[CaptureResult]:
    """
    Test getting locks.
    :param locker: method that locks Consul
    :param keys: optional keys of the locks to acquire (in order). Will use single test key if not defined
    :param on_complete: optional method to call before the service is taken down
    :return: the result of the lock acquisitions
    """
    if keys is None:
        keys = [KEY_1]
    lock_results: List[CaptureResult] = []
    with ConsulServiceController().start_service() as service:
        for key in keys:
            lock_results.append(locker(key, service))
        if on_complete is not None:
            on_complete(service)
        return lock_results
Example #7
0
 def setUp(self):
     self._consul_service_controller = ConsulServiceController()
     self._env_cache = deepcopy(os.environ)
Example #8
0
 def test_unlock_when_unlocked(self):
     with ConsulServiceController().start_service() as service:
         unlock_result = TestCli._build_executor(Action.UNLOCK)(KEY_1,
                                                                service)
     self.assertEqual(SUCCESS_EXIT_CODE, unlock_result.exception.code)
     self.assertIsNone(json.loads(unlock_result.stdout))
Example #9
0
 def test_can_get_configuration_from_environment(self):
     with ConsulServiceController().start_service() as service:
         set_consul_env(service)
         lock_manager = ConsulLockManager()
         self.assertIsNone(lock_manager.release(KEY_1))
Example #10
0
 def test_find_when_no_locks(self):
     with ConsulServiceController().start_service() as service:
         lock_manager = ConsulLockManager(
             consul_client=service.create_consul_client())
         self.assertIsNone(lock_manager.find(KEY_1))
Example #11
0
 def test_unlock_when_unlocked(self):
     with ConsulServiceController().start_service() as service:
         unlock_result = TestConsulLockManager._build_executor(
             Action.UNLOCK)(KEY_1, service)
     self.assertIsNone(unlock_result.return_value)