|
| 1 | +import logging |
1 | 2 | import time |
2 | 3 |
|
3 | 4 | from ldclient.client import Config, Context, LDClient |
|
10 | 11 | from ldclient.testing.builders import * |
11 | 12 | from ldclient.testing.mock_components import MockBigSegmentStore |
12 | 13 | from ldclient.testing.stub_util import MockEventProcessor, MockUpdateProcessor |
13 | | -from ldclient.testing.test_ldclient import make_client, user |
| 14 | +from ldclient.testing.test_ldclient import make_client, unreachable_uri, user |
14 | 15 | from ldclient.versioned_data_kind import FEATURES, SEGMENTS |
15 | 16 |
|
16 | 17 | flag1 = {'key': 'key1', 'version': 100, 'on': False, 'offVariation': 0, 'variations': ['value1'], 'trackEvents': False} |
@@ -425,3 +426,42 @@ def fake_evaluate(flag, context, event_factory): |
425 | 426 | assert metadata['good'].get('prerequisites') == ['prereq-of-good'] |
426 | 427 | assert 'prerequisites' not in metadata['bad-first'] |
427 | 428 | assert 'prerequisites' not in metadata['bad-last'] |
| 429 | + |
| 430 | + |
| 431 | +class UninitializedUpdateProcessor(MockUpdateProcessor): |
| 432 | + def initialized(self): |
| 433 | + return False |
| 434 | + |
| 435 | + |
| 436 | +def make_client_with_cached_data(store): |
| 437 | + return LDClient( |
| 438 | + config=Config( |
| 439 | + sdk_key='SDK_KEY', |
| 440 | + base_uri=unreachable_uri, |
| 441 | + events_uri=unreachable_uri, |
| 442 | + stream_uri=unreachable_uri, |
| 443 | + event_processor_class=MockEventProcessor, |
| 444 | + update_processor_class=UninitializedUpdateProcessor, |
| 445 | + feature_store=store, |
| 446 | + ) |
| 447 | + ) |
| 448 | + |
| 449 | + |
| 450 | +def test_cached_data_warnings_are_logged_once_per_client(caplog): |
| 451 | + caplog.set_level(logging.WARNING, logger='ldclient.util') |
| 452 | + store = InMemoryFeatureStore() |
| 453 | + store.init({FEATURES: {'key1': flag1}}) |
| 454 | + client = make_client_with_cached_data(store) |
| 455 | + |
| 456 | + assert client.variation('key1', user, default='default') == 'value1' |
| 457 | + assert client.variation('key1', user, default='default') == 'value1' |
| 458 | + assert client.all_flags_state(user).valid |
| 459 | + assert client.all_flags_state(user).valid |
| 460 | + |
| 461 | + warnings = get_log_lines(caplog, 'WARNING') |
| 462 | + eval_warnings = [m for m in warnings if m.startswith('Feature Flag evaluation attempted')] |
| 463 | + all_flags_warnings = [m for m in warnings if m.startswith('all_flags_state() called before')] |
| 464 | + assert len(eval_warnings) == 1 |
| 465 | + assert len(all_flags_warnings) == 1 |
| 466 | + assert eval_warnings[0].endswith('This message is logged once.') |
| 467 | + assert all_flags_warnings[0].endswith('This message is logged once.') |
0 commit comments