Esempio n. 1
0
    def test_find_when_locks(self):
        def find(service: ConsulDockerisedService):
            lock_manager = ConsulLockManager(consul_client=service.create_consul_client())
            found_lock = lock_manager.find(KEYS_1[0])
            self.assertEqual(KEYS_1[0], found_lock.key)

        acquire_locks(TestConsulLockManager._build_executor(Action.LOCK), KEYS_1 + KEYS_2, find)
Esempio n. 2
0
    def test_find_regex_when_locks(self):
        def find_regex(service: ConsulDockerisedService):
            lock_manager = ConsulLockManager(consul_client=service.create_consul_client())
            found_locks = lock_manager.find_regex(KEYS_1_REGEX)
            self.assertCountEqual(KEYS_1, [lock.key for lock in found_locks.values()])

        acquire_locks(TestConsulLockManager._build_executor(Action.LOCK), KEYS_1 + KEYS_2, find_regex)
Esempio n. 3
0
    def test_unlock_with_regex(self):
        def release(service: ConsulDockerisedService):
            lock_manager = ConsulLockManager(consul_client=service.create_consul_client())
            released_locks = lock_manager.release_regex(KEYS_1_REGEX)
            self.assertCountEqual(KEYS_1, released_locks)

        acquire_locks(TestConsulLockManager._build_executor(Action.LOCK), KEYS_1 + KEYS_2, release)
Esempio n. 4
0
    def test_find_when_locks(self):
        def find(service: ConsulDockerisedService):
            lock_manager = ConsulLockManager(
                consul_client=service.create_consul_client())
            found_lock = lock_manager.find(KEYS_1[0])
            self.assertEqual(KEYS_1[0], found_lock.key)

        acquire_locks(TestConsulLockManager._build_executor(Action.LOCK),
                      KEYS_1 + KEYS_2, find)
Esempio n. 5
0
    def test_unlock_with_regex(self):
        def release(service: ConsulDockerisedService):
            lock_manager = ConsulLockManager(
                consul_client=service.create_consul_client())
            released_locks = lock_manager.release_regex(KEYS_1_REGEX)
            self.assertCountEqual(KEYS_1, released_locks)

        acquire_locks(TestConsulLockManager._build_executor(Action.LOCK),
                      KEYS_1 + KEYS_2, release)
Esempio n. 6
0
    def test_find_regex_when_locks(self):
        def find_regex(service: ConsulDockerisedService):
            lock_manager = ConsulLockManager(
                consul_client=service.create_consul_client())
            found_locks = lock_manager.find_regex(KEYS_1_REGEX)
            self.assertCountEqual(KEYS_1,
                                  [lock.key for lock in found_locks.values()])

        acquire_locks(TestConsulLockManager._build_executor(Action.LOCK),
                      KEYS_1 + KEYS_2, find_regex)
Esempio n. 7
0
    def test_find_regex_when_locks_and_other_key(self):
        def find_regex(service: ConsulDockerisedService):
            consul_client = service.create_consul_client()
            lock_manager = ConsulLockManager(consul_client=consul_client)
            consul_client.kv.put(KEYS_1[1], "unrelated")
            found_locks = lock_manager.find_regex(KEYS_1_REGEX)
            self.assertEqual(2, len(found_locks))
            self.assertIsInstance(found_locks[KEYS_1[0]], ConsulLockInformation)
            self.assertIsNone(found_locks[KEYS_1[1]])

        acquire_locks(TestConsulLockManager._build_executor(Action.LOCK), [KEYS_1[0]], find_regex)
Esempio n. 8
0
    def test_find_regex_when_locks_and_other_key(self):
        def find_regex(service: ConsulDockerisedService):
            consul_client = service.create_consul_client()
            lock_manager = ConsulLockManager(consul_client=consul_client)
            consul_client.kv.put(KEYS_1[1], "unrelated")
            found_locks = lock_manager.find_regex(KEYS_1_REGEX)
            self.assertEqual(2, len(found_locks))
            self.assertIsInstance(found_locks[KEYS_1[0]],
                                  ConsulLockInformation)
            self.assertIsNone(found_locks[KEYS_1[1]])

        acquire_locks(TestConsulLockManager._build_executor(Action.LOCK),
                      [KEYS_1[0]], find_regex)
Esempio n. 9
0
 def test_lock_when_unlocked(self):
     locker = TestConsulLockManager._build_executor(Action.LOCK, action_kwargs=dict(metadata=METADATA))
     seconds_to_test = monotonic()
     lock_result = acquire_locks(locker)[0]
     self.assertIsInstance(lock_result.return_value, ConsulLockInformation)
     self.assertGreater(monotonic() - seconds_to_test, lock_result.return_value.seconds_to_lock)
     self.assertEqual(METADATA, lock_result.return_value.metadata)
Esempio n. 10
0
 def test_lock_when_unlocked(self):
     locker = TestConsulLockManager._build_executor(
         Action.LOCK, action_kwargs=dict(metadata=METADATA))
     seconds_to_test = monotonic()
     lock_result = acquire_locks(locker)[0]
     self.assertIsInstance(lock_result.return_value, ConsulLockInformation)
     self.assertGreater(monotonic() - seconds_to_test,
                        lock_result.return_value.seconds_to_lock)
     self.assertEqual(METADATA, lock_result.return_value.metadata)
Esempio n. 11
0
    def test_lock_callbacks_when_not_locked(self):
        on_before_lock_listener = MagicMock()
        on_lock_already_locked_listener = MagicMock()

        locker = TestConsulLockManager._build_executor(Action.LOCK, action_kwargs=dict(
            on_before_lock=on_before_lock_listener, on_lock_already_locked=on_lock_already_locked_listener))
        lock_result = acquire_locks(locker)[0]
        assert lock_result.return_value is not None

        on_before_lock_listener.assert_called_once_with(KEY_1)
        on_lock_already_locked_listener.assert_not_called()
Esempio n. 12
0
    def test_lock_callbacks_when_not_locked(self):
        on_before_lock_listener = MagicMock()
        on_lock_already_locked_listener = MagicMock()

        locker = TestConsulLockManager._build_executor(
            Action.LOCK,
            action_kwargs=dict(
                on_before_lock=on_before_lock_listener,
                on_lock_already_locked=on_lock_already_locked_listener))
        lock_result = acquire_locks(locker)[0]
        assert lock_result.return_value is not None

        on_before_lock_listener.assert_called_once_with(KEY_1)
        on_lock_already_locked_listener.assert_not_called()
Esempio n. 13
0
 def test_lock_with_no_metadata(self):
     locker = TestConsulLockManager._build_executor(Action.LOCK)
     lock_result = acquire_locks(locker)[0]
     self.assertIsNone(lock_result.return_value.metadata)
Esempio n. 14
0
 def test_lock_with_no_metadata(self):
     locker = TestConsulLockManager._build_executor(Action.LOCK)
     lock_result = acquire_locks(locker)[0]
     self.assertIsNone(lock_result.return_value.metadata)