Skip to content

Commit 5e2a2cf

Browse files
authored
autopoll unit test improvements + cleanup + comments (#42)
1 parent 2ad785b commit 5e2a2cf

File tree

8 files changed

+57
-74
lines changed

8 files changed

+57
-74
lines changed

configcatclient/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def create_client_with_auto_poll(sdk_key, poll_interval_seconds=60, max_init_wai
5858
:param connect_timeout_seconds: The number of seconds to wait for the server to make the initial connection
5959
(i.e. completing the TCP connection handshake). Default: 10 seconds.
6060
:param read_timeout_seconds: The number of seconds to wait for the server to respond before giving up. Default: 30 seconds.
61-
:param flag_overrides: An FlagOverrides implementation used to override feature flags & settings.
61+
:param flag_overrides: A FlagOverrides implementation used to override feature flags & settings.
6262
:param data_governance:
6363
Default: Global. Set this parameter to be in sync with the Data Governance preference on the Dashboard: \n
6464
https://app.configcat.com/organization/data-governance \n
@@ -104,7 +104,7 @@ def create_client_with_lazy_load(sdk_key, cache_time_to_live_seconds=60, config_
104104
:param connect_timeout_seconds: The number of seconds to wait for the server to make the initial connection
105105
(i.e. completing the TCP connection handshake). Default: 10 seconds.
106106
:param read_timeout_seconds: The number of seconds to wait for the server to respond before giving up. Default: 30 seconds.
107-
:param flag_overrides: An FlagOverrides implementation used to override feature flags & settings.
107+
:param flag_overrides: A FlagOverrides implementation used to override feature flags & settings.
108108
:param data_governance:
109109
Default: Global. Set this parameter to be in sync with the Data Governance preference on the Dashboard: \n
110110
https://app.configcat.com/organization/data-governance \n
@@ -144,7 +144,7 @@ def create_client_with_manual_poll(sdk_key, config_cache=None,
144144
:param connect_timeout_seconds: The number of seconds to wait for the server to make the initial connection
145145
(i.e. completing the TCP connection handshake). Default: 10 seconds.
146146
:param read_timeout_seconds: The number of seconds to wait for the server to respond before giving up. Default: 30 seconds.
147-
:param flag_overrides: An FlagOverrides implementation used to override feature flags & settings.
147+
:param flag_overrides: A FlagOverrides implementation used to override feature flags & settings.
148148
:param data_governance:
149149
Default: Global. Set this parameter to be in sync with the Data Governance preference on the Dashboard: \n
150150
https://app.configcat.com/organization/data-governance \n

configcatclient/configcatclient.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(self,
8181
else:
8282
self._override_data_source = None
8383

84-
self._config_cache = options.config_cache if options.config_cache is not None else NullConfigCache()
84+
config_cache = options.config_cache if options.config_cache is not None else NullConfigCache()
8585

8686
if self._override_data_source and self._override_data_source.get_behaviour() == OverrideBehaviour.LocalOnly:
8787
self._config_fetcher = None
@@ -99,7 +99,7 @@ def __init__(self,
9999
self._hooks,
100100
self._config_fetcher,
101101
self.log,
102-
self._config_cache,
102+
config_cache,
103103
options.offline)
104104

105105
def get_value(self, key, default_value, user=None):
@@ -139,8 +139,8 @@ def get_value_details(self, key, default_value, user=None):
139139
"""
140140
settings, fetch_time = self.__get_settings()
141141
if settings is None:
142-
message = 'Evaluating get_value(\'{}\') failed. Cache is empty. ' \
143-
'Returning default_value in your get_value call: [{}].'.format(key, str(default_value))
142+
message = 'Evaluating get_value_details(\'{}\') failed. Cache is empty. ' \
143+
'Returning default_value in your get_value_details call: [{}].'.format(key, str(default_value))
144144
self.log.error(message)
145145
details = EvaluationDetails.from_error(key, default_value, message)
146146
self._hooks.invoke_on_flag_evaluated(details)

configcatclient/configcatoptions.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,6 @@ def __init__(self,
8686
default_user=None,
8787
hooks=None,
8888
offline=False):
89-
"""
90-
Default: `DataGovernance.Global`. Set this parameter to be in sync with the
91-
Data Governance preference on the [Dashboard](https://app.configcat.com/organization/data-governance).
92-
(Only Organization Admins have access)
93-
"""
94-
self.data_governance = data_governance
95-
9689
# The base ConfigCat CDN url.
9790
self.base_url = base_url
9891

@@ -117,6 +110,11 @@ def __init__(self,
117110
# Feature flag and setting overrides.
118111
self.flag_overrides = flag_overrides
119112

113+
# Default: `DataGovernance.Global`. Set this parameter to be in sync with the
114+
# Data Governance preference on the [Dashboard](https://app.configcat.com/organization/data-governance).
115+
# (Only Organization Admins have access)
116+
self.data_governance = data_governance
117+
120118
# The default user, used as fallback when there's no user parameter is passed to the getValue() method.
121119
self.default_user = default_user
122120

configcatclient/configentry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ def is_empty(self):
2727

2828
def to_json(self):
2929
return {
30+
ConfigEntry.CONFIG: self.config,
3031
ConfigEntry.ETAG: self.etag,
31-
ConfigEntry.FETCH_TIME: self.fetch_time,
32-
ConfigEntry.CONFIG: self.config
32+
ConfigEntry.FETCH_TIME: self.fetch_time
3333
}
3434

3535

configcatclient/configfetcher.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ def __init__(self, status, entry, error=None, is_transient_error=False):
4141

4242
def is_fetched(self):
4343
"""Gets whether a new configuration value was fetched or not.
44-
:return: True if a new configuration value was fetched, otherwise false.
44+
:return: True if a new configuration value was fetched, otherwise False.
4545
"""
4646
return self._status == Status.Fetched
4747

4848
def is_not_modified(self):
4949
"""Gets whether the fetch resulted a '304 Not Modified' or not.
50-
:return: True if the fetch resulted a '304 Not Modified' code, otherwise false.
50+
:return: True if the fetch resulted a '304 Not Modified' code, otherwise False.
5151
"""
5252
return self._status == Status.NotModified
5353

5454
def is_failed(self):
5555
"""Gets whether the fetch failed or not.
56-
:return: True if the fetch failed, otherwise false.
56+
:return: True if the fetch failed, otherwise False.
5757
"""
5858
return self._status == Status.Failure
5959

configcatclient/interfaces.py

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,6 @@ def set(self, key, value):
2020
"""
2121

2222

23-
class CachePolicy(object):
24-
"""
25-
Cache policy interface
26-
"""
27-
__metaclass__ = ABCMeta
28-
29-
@abstractmethod
30-
def get(self):
31-
"""
32-
:returns the config json object from the cache, fetch time of the config
33-
"""
34-
35-
@abstractmethod
36-
def force_refresh(self):
37-
"""
38-
39-
:return:
40-
"""
41-
42-
@abstractmethod
43-
def stop(self):
44-
"""
45-
46-
:return:
47-
"""
48-
49-
5023
class ConfigCatClientException(Exception):
5124
"""
5225
Generic ConfigCatClientException

configcatclienttests/test_autopollingcachepolicy.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ def test_return_cached_config_when_cache_is_not_expired(self):
228228

229229
time.sleep(3)
230230

231+
settings, _ = cache_policy.get_settings()
232+
233+
self.assertEqual('testValue', settings.get('testKey').get(VALUE))
231234
self.assertEqual(config_fetcher.get_call_count, 1)
232235
self.assertEqual(config_fetcher.get_fetch_count, 1)
233236
cache_policy.close()
@@ -295,6 +298,8 @@ def test_online_offline(self):
295298

296299
cache_policy.set_offline()
297300
self.assertTrue(cache_policy.is_offline())
301+
settings, _ = cache_policy.get_settings()
302+
self.assertEqual('testValue', settings.get('testStringKey').get(VALUE))
298303
self.assertEqual(2, request_get.call_count)
299304

300305
time.sleep(2)
@@ -323,16 +328,23 @@ def test_init_offline(self):
323328
Hooks(), config_fetcher, log, NullConfigCache(), True)
324329

325330
self.assertTrue(cache_policy.is_offline())
331+
settings, _ = cache_policy.get_settings()
332+
self.assertIsNone(settings)
326333
self.assertEqual(0, request_get.call_count)
327334

328335
time.sleep(2)
329336

337+
settings, _ = cache_policy.get_settings()
338+
self.assertIsNone(settings)
330339
self.assertEqual(0, request_get.call_count)
340+
331341
cache_policy.set_online()
332342
self.assertFalse(cache_policy.is_offline())
333343

334344
time.sleep(2.5)
335345

346+
settings, _ = cache_policy.get_settings()
347+
self.assertEqual('testValue', settings.get('testStringKey').get(VALUE))
336348
self.assertGreaterEqual(request_get.call_count, 2)
337349
cache_policy.close()
338350

0 commit comments

Comments
 (0)