|
1 | 1 | import logging |
2 | 2 | import unittest |
3 | 3 | from os import path |
| 4 | +import tempfile |
| 5 | +import json |
| 6 | +import time |
4 | 7 |
|
5 | 8 | from configcatclient import ConfigCatClient |
6 | 9 | from configcatclient.localdictionarydatasource import LocalDictionaryDataSource |
@@ -65,6 +68,52 @@ def test_non_existent_file(self): |
65 | 68 | self.assertFalse(client.get_value('enabledFeature', False)) |
66 | 69 | client.stop() |
67 | 70 |
|
| 71 | + def test_reload_file(self): |
| 72 | + temp = tempfile.NamedTemporaryFile(mode="w") |
| 73 | + dictionary = {'flags': { |
| 74 | + 'enabledFeature': False |
| 75 | + }} |
| 76 | + json.dump(dictionary, temp) |
| 77 | + temp.flush() |
| 78 | + |
| 79 | + client = ConfigCatClient(sdk_key='test', |
| 80 | + poll_interval_seconds=0, |
| 81 | + max_init_wait_time_seconds=0, |
| 82 | + flag_overrides=LocalFileDataSource(file_path=temp.name, |
| 83 | + override_behaviour=OverrideBehaviour.LocalOnly)) |
| 84 | + |
| 85 | + self.assertFalse(client.get_value('enabledFeature', True)) |
| 86 | + |
| 87 | + time.sleep(0.5) |
| 88 | + |
| 89 | + # clear the content of the temp file |
| 90 | + temp.seek(0) |
| 91 | + temp.truncate() |
| 92 | + |
| 93 | + # change the temporary file |
| 94 | + dictionary['flags']['enabledFeature'] = True |
| 95 | + json.dump(dictionary, temp) |
| 96 | + temp.flush() |
| 97 | + |
| 98 | + self.assertTrue(client.get_value('enabledFeature', False)) |
| 99 | + |
| 100 | + client.stop() |
| 101 | + |
| 102 | + def test_invalid_file(self): |
| 103 | + temp = tempfile.NamedTemporaryFile(mode="w") |
| 104 | + temp.write('{"flags": {"enabledFeature": true}') |
| 105 | + temp.flush() |
| 106 | + |
| 107 | + client = ConfigCatClient(sdk_key='test', |
| 108 | + poll_interval_seconds=0, |
| 109 | + max_init_wait_time_seconds=0, |
| 110 | + flag_overrides=LocalFileDataSource(file_path=temp.name, |
| 111 | + override_behaviour=OverrideBehaviour.LocalOnly)) |
| 112 | + |
| 113 | + self.assertFalse(client.get_value('enabledFeature', False)) |
| 114 | + |
| 115 | + client.stop() |
| 116 | + |
68 | 117 | def test_dictionary(self): |
69 | 118 | dictionary = { |
70 | 119 | 'enabledFeature': True, |
|
0 commit comments