From 58c8547fe75983d235b96cdd2e0bce7b9834623f Mon Sep 17 00:00:00 2001 From: kp-cat <52385411+kp-cat@users.noreply.github.com> Date: Fri, 8 Apr 2022 12:23:24 +0200 Subject: [PATCH 1/3] test_reload_file + test_invalid_file --- configcatclient/localfiledatasource.py | 15 ++++++- configcatclienttests/test_local.py | 60 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/configcatclient/localfiledatasource.py b/configcatclient/localfiledatasource.py index 9f5c0b7..20c3f8a 100644 --- a/configcatclient/localfiledatasource.py +++ b/configcatclient/localfiledatasource.py @@ -25,10 +25,23 @@ def get_overrides(self): def _reload_file_content(self): try: stamp = os.stat(self._file_path).st_mtime + # test + print('reload_file_content stamp:' + str(stamp)) + print('self._cached_file_stamp:' + str(self._cached_file_stamp)) + # test if stamp != self._cached_file_stamp: + # test + print('stamp != self._cached_file_stamp') + # test self._cached_file_stamp = stamp with open(self._file_path) as file: + # test + print('open') + # test data = json.load(file) + # test + print('json.load:' + str(data)) + # test if 'flags' in data: dictionary = {} source = data['flags'] @@ -39,6 +52,6 @@ def _reload_file_content(self): self._settings = data except OSError as e: log.error('Could not read the content of the file %s. %s' % (self._file_path, e)) - except json.decoder.JSONDecodeError as e: + except ValueError as e: log.error('Could not decode json from file %s. %s' % (self._file_path, e)) diff --git a/configcatclienttests/test_local.py b/configcatclienttests/test_local.py index 6823729..463759d 100644 --- a/configcatclienttests/test_local.py +++ b/configcatclienttests/test_local.py @@ -1,6 +1,9 @@ import logging import unittest from os import path +import tempfile +import json +import shutil from configcatclient import ConfigCatClient from configcatclient.localdictionarydatasource import LocalDictionaryDataSource @@ -65,6 +68,63 @@ def test_non_existent_file(self): self.assertFalse(client.get_value('enabledFeature', False)) client.stop() + def test_reload_file(self): + temp_dir = tempfile.mkdtemp() + try: + temp_path = path.join(temp_dir, 'test-simple.json') + dictionary = {'flags': { + 'enabledFeature': False + }} + with open(temp_path, 'w') as f: + json.dump(dictionary, f) + + client = ConfigCatClient(sdk_key='test', + poll_interval_seconds=0, + max_init_wait_time_seconds=0, + flag_overrides=LocalFileDataSource(file_path=temp_path, + override_behaviour=OverrideBehaviour.LocalOnly)) + + self.assertFalse(client.get_value('enabledFeature', True)) + + # test + with open(temp_path, 'r') as f: + print(f.read()) + + import time + time.sleep(2) + # test + + # change the temporary file + dictionary['flags']['enabledFeature'] = True + with open(temp_path, 'w') as f: + json.dump(dictionary, f) + + # test + with open(temp_path, 'r') as f: + print(f.read()) + # test + + self.assertTrue(client.get_value('enabledFeature', False)) + + client.stop() + finally: + shutil.rmtree(temp_dir) + + def test_invalid_file(self): + temp = tempfile.NamedTemporaryFile(mode="w") + temp.write('{"flags": {"enabledFeature": true}') + temp.flush() + + client = ConfigCatClient(sdk_key='test', + poll_interval_seconds=0, + max_init_wait_time_seconds=0, + flag_overrides=LocalFileDataSource(file_path=temp.name, + override_behaviour=OverrideBehaviour.LocalOnly)) + + self.assertFalse(client.get_value('enabledFeature', False)) + + client.stop() + def test_dictionary(self): dictionary = { 'enabledFeature': True, From 2fcedf2a756384a490480d6fbb2d7a9225c506b5 Mon Sep 17 00:00:00 2001 From: kp-cat <52385411+kp-cat@users.noreply.github.com> Date: Fri, 8 Apr 2022 15:06:43 +0200 Subject: [PATCH 2/3] test_reload_file + test_invalid_file --- configcatclient/localfiledatasource.py | 13 ----- configcatclienttests/test_local.py | 71 +++++++++++--------------- 2 files changed, 30 insertions(+), 54 deletions(-) diff --git a/configcatclient/localfiledatasource.py b/configcatclient/localfiledatasource.py index 20c3f8a..d6120e8 100644 --- a/configcatclient/localfiledatasource.py +++ b/configcatclient/localfiledatasource.py @@ -25,23 +25,10 @@ def get_overrides(self): def _reload_file_content(self): try: stamp = os.stat(self._file_path).st_mtime - # test - print('reload_file_content stamp:' + str(stamp)) - print('self._cached_file_stamp:' + str(self._cached_file_stamp)) - # test if stamp != self._cached_file_stamp: - # test - print('stamp != self._cached_file_stamp') - # test self._cached_file_stamp = stamp with open(self._file_path) as file: - # test - print('open') - # test data = json.load(file) - # test - print('json.load:' + str(data)) - # test if 'flags' in data: dictionary = {} source = data['flags'] diff --git a/configcatclienttests/test_local.py b/configcatclienttests/test_local.py index 463759d..1c1e2f0 100644 --- a/configcatclienttests/test_local.py +++ b/configcatclienttests/test_local.py @@ -3,7 +3,7 @@ from os import path import tempfile import json -import shutil +import time from configcatclient import ConfigCatClient from configcatclient.localdictionarydatasource import LocalDictionaryDataSource @@ -69,46 +69,35 @@ def test_non_existent_file(self): client.stop() def test_reload_file(self): - temp_dir = tempfile.mkdtemp() - try: - temp_path = path.join(temp_dir, 'test-simple.json') - dictionary = {'flags': { - 'enabledFeature': False - }} - with open(temp_path, 'w') as f: - json.dump(dictionary, f) - - client = ConfigCatClient(sdk_key='test', - poll_interval_seconds=0, - max_init_wait_time_seconds=0, - flag_overrides=LocalFileDataSource(file_path=temp_path, - override_behaviour=OverrideBehaviour.LocalOnly)) - - self.assertFalse(client.get_value('enabledFeature', True)) - - # test - with open(temp_path, 'r') as f: - print(f.read()) - - import time - time.sleep(2) - # test - - # change the temporary file - dictionary['flags']['enabledFeature'] = True - with open(temp_path, 'w') as f: - json.dump(dictionary, f) - - # test - with open(temp_path, 'r') as f: - print(f.read()) - # test - - self.assertTrue(client.get_value('enabledFeature', False)) - - client.stop() - finally: - shutil.rmtree(temp_dir) + temp = tempfile.NamedTemporaryFile(mode="w") + dictionary = {'flags': { + 'enabledFeature': False + }} + json.dump(dictionary, temp) + temp.flush() + + client = ConfigCatClient(sdk_key='test', + poll_interval_seconds=0, + max_init_wait_time_seconds=0, + flag_overrides=LocalFileDataSource(file_path=temp.name, + override_behaviour=OverrideBehaviour.LocalOnly)) + + self.assertFalse(client.get_value('enabledFeature', True)) + + time.sleep(0.5) + + # clear the content of the temp file + temp.seek(0) + temp.truncate() + + # change the temporary file + dictionary['flags']['enabledFeature'] = True + json.dump(dictionary, temp) + temp.flush() + + self.assertTrue(client.get_value('enabledFeature', False)) + + client.stop() def test_invalid_file(self): temp = tempfile.NamedTemporaryFile(mode="w") From a4847023f9e6208fad910c60b98ab6ba71d80a9d Mon Sep 17 00:00:00 2001 From: kp-cat <52385411+kp-cat@users.noreply.github.com> Date: Fri, 8 Apr 2022 15:19:53 +0200 Subject: [PATCH 3/3] bump version 6.0.1 --- configcatclient/version.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/configcatclient/version.py b/configcatclient/version.py index 9e5cb12..2519168 100644 --- a/configcatclient/version.py +++ b/configcatclient/version.py @@ -1 +1 @@ -CONFIGCATCLIENT_VERSION = "6.0.0" +CONFIGCATCLIENT_VERSION = "6.0.1" diff --git a/setup.py b/setup.py index 05f5938..fe323d0 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ def parse_requirements(filename): return [line for line in lines if line] -configcatclient_version = '6.0.0' +configcatclient_version = '6.0.1' requirements = parse_requirements('requirements.txt')