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)
Пример #2
0
    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)
Пример #3
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]))
Пример #4
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))
Пример #5
0
def run(arguments):
    try:
        config = parse_config_file(arguments['<config_file>'])
        redis = get_redis(config)
        segment_cache = RedisSegmentCache(redis)
        sdk_api = api_factory(config)
        segment_change_fetcher = ApiSegmentChangeFetcher(sdk_api)
        update_segments(segment_cache, segment_change_fetcher)
    except:
        logger.exception('Exception caught updating segments')
Пример #6
0
 def testAlgoHandlers(self):
     '''
     '''
     redis = get_redis({})
     segment_cache = RedisSegmentCache(redis)
     split_parser = RedisSplitParser(segment_cache)
     for sp in self._testData:
         split = split_parser.parse(sp['body'], True)
         self.assertEqual(split.algo, sp['algo'])
         self.assertEqual(get_hash_fn(split.algo), sp['hashfn'])
Пример #7
0
def _update_segments(seconds, config):
    try:
        while True:
            redis = get_redis(config)
            segment_cache = RedisSegmentCache(redis)
            sdk_api = api_factory(config)
            segment_change_fetcher = ApiSegmentChangeFetcher(sdk_api)
            update_segments(segment_cache, segment_change_fetcher)

            time.sleep(seconds)
    except:
        logger.exception('Exception caught updating segments')
Пример #8
0
    def setUp(self):
        '''
        '''
        redis = get_redis({})
        segment_cache = RedisSegmentCache(redis)
        split_parser = RedisSplitParser(segment_cache)
        self._client = Client(RedisBroker(redis))

        self._splitObjects = {}

        raw_split = {
            'name':
            'test1',
            'algo':
            1,
            'killed':
            False,
            'status':
            'ACTIVE',
            'defaultTreatment':
            'default',
            'seed':
            -1222652054,
            'orgId':
            None,
            'environment':
            None,
            'trafficTypeId':
            None,
            'trafficTypeName':
            None,
            'changeNumber':
            1,
            'conditions': [{
                'conditionType': 'WHITELIST',
                'matcherGroup': {
                    'combiner':
                    'AND',
                    'matchers': [{
                        'matcherType': 'ALL_KEYS',
                        'negate': False,
                        'userDefinedSegmentMatcherData': None,
                        'whitelistMatcherData': None
                    }]
                },
                'partitions': [{
                    'treatment': 'on',
                    'size': 100
                }],
                'label': 'in segment all'
            }]
        }
        self._splitObjects['whitelist'] = split_parser.parse(raw_split, True)

        raw_split['name'] = 'test2'
        raw_split['conditions'][0]['conditionType'] = 'ROLLOUT'
        self._splitObjects['rollout1'] = split_parser.parse(raw_split, True)

        raw_split['name'] = 'test3'
        raw_split['trafficAllocation'] = 1
        raw_split['trafficAllocationSeed'] = -1
        self._splitObjects['rollout2'] = split_parser.parse(raw_split, True)

        raw_split['name'] = 'test4'
        raw_split['trafficAllocation'] = None  #must be mapped as 100
        raw_split['trafficAllocationSeed'] = -1
        self._splitObjects['rollout3'] = split_parser.parse(raw_split, True)