Ejemplo n.º 1
0
    def test_should_repeat_query_when_value_changed_in_meantime(
            self, mock_time, mock_uuid, mock_redis):
        # given
        mock_uuid.return_value = 'QWERTY'
        pipeline = MagicMock()
        pipeline.return_value.get.return_value = None
        mock_redis.return_value.pipeline.return_value.__enter__ = pipeline
        lock = RedisReadLock('TestLock', prefix='RedisLockUnitTest')
        mock_time.return_value = 123456
        pipeline.return_value.set.side_effect = (WatchError(), WatchError(),
                                                 'OK')

        # when
        result = lock.acquire()

        # then
        self.assertTrue(result)
        self.assertEqual(pipeline.return_value.watch.call_count, 3)
        self.assertEqual(pipeline.return_value.set.call_count, 3)
        value = pipeline.return_value.set.mock_calls[0][2].get('value')
        self.assertDictEqual(loads(value), {
            'timestamp': 123456,
            'secret': ['QWERTY'],
            'exclusive': False
        })
        pipeline.return_value.execute.assert_called_once_with()
Ejemplo n.º 2
0
    def test_should_acquire_read_lock_when_other_non_exclusive_exists(
            self, mock_time, mock_uuid, mock_redis):
        # given
        mock_uuid.return_value = 'QWERTY'
        pipeline = MagicMock()
        pipeline.return_value.get.return_value =\
            b'{"timestamp": 123456, "secret": ["secret", "other"], "exclusive": false}'
        mock_redis.return_value.pipeline.return_value.__enter__ = pipeline
        lock = RedisReadLock('TestLock', prefix='RedisLockUnitTest')
        mock_time.return_value = 123456

        # when
        result = lock.acquire()

        # then
        self.assertTrue(result)
        pipeline.return_value.watch.assert_called_once_with(
            'RedisLockUnitTest:lock:TestLock')
        pipeline.return_value.set.assert_called_once_with(
            'RedisLockUnitTest:lock:TestLock', ex=None, value=ANY)
        value = pipeline.return_value.set.mock_calls[0][2].get('value')
        self.assertDictEqual(loads(value), {
            'timestamp': 123456,
            'secret': ANY,
            'exclusive': False
        })
        secrets = loads(value).get('secret')
        self.assertSetEqual(set(secrets), {'QWERTY', 'secret', 'other'})
        pipeline.return_value.execute.assert_called_once_with()
Ejemplo n.º 3
0
    def test_should_not_acquire_read_lock_when_non_exclusive_lock_exists_with_non_list_secret(
            self, mock_time, mock_uuid, mock_redis):
        # given
        mock_uuid.return_value = 'QWERTY'
        pipeline = MagicMock()
        pipeline.return_value.get.return_value = \
            b'{"timestamp": 123456, "secret": "secretData", "exclusive": false}'
        mock_redis.return_value.pipeline.return_value.__enter__ = pipeline
        lock = RedisReadLock('TestLock', prefix='RedisLockUnitTest')
        mock_time.return_value = 123456

        # when
        result = lock.acquire(blocking=False)

        # then
        self.assertFalse(result)
        pipeline.return_value.watch.assert_called_once_with(
            'RedisLockUnitTest:lock:TestLock')
        pipeline.return_value.set.assert_not_called()