Skip to content

Commit 1a8fde8

Browse files
authored
Merge pull request #189 from splitio/fix/ready_flag
Fix/ready flag
2 parents 3047f61 + 6cf2f39 commit 1a8fde8

File tree

3 files changed

+7
-10
lines changed

3 files changed

+7
-10
lines changed

splitio/client/factory.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ def __init__( # pylint: disable=too-many-arguments
101101
self._labels_enabled = labels_enabled
102102
self._apis = apis if apis else {}
103103
self._tasks = tasks if tasks else {}
104-
self._sdk_ready_flag = sdk_ready_flag
104+
self._sdk_internal_ready_flag = sdk_ready_flag
105+
self._sdk_ready_flag = threading.Event()
105106
self._impressions_manager = impressions_manager
106107

107108
# If we have a ready flag, it means we have sync tasks that need to finish
108109
# before the SDK client becomes ready.
109-
if self._sdk_ready_flag is not None:
110+
if self._sdk_internal_ready_flag is not None:
110111
self._status = Status.NOT_INITIALIZED
111112
# add a listener that updates the status to READY once the flag is set.
112113
ready_updater = threading.Thread(target=self._update_status_when_ready)
@@ -117,8 +118,9 @@ def __init__( # pylint: disable=too-many-arguments
117118

118119
def _update_status_when_ready(self):
119120
"""Wait until the sdk is ready and update the status."""
120-
self._sdk_ready_flag.wait()
121+
self._sdk_internal_ready_flag.wait()
121122
self._status = Status.READY
123+
self._sdk_ready_flag.set()
122124

123125
def _get_storage(self, name):
124126
"""
@@ -153,11 +155,12 @@ def manager(self):
153155
def block_until_ready(self, timeout=None):
154156
"""
155157
Blocks until the sdk is ready or the timeout specified by the user expires.
158+
When ready, the factory's status is updated accordingly.
156159
157160
:param timeout: Number of seconds to wait (fractions allowed)
158161
:type timeout: int
159162
"""
160-
if self._sdk_ready_flag is not None:
163+
if self._sdk_internal_ready_flag is not None:
161164
ready = self._sdk_ready_flag.wait(timeout)
162165

163166
if not ready:

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)