Skip to content

Commit 7ea6cbb

Browse files
committed
Make block_until_ready() guarantee factory readiness
1 parent 69892f6 commit 7ea6cbb

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
@@ -108,17 +108,12 @@ def __init__( # pylint: disable=too-many-arguments
108108
if self._sdk_ready_flag is not None:
109109
self._status = Status.NOT_INITIALIZED
110110
# add a listener that updates the status to READY once the flag is set.
111-
ready_updater = threading.Thread(target=self._update_status_when_ready)
111+
ready_updater = threading.Thread(target=self.block_until_ready)
112112
ready_updater.setDaemon(True)
113113
ready_updater.start()
114114
else:
115115
self._status = Status.READY
116116

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

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

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._impression_listener, ImpressionListenerWrapper)
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

@@ -182,7 +180,6 @@ def test_uwsgi_client_creation(self):
182180
assert factory._labels_enabled is True
183181
assert factory._impression_listener is None
184182
factory.block_until_ready()
185-
time.sleep(1) # give a chance for the bg thread to set the ready status
186183
assert factory.ready
187184
factory.destroy()
188185

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

@@ -303,7 +299,6 @@ def _telemetry_task_init_mock(self, api, storage, refresh_rate):
303299
assert factory.destroyed is False
304300

305301
factory.block_until_ready()
306-
time.sleep(1) # give a chance for the bg thread to set the ready status
307302
assert factory.ready
308303

309304
event = threading.Event()

tests/integration/test_client_e2e.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,6 @@ def test_localhost_e2e(self):
578578
filename = os.path.join(os.path.dirname(__file__), 'files', 'file2.yaml')
579579
factory = get_factory('localhost', config={'splitFile': filename})
580580
factory.block_until_ready()
581-
time.sleep(1)
582581
client = factory.client()
583582
assert client.get_treatment_with_config('key', 'my_feature') == ('on', '{"desc" : "this applies only to ON treatment"}')
584583
assert client.get_treatment_with_config('only_key', 'my_feature') == (

0 commit comments

Comments
 (0)