def setUp(self):
     self.encode_mock = self.patch('splitio.redis_support.encode')
     self.decode_mock = self.patch('splitio.redis_support.decode')
     self.some_impression = mock.MagicMock()
     self.some_redis = mock.MagicMock()
     self.an_impressions_cache = RedisImpressionsCache(self.some_redis)
     self.build_impressions_dict_mock = self.patch_object(self.an_impressions_cache,
                                                          '_build_impressions_dict')
Ejemplo n.º 2
0
class RedisImpressionsCacheBuildImpressionsDictTests(TestCase):
    def setUp(self):
        self.some_redis = mock.MagicMock()
        self.an_impressions_cache = RedisImpressionsCache(self.some_redis)

    def _build_impression(self, feature_name):
        return Impression(matching_key=mock.MagicMock(),
                          feature_name=feature_name,
                          treatment=mock.MagicMock(),
                          label=mock.MagicMock(),
                          time=mock.MagicMock(),
                          change_number=mock.MagicMock(),
                          bucketing_key=mock.MagicMock())

    def test_build_impressions_dict(self):
        """Test that _build_impressions_dict builds the dictionary properly"""
        some_feature_name = mock.MagicMock()
        some_other_feature_name = mock.MagicMock()
        some_feature_impressions = [self._build_impression(some_feature_name)]
        some_other_feature_impressions = [
            self._build_impression(some_other_feature_name),
            self._build_impression(some_other_feature_name)
        ]

        self.assertDictEqual(
            {
                some_feature_name: some_feature_impressions,
                some_other_feature_name: some_other_feature_impressions
            },
            self.an_impressions_cache._build_impressions_dict(
                some_feature_impressions + some_other_feature_impressions))
 def test_clear_deletes_impressions_key(self):
     """Test that clear deletes impressions key"""
     self.an_impressions_cache.clear()
     self.some_redis.eval.assert_called_once_with(
         "return redis.call('del', unpack(redis.call('keys', ARGV[1])))",
         0,
         RedisImpressionsCache._get_impressions_key('*')
     )
Ejemplo n.º 4
0
def run(arguments):
    try:
        config = parse_config_file(arguments['<config_file>'])
        redis = get_redis(config)
        impressions_cache = RedisImpressionsCache(redis)
        sdk_api = api_factory(config)
        report_impressions(impressions_cache, sdk_api)
    except:
        logger.exception('Exception caught posting impressions')
Ejemplo n.º 5
0
def _report_impressions(seconds, config):
    try:
        while True:
            redis = get_redis(config)
            impressions_cache = RedisImpressionsCache(redis)
            sdk_api = api_factory(config)
            report_impressions(impressions_cache, sdk_api)

            time.sleep(seconds)
    except:
        logger.exception('Exception caught posting impressions')
Ejemplo n.º 6
0
    def __init__(self, redis, config):
        """A Broker implementation that uses Redis as its backend.
        :param redis: A redis broker
        :type redis: StrctRedis"""
        super(RedisBroker, self).__init__(config)

        split_cache = RedisSplitCache(redis)
        split_fetcher = CacheBasedSplitFetcher(split_cache)

        impressions_cache = RedisImpressionsCache(redis)
        treatment_log = CacheBasedTreatmentLog(impressions_cache)

        metrics_cache = RedisMetricsCache(redis)
        delegate_metrics = CacheBasedMetrics(metrics_cache)
        metrics = AsyncMetrics(delegate_metrics)

        self._split_fetcher = split_fetcher
        self._treatment_log = treatment_log
        self._metrics = metrics

        self._event_storage = RedisEventsCache(redis)
class RedisImpressionsCacheTests(TestCase, MockUtilsMixin):
    def setUp(self):
        self.encode_mock = self.patch('splitio.redis_support.encode')
        self.decode_mock = self.patch('splitio.redis_support.decode')
        self.some_impression = mock.MagicMock()
        self.some_redis = mock.MagicMock()
        self.an_impressions_cache = RedisImpressionsCache(self.some_redis)
        self.build_impressions_dict_mock = self.patch_object(self.an_impressions_cache,
                                                             '_build_impressions_dict')

    def test_fetch_all_doesnt_call_build_impressions_dict_if_no_impressions_cached(self):
        """Test that fetch_all doesn't call _build_impressions_dict if no impressions are cached"""
        self.some_redis.lrange.return_value = None
        self.assertDictEqual(dict(), self.an_impressions_cache.fetch_all())
        self.build_impressions_dict_mock.assert_not_called()

    def test_clear_deletes_impressions_key(self):
        """Test that clear deletes impressions key"""
        self.an_impressions_cache.clear()
        self.some_redis.eval.assert_called_once_with(
            "return redis.call('del', unpack(redis.call('keys', ARGV[1])))",
            0,
            RedisImpressionsCache._get_impressions_key('*')
        )

    def test_fetch_all_and_clear_calls_eval_with_fetch_and_clear_script(self):
        """Test that fetch_and_clear calls eval with the fetch and clear script"""
        self.an_impressions_cache.fetch_all_and_clear()
        self.some_redis.keys.assert_called_once_with(
            RedisImpressionsCache._get_impressions_key('*')
        )

    def test_fetch_all_and_clear_doesnt_call_build_impressions_dict_if_no_impressions_cached(self):
        """Test that fetch_all_and_clear doesn't call _build_impressions_dict if no impressions are
        cached"""
        self.some_redis.eval.return_value = None
        self.assertDictEqual(dict(), self.an_impressions_cache.fetch_all_and_clear())
        self.build_impressions_dict_mock.assert_not_called()
 def setUp(self):
     self.some_redis = mock.MagicMock()
     self.an_impressions_cache = RedisImpressionsCache(self.some_redis)
 def test_fetch_all_and_clear_calls_eval_with_fetch_and_clear_script(self):
     """Test that fetch_and_clear calls eval with the fetch and clear script"""
     self.an_impressions_cache.fetch_all_and_clear()
     self.some_redis.keys.assert_called_once_with(
         RedisImpressionsCache._get_impressions_key('*')
     )