Skip to content

Commit 7b1c3f1

Browse files
fix: block odp methods on datafile (#419)
* add blocking call to odp methods * git ignore mypy cache
1 parent 60ab807 commit 7b1c3f1

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
MANIFEST
33
.idea/*
44
.*virtualenv/*
5+
.mypy_cache
56

67
# Output of building package
78
*.egg-info

optimizely/optimizely.py

+15
Original file line numberDiff line numberDiff line change
@@ -1357,13 +1357,23 @@ def _identify_user(self, user_id: str) -> None:
13571357
self.logger.error(enums.Errors.INVALID_OPTIMIZELY.format('identify_user'))
13581358
return
13591359

1360+
config = self.config_manager.get_config()
1361+
if not config:
1362+
self.logger.error(enums.Errors.INVALID_PROJECT_CONFIG.format('identify_user'))
1363+
return
1364+
13601365
self.odp_manager.identify_user(user_id)
13611366

13621367
def _fetch_qualified_segments(self, user_id: str, options: Optional[list[str]] = None) -> Optional[list[str]]:
13631368
if not self.is_valid:
13641369
self.logger.error(enums.Errors.INVALID_OPTIMIZELY.format('fetch_qualified_segments'))
13651370
return None
13661371

1372+
config = self.config_manager.get_config()
1373+
if not config:
1374+
self.logger.error(enums.Errors.INVALID_PROJECT_CONFIG.format('fetch_qualified_segments'))
1375+
return None
1376+
13671377
return self.odp_manager.fetch_qualified_segments(user_id, options or [])
13681378

13691379
def send_odp_event(
@@ -1391,6 +1401,11 @@ def send_odp_event(
13911401
self.logger.error('ODP events must have at least one key-value pair in identifiers.')
13921402
return
13931403

1404+
config = self.config_manager.get_config()
1405+
if not config:
1406+
self.logger.error(enums.Errors.INVALID_PROJECT_CONFIG.format('send_odp_event'))
1407+
return
1408+
13941409
self.odp_manager.send_event(type, action, identifiers, data or {})
13951410

13961411
def close(self) -> None:

tests/test_optimizely.py

+6-11
Original file line numberDiff line numberDiff line change
@@ -5399,12 +5399,8 @@ def test_send_odp_event__send_event_with_polling_config_manager(self):
53995399
status_code=200,
54005400
content=json.dumps(self.config_dict_with_audience_segments)
54015401
)
5402-
):
5402+
), mock.patch('requests.post', return_value=self.fake_server_response(status_code=200)):
54035403
client = optimizely.Optimizely(sdk_key='test', logger=mock_logger)
5404-
# wait for config
5405-
client.config_manager.get_config()
5406-
5407-
with mock.patch('requests.post', return_value=self.fake_server_response(status_code=200)):
54085404
client.send_odp_event(type='wow', action='great', identifiers={'amazing': 'fantastic'}, data={})
54095405
client.close()
54105406

@@ -5426,9 +5422,12 @@ def test_send_odp_event__log_error_when_odp_disabled(self):
54265422
def test_send_odp_event__log_debug_if_datafile_not_ready(self):
54275423
mock_logger = mock.Mock()
54285424
client = optimizely.Optimizely(sdk_key='test', logger=mock_logger)
5425+
client.config_manager.set_blocking_timeout(0)
54295426
client.send_odp_event(type='wow', action='great', identifiers={'amazing': 'fantastic'}, data={})
54305427

5431-
mock_logger.debug.assert_called_with('ODP event queue: cannot send before config has been set.')
5428+
mock_logger.error.assert_called_with(
5429+
'Invalid config. Optimizely instance is not valid. Failing "send_odp_event".'
5430+
)
54325431
client.close()
54335432

54345433
def test_send_odp_event__log_error_if_odp_not_enabled_with_polling_config_manager(self):
@@ -5439,16 +5438,12 @@ def test_send_odp_event__log_error_if_odp_not_enabled_with_polling_config_manage
54395438
status_code=200,
54405439
content=json.dumps(self.config_dict_with_audience_segments)
54415440
)
5442-
):
5441+
), mock.patch('requests.post', return_value=self.fake_server_response(status_code=200)):
54435442
client = optimizely.Optimizely(
54445443
sdk_key='test',
54455444
logger=mock_logger,
54465445
settings=OptimizelySdkSettings(odp_disabled=True)
54475446
)
5448-
# wait for config
5449-
client.config_manager.get_config()
5450-
5451-
with mock.patch('requests.post', return_value=self.fake_server_response(status_code=200)):
54525447
client.send_odp_event(type='wow', action='great', identifiers={'amazing': 'fantastic'}, data={})
54535448
client.close()
54545449

0 commit comments

Comments
 (0)