Skip to content

Commit

Permalink
check tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oakbani committed Sep 26, 2019
1 parent 4296539 commit 4bb2cfb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
18 changes: 9 additions & 9 deletions optimizely/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# limitations under the License.

import abc
import numbers
import requests
import threading
import time
Expand Down Expand Up @@ -95,6 +96,8 @@ def __init__(self,
notification_center=notification_center)
self._config = None
self.validate_schema = not skip_json_validation
self.blocking_timeout = None
self._event = threading.Event()
self._set_config(datafile)

def _set_config(self, datafile):
Expand Down Expand Up @@ -131,9 +134,8 @@ def _set_config(self, datafile):

if previous_revision == config.get_revision():
return
self._condition.acquire()
self._config = config
self._condition.notify() # Notifies the consumer about the availability.
self._event.set()
self.notification_center.send_notifications(enums.NotificationTypes.OPTIMIZELY_CONFIG_UPDATE)
self.logger.debug(
'Received new datafile and updated config. '
Expand All @@ -146,9 +148,8 @@ def get_config(self):
Returns:
ProjectConfig. None if not set.
"""
self._condition.acquire()
self._condition.wait(self.blocking_timeout)
self._condition.release()
if self.blocking_timeout is not None:
self._event.wait(self.blocking_timeout)
return self._config


Expand Down Expand Up @@ -199,7 +200,6 @@ def __init__(self,
self._polling_thread = threading.Thread(target=self._run)
self._polling_thread.setDaemon(True)
self._polling_thread.start()
self._condition = threading.Condition()

@staticmethod
def get_datafile_url(sdk_key, url, url_template):
Expand Down Expand Up @@ -239,7 +239,7 @@ def set_update_interval(self, update_interval):
Args:
update_interval: Time in seconds after which to update datafile.
"""
if not update_interval:
if update_interval is None:
update_interval = enums.ConfigManager.DEFAULT_UPDATE_INTERVAL
self.logger.debug('Set config update interval to default value {}.'.format(update_interval))

Expand All @@ -264,11 +264,11 @@ def set_blocking_timeout(self, blocking_timeout):
Args:
blocking_timeout: Time in seconds to block the config call.
"""
if not blocking_timeout:
if blocking_timeout is None:
blocking_timeout = enums.ConfigManager.DEFAULT_BLOCKING_TIMEOUT
self.logger.debug('Set config blocking timeout to default value {}.'.format(blocking_timeout))

if not isinstance(blocking_timeout, (int, float)):
if not isinstance(blocking_timeout, (numbers.Integral, int)):
raise optimizely_exceptions.InvalidInputException(
'Invalid blocking timeout "{}" provided.'.format(blocking_timeout)
)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import json
import mock
import requests
import time

from optimizely import config_manager
from optimizely import exceptions as optimizely_exceptions
Expand Down Expand Up @@ -235,6 +236,33 @@ def test_set_update_interval(self, _):
project_config_manager.set_update_interval(42)
self.assertEqual(42, project_config_manager.update_interval)

def test_set_blocking_timeout(self, _):
""" Test set_blocking_timeout with different inputs. """
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 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)

# Assert get_config should block until blocking timeout.
start_time = time.time()
project_config_manager.get_config()
end_time = time.time()
self.assertEqual(5, round(end_time - start_time))

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')
Expand Down

0 comments on commit 4bb2cfb

Please sign in to comment.