Exemplo n.º 1
0
class CacheInterfacesTests(TestCase):
    def setUp(self):
        self._segment_changes_file_name = join(dirname(__file__),
                                               'segmentChanges.json')
        self._split_changes_file_name = join(dirname(__file__),
                                             'splitChanges.json')

        self._redis = get_redis({'redisPrefix': 'test'})
        self._redis_split_cache = RedisSplitCache(self._redis)
        self._redis_segment_cache = RedisSegmentCache(self._redis)

    def test_split_cache_interface(self):

        with open(self._split_changes_file_name) as f:
            self._json = load(f)
            split_definition = self._json['splits'][0]
            split_name = split_definition['name']

        #Add and get Split
        self._redis_split_cache.add_split(split_name, split_definition)
        self.assertEqual(split_definition['name'],
                         self._redis_split_cache.get_split(split_name).name)
        self.assertEqual(split_definition['killed'],
                         self._redis_split_cache.get_split(split_name).killed)
        self.assertEqual(split_definition['seed'],
                         self._redis_split_cache.get_split(split_name).seed)

        #Remove Split
        self._redis_split_cache.remove_split(split_name)
        self.assertIsNone(self._redis_split_cache.get_split(split_name))

        #Change Number
        self._redis_split_cache.set_change_number(1212)
        self.assertEqual(1212, self._redis_split_cache.get_change_number())

    def testSegmentCacheInterface(self):
        with open(self._segment_changes_file_name) as f:
            self._json = load(f)
            segment_name = self._json['name']
            segment_change_number = self._json['till']
            segment_keys = self._json['added']

        self._redis_segment_cache.set_change_number(segment_name,
                                                    segment_change_number)
        self.assertEqual(
            segment_change_number,
            self._redis_segment_cache.get_change_number(segment_name))

        self._redis_segment_cache.add_keys_to_segment(segment_name,
                                                      segment_keys)
        self.assertTrue(
            self._redis_segment_cache.is_in_segment(segment_name,
                                                    segment_keys[0]))

        self._redis_segment_cache.remove_keys_from_segment(
            segment_name, [segment_keys[0]])
        self.assertFalse(
            self._redis_segment_cache.is_in_segment(segment_name,
                                                    segment_keys[0]))
Exemplo n.º 2
0
class RedisSegmentCacheTests(TestCase):
    def setUp(self):
        self.some_segment_name = mock.MagicMock()
        self.some_segment_name_str = 'some_segment_name'
        self.some_segment_keys = [mock.MagicMock(), mock.MagicMock()]
        self.some_key = mock.MagicMock()
        self.some_change_number = mock.MagicMock()
        self.some_redis = mock.MagicMock()
        self.a_segment_cache = RedisSegmentCache(self.some_redis)

    def test_add_keys_to_segment_adds_keys_to_segment_set(self):
        """Test that add_keys_to_segment adds the keys to the segment key set"""
        self.a_segment_cache.add_keys_to_segment(self.some_segment_name_str,
                                                 self.some_segment_keys)
        self.some_redis.sadd.assert_called_once_with(
            'SPLITIO.segment.some_segment_name', self.some_segment_keys[0],
            self.some_segment_keys[1])

    def test_remove_keys_from_segment_remove_keys_from_segment_set(self):
        """Test that remove_keys_from_segment removes the keys to the segment key set"""
        self.a_segment_cache.remove_keys_from_segment(
            self.some_segment_name_str, self.some_segment_keys)
        self.some_redis.srem.assert_called_once_with(
            'SPLITIO.segment.some_segment_name', self.some_segment_keys[0],
            self.some_segment_keys[1])

    def test_is_in_segment_tests_whether_a_key_is_in_a_segments_key_set(self):
        """Test that is_in_segment checks if a key is in a segment's key set"""
        self.assertEqual(
            self.some_redis.sismember.return_value,
            self.a_segment_cache.is_in_segment(self.some_segment_name_str,
                                               self.some_key))
        self.some_redis.sismember.assert_called_once_with(
            'SPLITIO.segment.some_segment_name', self.some_key)

    def test_set_change_number_sets_segment_change_number_key(self):
        """Test that set_change_number sets the segment's change number key"""
        self.a_segment_cache.set_change_number(self.some_segment_name_str,
                                               self.some_change_number)
        self.some_redis.set.assert_called_once_with(
            'SPLITIO.segment.some_segment_name.till', self.some_change_number)

    def test_get_change_number_gets_segment_change_number_key(self):
        """Test that get_change_number gets the segment's change number key"""
        self.some_redis.get.return_value = '1234'
        result = self.a_segment_cache.get_change_number(
            self.some_segment_name_str)
        self.assertEqual(int(self.some_redis.get.return_value), result)
        self.assertIsInstance(result, int)
        self.some_redis.get.assert_called_once_with(
            'SPLITIO.segment.some_segment_name.till')

    def test_get_change_number_returns_default_value_if_not_set(self):
        """Test that get_change_number returns -1 if the value is not set"""
        self.some_redis.get.return_value = None
        self.assertEqual(
            -1,
            self.a_segment_cache.get_change_number(self.some_segment_name_str))