Ejemplo n.º 1
0
def uwsgi_update_segments(user_config):
    """
    Update segments task.

    :param user_config: User-provided configuration.
    :type user_config: dict
    """
    config = _get_config(user_config)
    seconds = config['segmentsRefreshRate']
    metadata = get_metadata(config)
    segment_sync = SegmentSynchronizer(
        SegmentsAPI(
            HttpClient(1500, config.get('sdk_url'), config.get('events_url')),
            config['apikey'], metadata),
        UWSGISplitStorage(get_uwsgi()),
        UWSGISegmentStorage(get_uwsgi()),
    )

    pool = workerpool.WorkerPool(20, segment_sync.synchronize_segment)  # pylint: disable=protected-access
    pool.start()
    split_storage = UWSGISplitStorage(get_uwsgi())
    while True:
        try:
            for segment_name in split_storage.get_segment_names():
                pool.submit_work(segment_name)
            time.sleep(seconds)
        except Exception:  # pylint: disable=broad-except
            _LOGGER.error('Error updating segments')
            _LOGGER.debug('Error: ', exc_info=True)
Ejemplo n.º 2
0
    def test_get_all_splits(self, mocker):
        """Test fetching all splits."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)
        split_1 = mocker.Mock(spec=Split)
        split_1.to_json.return_value = '{"name": "some_split_1"}'
        split_name_1 = mocker.PropertyMock()
        split_name_1.return_value = 'some_split_1'
        type(split_1).name = split_name_1
        split_2 = mocker.Mock(spec=Split)
        split_2.to_json.return_value = '{"name": "some_split_2"}'
        split_name_2 = mocker.PropertyMock()
        split_name_2.return_value = 'some_split_2'
        type(split_2).name = split_name_2

        def _from_raw_mock(split_json):
            split_mock = mocker.Mock(spec=Split)
            name = mocker.PropertyMock()
            name.return_value = json.loads(split_json)['name']
            type(split_mock).name = name
            return split_mock
        mocker.patch('splitio.storage.uwsgi.splits.from_raw', new=_from_raw_mock)

        storage.put(split_1)
        storage.put(split_2)

        splits = storage.get_all_splits()
        s1 = next(split for split in splits if split.name == 'some_split_1')
        s2 = next(split for split in splits if split.name == 'some_split_2')
Ejemplo n.º 3
0
def uwsgi_update_segments(user_config):
    """
    Update segments task.

    :param user_config: User-provided configuration.
    :type user_config: dict
    """
    config = _get_config(user_config)
    seconds = config['segmentsRefreshRate']
    segment_sync_task = SegmentSynchronizationTask(
        SegmentsAPI(
            HttpClient(1500, config.get('sdk_url'), config.get('events_url')),
            config['apikey']),
        UWSGISegmentStorage(get_uwsgi()),
        None,  # Split sotrage not needed, segments provided manually,
        None,  # Period not needed, task executed manually
        None  # Flag not needed, never consumed or set.
    )

    pool = workerpool.WorkerPool(20, segment_sync_task._update_segment)  #pylint: disable=protected-access
    pool.start()
    split_storage = UWSGISplitStorage(get_uwsgi())
    while True:
        try:
            for split in split_storage.get_all_splits():
                for segment_name in split.get_segment_names():
                    pool.submit_work(segment_name)
            time.sleep(seconds)
        except Exception:  #pylint: disable=broad-except
            _LOGGER.error('Error updating segments')
            _LOGGER.debug('Error: ', exc_info=True)
Ejemplo n.º 4
0
def uwsgi_update_splits(user_config):
    """
    Update splits task.

    :param user_config: User-provided configuration.
    :type user_config: dict
    """
    config = _get_config(user_config)
    seconds = config['featuresRefreshRate']
    split_sync_task = SplitSynchronizationTask(
        SplitsAPI(
            HttpClient(1500, config.get('sdk_url'), config.get('events_url')),
            config['apikey']),
        UWSGISplitStorage(get_uwsgi()),
        None,  # Time not needed since the task will be triggered manually.
        None  # Ready flag not needed since it will never be set and consumed.
    )

    while True:
        try:
            split_sync_task._update_splits()  #pylint: disable=protected-access
            time.sleep(seconds)
        except Exception:  #pylint: disable=broad-except
            _LOGGER.error('Error updating splits')
            _LOGGER.debug('Error: ', exc_info=True)
Ejemplo n.º 5
0
    def test_update_segments(self, mocker):
        """Test split sync task wrapper."""
        data = {'executions': 0}

        def _submit_work(*_, **__):
            data['executions'] += 1
            # we mock 2 segments, so we expect this to be called at least twice before ending.
            if data['executions'] > 2:
                raise NonCatchableException('asd')

        wpmock = mocker.Mock(spec=WorkerPool)
        wpmock.submit_work.side_effect = _submit_work
        wpmock_class = mocker.Mock(spec=WorkerPool)
        wpmock_class.return_value = wpmock
        mocker.patch('splitio.tasks.uwsgi_wrappers.workerpool.WorkerPool',
                     new=wpmock_class)

        mocked_update_segment = mocker.patch.object(SplitStorage,
                                                    'get_segment_names')
        mocked_update_segment.return_value = ['segment1', 'segment2']
        mocked_split_storage_instance = UWSGISplitStorage(True)
        split_storage_mock = mocker.Mock(spec=UWSGISplitStorage)
        split_storage_mock.return_value = mocked_split_storage_instance

        mocker.patch('splitio.tasks.uwsgi_wrappers.UWSGISplitStorage',
                     new=split_storage_mock)

        try:
            uwsgi_update_segments({'apikey': 'asd', 'segmentsRefreshRate': 1})
        except NonCatchableException:
            # Make sure that the task was called before being forced to stop.
            assert data['executions'] > 2
            assert len(wpmock.submit_work.mock_calls) > 2
Ejemplo n.º 6
0
    def test_get_splits(self, mocker):
        """Test retrieving a list of passed splits."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)
        from_raw_mock = self._get_from_raw_mock(mocker)
        mocker.patch('splitio.models.splits.from_raw', new=from_raw_mock)

        split_1 = from_raw_mock({'name': 'some_split_1', 'trafficTypeName': 'user'})
        split_2 = from_raw_mock({'name': 'some_split_2', 'trafficTypeName': 'user'})
        storage.put(split_1)
        storage.put(split_2)

        splits = storage.fetch_many(['some_split_1', 'some_split_2', 'some_split_3'])
        assert len(splits) == 3
        assert splits['some_split_1'].name == 'some_split_1'
        assert splits['some_split_2'].name == 'some_split_2'
        assert 'some_split_3' in splits
Ejemplo n.º 7
0
    def test_get_all_splits(self, mocker):
        """Test fetching all splits."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)
        from_raw_mock = self._get_from_raw_mock(mocker)
        mocker.patch('splitio.models.splits.from_raw', new=from_raw_mock)

        split_1 = from_raw_mock({'name': 'some_split_1', 'trafficTypeName': 'user'})
        split_2 = from_raw_mock({'name': 'some_split_2', 'trafficTypeName': 'user'})
        storage.put(split_1)
        storage.put(split_2)

        splits = storage.get_all_splits()
        s1 = next(split for split in splits if split.name == 'some_split_1')
        s2 = next(split for split in splits if split.name == 'some_split_2')

        assert s1.traffic_type_name == 'user'
        assert s2.traffic_type_name == 'user'
Ejemplo n.º 8
0
    def test_get_split_names(self, mocker):
        """Test getting all split names."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)
        from_raw_mock = self._get_from_raw_mock(mocker)
        mocker.patch('splitio.models.splits.from_raw', new=from_raw_mock)

        split_1 = from_raw_mock({'name': 'some_split_1', 'trafficTypeName': 'user'})
        split_2 = from_raw_mock({'name': 'some_split_2', 'trafficTypeName': 'user'})
        storage.put(split_1)
        storage.put(split_2)

        assert set(storage.get_split_names()) == set(['some_split_1', 'some_split_2'])
        storage.remove('some_split_1')
        assert storage.get_split_names() == ['some_split_2']
Ejemplo n.º 9
0
    def test_set_get_changenumber(self, mocker):
        """Test setting and retrieving changenumber."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)

        assert storage.get_change_number() == None
        storage.set_change_number(123)
        assert storage.get_change_number() == 123
Ejemplo n.º 10
0
def _build_uwsgi_factory(config):
    """Build and return a split factory with redis-based storage."""
    cfg = DEFAULT_CONFIG.copy()
    cfg.update(config)
    sdk_metadata = util.get_metadata(cfg)
    uwsgi_adapter = get_uwsgi()
    storages = {
        'splits': UWSGISplitStorage(uwsgi_adapter),
        'segments': UWSGISegmentStorage(uwsgi_adapter),
        'impressions': UWSGIImpressionStorage(uwsgi_adapter),
        'events': UWSGIEventStorage(uwsgi_adapter),
        'telemetry': UWSGITelemetryStorage(uwsgi_adapter)
    }
    return SplitFactory(storages,
                        cfg['labelsEnabled'],
                        impression_listener=_wrap_impression_listener(
                            cfg['impressionListener'], sdk_metadata))
Ejemplo n.º 11
0
 def test_get_split_names(self, mocker):
     """Test getting all split names."""
     uwsgi = get_uwsgi(True)
     storage = UWSGISplitStorage(uwsgi)
     split_1 = mocker.Mock(spec=Split)
     split_1.to_json.return_value = '{"name": "split1"}'
     split_name_1 = mocker.PropertyMock()
     split_name_1.return_value = 'some_split_1'
     type(split_1).name = split_name_1
     split_2 = mocker.Mock(spec=Split)
     split_2.to_json.return_value = '{"name": "split2"}'
     split_name_2 = mocker.PropertyMock()
     split_name_2.return_value = 'some_split_2'
     type(split_2).name = split_name_2
     storage.put(split_1)
     storage.put(split_2)
     assert set(storage.get_split_names()) == set(['some_split_1', 'some_split_2'])
     storage.remove('some_split_1')
     assert storage.get_split_names() == ['some_split_2']
Ejemplo n.º 12
0
    def test_store_retrieve_split(self, mocker):
        """Test storing and retrieving splits."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)
        from_raw_mock = self._get_from_raw_mock(mocker)
        mocker.patch('splitio.models.splits.from_raw', new=from_raw_mock)

        raw_split = {'name': 'some_split', 'trafficTypeName': 'user'}
        split = from_raw_mock(raw_split)

        from_raw_mock.reset_mock()  # clear mock calls so they don't interfere with the testing itself.
        storage.put(split)

        retrieved = storage.get('some_split')

        assert retrieved.name == split.name and retrieved.traffic_type_name == split.traffic_type_name
        assert from_raw_mock.mock_calls == [mocker.call(raw_split)]
        assert split.to_json.mock_calls == [mocker.call()]

        assert storage.get('nonexistant_split') is None

        storage.remove('some_split')
        assert storage.get('some_split') == None
Ejemplo n.º 13
0
def _build_uwsgi_factory(api_key, cfg):
    """Build and return a split factory with redis-based storage."""
    sdk_metadata = util.get_metadata(cfg)
    uwsgi_adapter = get_uwsgi()
    storages = {
        'splits': UWSGISplitStorage(uwsgi_adapter),
        'segments': UWSGISegmentStorage(uwsgi_adapter),
        'impressions': UWSGIImpressionStorage(uwsgi_adapter),
        'events': UWSGIEventStorage(uwsgi_adapter),
        'telemetry': UWSGITelemetryStorage(uwsgi_adapter)
    }
    return SplitFactory(
        api_key,
        storages,
        cfg['labelsEnabled'],
        ImpressionsManager(storages['impressions'].put, cfg['impressionsMode'], True,
                           _wrap_impression_listener(cfg['impressionListener'], sdk_metadata))
    )
Ejemplo n.º 14
0
    def test_store_retrieve_split(self, mocker):
        """Test storing and retrieving splits."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)
        split = mocker.Mock(spec=Split)
        split.to_json.return_value = '{}'
        split_name = mocker.PropertyMock()
        split_name.return_value = 'some_split'
        type(split).name = split_name
        storage.put(split)

        from_raw_mock = mocker.Mock()
        from_raw_mock.return_value = 'ok'
        mocker.patch('splitio.models.splits.from_raw', new=from_raw_mock)
        retrieved = storage.get('some_split')

        assert retrieved == 'ok'
        assert from_raw_mock.mock_calls == [mocker.call('{}')]
        assert split.to_json.mock_calls == [mocker.call()]

        assert storage.get('nonexistant_split') is None

        storage.remove('some_split')
        assert storage.get('some_split') == None
Ejemplo n.º 15
0
def uwsgi_update_splits(user_config):
    """
    Update splits task.

    :param user_config: User-provided configuration.
    :type user_config: dict
    """
    config = _get_config(user_config)
    seconds = config['featuresRefreshRate']
    split_sync = SplitSynchronizer(
        SplitsAPI(
            HttpClient(1500, config.get('sdk_url'), config.get('events_url')),
            config['apikey']),
        UWSGISplitStorage(get_uwsgi()),
    )

    while True:
        try:
            split_sync.synchronize_splits()  # pylint: disable=protected-access
            time.sleep(seconds)
        except Exception:  # pylint: disable=broad-except
            _LOGGER.error('Error updating splits')
            _LOGGER.debug('Error: ', exc_info=True)
Ejemplo n.º 16
0
    def test_is_valid_traffic_type(self, mocker):
        """Test that traffic type validation works properly."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)
        from_raw_mock = self._get_from_raw_mock(mocker)
        mocker.patch('splitio.models.splits.from_raw', new=from_raw_mock)

        split_1 = from_raw_mock({
            'name': 'some_split_1',
            'trafficTypeName': 'user'
        })
        storage.put(split_1)
        assert storage.is_valid_traffic_type('user') is True
        assert storage.is_valid_traffic_type('account') is False

        split_2 = from_raw_mock({
            'name': 'some_split_2',
            'trafficTypeName': 'account'
        })
        storage.put(split_2)
        assert storage.is_valid_traffic_type('user') is True
        assert storage.is_valid_traffic_type('account') is True

        split_3 = from_raw_mock({
            'name': 'some_split_3',
            'trafficTypeName': 'user'
        })
        storage.put(split_3)
        assert storage.is_valid_traffic_type('user') is True
        assert storage.is_valid_traffic_type('account') is True

        storage.remove('some_split_1')
        assert storage.is_valid_traffic_type('user') is True
        assert storage.is_valid_traffic_type('account') is True

        storage.remove('some_split_2')
        assert storage.is_valid_traffic_type('user') is True
        assert storage.is_valid_traffic_type('account') is False

        storage.remove('some_split_3')
        assert storage.is_valid_traffic_type('user') is False
        assert storage.is_valid_traffic_type('account') is False
Ejemplo n.º 17
0
    def test_kill_locally(self):
        """Test kill local."""
        uwsgi = get_uwsgi(True)
        storage = UWSGISplitStorage(uwsgi)

        split = Split('some_split', 123456789, False, 'some', 'traffic_type',
                      'ACTIVE', 1)
        storage.put(split)
        storage.set_change_number(1)

        storage.kill_locally('test', 'default_treatment', 2)
        assert storage.get('test') is None

        storage.kill_locally('some_split', 'default_treatment', 0)
        assert storage.get('some_split').change_number == 1
        assert storage.get('some_split').killed is False
        assert storage.get('some_split').default_treatment == 'some'

        storage.kill_locally('some_split', 'default_treatment', 3)
        assert storage.get('some_split').change_number == 3