Exemple #1
0
    def test_set_update_interval(self, _):
        """ Test set_update_interval with different inputs. """
        with mock.patch(
                'optimizely.config_manager.PollingConfigManager.fetch_datafile'
        ):
            project_config_manager = config_manager.PollingConfigManager(
                sdk_key='some_key')

        # Assert that if invalid update_interval is set, then exception is raised.
        with self.assertRaisesRegexp(
                optimizely_exceptions.InvalidInputException,
                'Invalid update_interval "invalid interval" provided.',
        ):
            project_config_manager.set_update_interval('invalid interval')

        # Assert that update_interval cannot be set to less than allowed minimum and instead is set to default value.
        project_config_manager.set_update_interval(-4.2)
        self.assertEqual(
            enums.ConfigManager.DEFAULT_UPDATE_INTERVAL,
            project_config_manager.update_interval,
        )

        # Assert that if no update_interval is provided, it is set to default value.
        project_config_manager.set_update_interval(None)
        self.assertEqual(
            enums.ConfigManager.DEFAULT_UPDATE_INTERVAL,
            project_config_manager.update_interval,
        )

        # Assert that if valid update_interval is provided, it is set to that value.
        project_config_manager.set_update_interval(42)
        self.assertEqual(42, project_config_manager.update_interval)
Exemple #2
0
    def test_fetch_datafile(self, _):
        """ Test that fetch_datafile sets config and last_modified based on response. """
        with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile'):
            project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key')
        expected_datafile_url = 'https://cdn.optimizely.com/datafiles/some_key.json'
        test_headers = {
            'Last-Modified': 'New Time'
        }
        test_datafile = json.dumps(self.config_dict_with_features)
        test_response = requests.Response()
        test_response.status_code = 200
        test_response.headers = test_headers
        test_response._content = test_datafile
        with mock.patch('requests.get', return_value=test_response):
            project_config_manager.fetch_datafile()

        self.assertEqual(test_headers['Last-Modified'], project_config_manager.last_modified)
        self.assertIsInstance(project_config_manager.get_config(), project_config.ProjectConfig)

        # Call fetch_datafile again and assert that request to URL is with If-Modified-Since header.
        with mock.patch('requests.get', return_value=test_response) as mock_requests:
            project_config_manager.fetch_datafile()

        mock_requests.assert_called_once_with(expected_datafile_url,
                                              headers={'If-Modified-Since': test_headers['Last-Modified']},
                                              timeout=enums.ConfigManager.REQUEST_TIMEOUT)
        self.assertEqual(test_headers['Last-Modified'], project_config_manager.last_modified)
        self.assertIsInstance(project_config_manager.get_config(), project_config.ProjectConfig)
Exemple #3
0
 def test_is_running(self, _):
     """ Test that polling thread is running after instance of PollingConfigManager is created. """
     with mock.patch(
             'optimizely.config_manager.PollingConfigManager.fetch_datafile'
     ):
         project_config_manager = config_manager.PollingConfigManager(
             sdk_key='some_key')
         self.assertTrue(project_config_manager.is_running)
Exemple #4
0
 def test_get_config_blocks(self):
     """ Test that get_config blocks until blocking timeout is hit. """
     start_time = time.time()
     project_config_manager = config_manager.PollingConfigManager(
         sdk_key='sdk_key', blocking_timeout=5)
     # Assert get_config should block until blocking timeout.
     project_config_manager.get_config()
     end_time = time.time()
     self.assertEqual(5, round(end_time - start_time))
Exemple #5
0
    def test_set_last_modified(self, _):
        """ Test that set_last_modified sets last_modified field based on header. """
        project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key')

        last_modified_time = 'Test Last Modified Time'
        test_response_headers = {
            'Last-Modified': last_modified_time,
            'Some-Other-Important-Header': 'some_value'
        }
        project_config_manager.set_last_modified(test_response_headers)
        self.assertEqual(last_modified_time, project_config_manager.last_modified)
    def test_fetch_datafile__status_exception_raised(self, _):
        """ Test that config_manager keeps running if status code exception is raised when fetching datafile. """
        class MockExceptionResponse(object):
            def raise_for_status(self):
                raise requests.exceptions.RequestException('Error Error !!')

        sdk_key = 'some_key'
        mock_logger = mock.Mock()
        with mock.patch(
                'optimizely.config_manager.PollingConfigManager.fetch_datafile'
        ):
            project_config_manager = config_manager.PollingConfigManager(
                sdk_key=sdk_key, logger=mock_logger)
        expected_datafile_url = enums.ConfigManager.DATAFILE_URL_TEMPLATE.format(
            sdk_key=sdk_key)
        test_headers = {'Last-Modified': 'New Time'}
        test_datafile = json.dumps(self.config_dict_with_features)
        test_response = requests.Response()
        test_response.status_code = 200
        test_response.headers = test_headers
        test_response._content = test_datafile
        with mock.patch('requests.get', return_value=test_response):
            project_config_manager.fetch_datafile()

        self.assertEqual(test_headers['Last-Modified'],
                         project_config_manager.last_modified)
        self.assertIsInstance(project_config_manager.get_config(),
                              project_config.ProjectConfig)

        # Call fetch_datafile again, but raise exception this time
        with mock.patch('requests.get',
                        return_value=MockExceptionResponse()) as mock_requests:
            project_config_manager.fetch_datafile()

        mock_requests.assert_called_once_with(
            expected_datafile_url,
            headers={'If-Modified-Since': test_headers['Last-Modified']},
            timeout=enums.ConfigManager.REQUEST_TIMEOUT,
        )
        mock_logger.error.assert_called_once_with(
            'Fetching datafile from {} failed. Error: Error Error !!'.format(
                expected_datafile_url))
        self.assertEqual(test_headers['Last-Modified'],
                         project_config_manager.last_modified)
        self.assertIsInstance(project_config_manager.get_config(),
                              project_config.ProjectConfig)
        # Confirm that config manager keeps running
        self.assertTrue(project_config_manager.is_running)
Exemple #7
0
    def test_set_blocking_timeout(self, _):
        """ Test set_blocking_timeout with different inputs. """
        with mock.patch(
                'optimizely.config_manager.PollingConfigManager.fetch_datafile'
        ):
            project_config_manager = config_manager.PollingConfigManager(
                sdk_key='some_key')

        # Assert that if invalid blocking_timeout is set, then exception is raised.
        with self.assertRaisesRegexp(
                optimizely_exceptions.InvalidInputException,
                'Invalid blocking timeout "invalid timeout" provided.',
        ):
            project_config_manager.set_blocking_timeout('invalid timeout')

        # Assert that blocking_timeout cannot be set to less than allowed minimum and instead is set to default value.
        project_config_manager.set_blocking_timeout(-4)
        self.assertEqual(
            enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT,
            project_config_manager.blocking_timeout,
        )

        # Assert that blocking_timeout can be set to 0.
        project_config_manager.set_blocking_timeout(0)
        self.assertIs(0, project_config_manager.blocking_timeout)

        # Assert that if no blocking_timeout is provided, it is set to default value.
        project_config_manager.set_blocking_timeout(None)
        self.assertEqual(
            enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT,
            project_config_manager.blocking_timeout,
        )

        # Assert that if valid blocking_timeout is provided, it is set to that value.
        project_config_manager.set_blocking_timeout(5)
        self.assertEqual(5, project_config_manager.blocking_timeout)