Skip to content

Commit 4208b4d

Browse files
authored
Merge pull request #182 from sethbrite/ensure_factory_readiness
Make block_until_ready() guarantee factory readiness
2 parents 3047f61 + 7ea6cbb commit 4208b4d

File tree

3 files changed

+4
-12
lines changed

3 files changed

+4
-12
lines changed

splitio/client/factory.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,12 @@ def __init__( # pylint: disable=too-many-arguments
109109
if self._sdk_ready_flag is not None:
110110
self._status = Status.NOT_INITIALIZED
111111
# add a listener that updates the status to READY once the flag is set.
112-
ready_updater = threading.Thread(target=self._update_status_when_ready)
112+
ready_updater = threading.Thread(target=self.block_until_ready)
113113
ready_updater.setDaemon(True)
114114
ready_updater.start()
115115
else:
116116
self._status = Status.READY
117117

118-
def _update_status_when_ready(self):
119-
"""Wait until the sdk is ready and update the status."""
120-
self._sdk_ready_flag.wait()
121-
self._status = Status.READY
122-
123118
def _get_storage(self, name):
124119
"""
125120
Return a reference to the specified storage.
@@ -153,6 +148,7 @@ def manager(self):
153148
def block_until_ready(self, timeout=None):
154149
"""
155150
Blocks until the sdk is ready or the timeout specified by the user expires.
151+
When ready, the factory's status is updated accordingly.
156152
157153
:param timeout: Number of seconds to wait (fractions allowed)
158154
:type timeout: int
@@ -163,6 +159,8 @@ def block_until_ready(self, timeout=None):
163159
if not ready:
164160
raise TimeoutException('SDK Initialization: time of %d exceeded' % timeout)
165161

162+
self._status = Status.READY
163+
166164
@property
167165
def ready(self):
168166
"""

tests/client/test_factory.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ def _segment_task_init_mock(self, api, storage, split_storage, period, event):
8686
assert factory._tasks['telemetry']._api == factory._apis['telemetry']
8787
assert factory._labels_enabled is True
8888
factory.block_until_ready()
89-
time.sleep(1) # give a chance for the bg thread to set the ready status
9089
assert factory.ready
9190
factory.destroy()
9291

@@ -164,7 +163,6 @@ def test_redis_client_creation(self, mocker):
164163
assert factory._labels_enabled is False
165164
assert isinstance(factory._impressions_manager, ImpressionsManager)
166165
factory.block_until_ready()
167-
time.sleep(1) # give a chance for the bg thread to set the ready status
168166
assert factory.ready
169167
factory.destroy()
170168

@@ -181,7 +179,6 @@ def test_uwsgi_client_creation(self):
181179
assert factory._tasks == {}
182180
assert factory._labels_enabled is True
183181
factory.block_until_ready()
184-
time.sleep(1) # give a chance for the bg thread to set the ready status
185182
assert factory.ready
186183
factory.destroy()
187184

@@ -232,7 +229,6 @@ def _event_task_init_mock(self, api, storage, refresh_rate, bulk_size):
232229
# Start factory and make assertions
233230
factory = get_factory('some_api_key')
234231
factory.block_until_ready()
235-
time.sleep(1) # give a chance for the bg thread to set the ready status
236232
assert factory.ready
237233
assert factory.destroyed is False
238234

@@ -302,7 +298,6 @@ def _telemetry_task_init_mock(self, api, storage, refresh_rate):
302298
assert factory.destroyed is False
303299

304300
factory.block_until_ready()
305-
time.sleep(1) # give a chance for the bg thread to set the ready status
306301
assert factory.ready
307302

308303
event = threading.Event()

tests/integration/test_client_e2e.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,6 @@ def test_localhost_e2e(self):
798798
filename = os.path.join(os.path.dirname(__file__), 'files', 'file2.yaml')
799799
factory = get_factory('localhost', config={'splitFile': filename})
800800
factory.block_until_ready()
801-
time.sleep(1)
802801
client = factory.client()
803802
assert client.get_treatment_with_config('key', 'my_feature') == ('on', '{"desc" : "this applies only to ON treatment"}')
804803
assert client.get_treatment_with_config('only_key', 'my_feature') == (

0 commit comments

Comments
 (0)