fb-python-sdk 1.1.9__tar.gz → 1.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. {fb_python_sdk-1.1.9/fb_python_sdk.egg-info → fb_python_sdk-1.2.0}/PKG-INFO +7 -1
  2. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/README.md +6 -0
  3. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0/fb_python_sdk.egg-info}/PKG-INFO +7 -1
  4. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fb_python_sdk.egg-info/SOURCES.txt +1 -0
  5. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/client.py +4 -2
  6. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/common_types.py +2 -2
  7. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/evaluator.py +3 -1
  8. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/streaming.py +16 -11
  9. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/utils/__init__.py +1 -1
  10. fb_python_sdk-1.2.0/release/package.json +3 -0
  11. fb_python_sdk-1.2.0/tests/test_insights_enabled.py +161 -0
  12. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/tests/test_runtime_safety.py +87 -0
  13. fb_python_sdk-1.1.9/release/package.json +0 -3
  14. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/LICENSE +0 -0
  15. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/MANIFEST.in +0 -0
  16. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/dev-requirements.txt +0 -0
  17. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fb_python_sdk.egg-info/dependency_links.txt +0 -0
  18. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fb_python_sdk.egg-info/requires.txt +0 -0
  19. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fb_python_sdk.egg-info/top_level.txt +0 -0
  20. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/__init__.py +0 -0
  21. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/category.py +0 -0
  22. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/config.py +0 -0
  23. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/data_storage.py +0 -0
  24. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/event_processor.py +0 -0
  25. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/event_types.py +0 -0
  26. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/flag_change_notification.py +0 -0
  27. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/interfaces.py +0 -0
  28. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/notice_broadcaster.py +0 -0
  29. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/status.py +0 -0
  30. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/status_types.py +0 -0
  31. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/update_processor.py +0 -0
  32. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/utils/exponential_backoff_jitter_strategy.py +0 -0
  33. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/utils/http_client.py +0 -0
  34. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/utils/repeatable_task.py +0 -0
  35. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/utils/rwlock.py +0 -0
  36. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/fbclient/utils/variation_splitting_algorithm.py +0 -0
  37. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/pyproject.toml +0 -0
  38. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/requirements.txt +0 -0
  39. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/setup.cfg +0 -0
  40. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/setup.py +0 -0
  41. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/tests/test_data_storage.py +0 -0
  42. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/tests/test_data_update_status_provider.py +0 -0
  43. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/tests/test_evaluator.py +0 -0
  44. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/tests/test_event_processor.py +0 -0
  45. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/tests/test_fbclient.py +0 -0
  46. {fb_python_sdk-1.1.9 → fb_python_sdk-1.2.0}/tests/test_flag_change_notification.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fb-python-sdk
3
- Version: 1.1.9
3
+ Version: 1.2.0
4
4
  Summary: A Python SDK for FeatBit plateform
5
5
  Home-page: https://github.com/featbit/featbit-python-sdk
6
6
  Author: Dian SUN
@@ -51,6 +51,12 @@ Dynamic: summary
51
51
 
52
52
  # FeatBit Server-Side SDK for Python
53
53
 
54
+ > **Important upgrade notice:** Versions **1.1.8 and 1.1.9** contain a streaming
55
+ > regression that can stop flag updates after the first heartbeat, leaving
56
+ > applications evaluating cached values. Online streaming users should upgrade to
57
+ > **1.1.10** as soon as it is available and update pinned dependencies or lock files.
58
+ > See [issue #18](https://github.com/featbit/featbit-python-sdk/issues/18).
59
+
54
60
  ## Introduction
55
61
 
56
62
  This is the Python Server-Side SDK for the 100% open-source feature flags management platform [FeatBit](https://github.com/featbit/featbit).
@@ -1,5 +1,11 @@
1
1
  # FeatBit Server-Side SDK for Python
2
2
 
3
+ > **Important upgrade notice:** Versions **1.1.8 and 1.1.9** contain a streaming
4
+ > regression that can stop flag updates after the first heartbeat, leaving
5
+ > applications evaluating cached values. Online streaming users should upgrade to
6
+ > **1.1.10** as soon as it is available and update pinned dependencies or lock files.
7
+ > See [issue #18](https://github.com/featbit/featbit-python-sdk/issues/18).
8
+
3
9
  ## Introduction
4
10
 
5
11
  This is the Python Server-Side SDK for the 100% open-source feature flags management platform [FeatBit](https://github.com/featbit/featbit).
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: fb-python-sdk
3
- Version: 1.1.9
3
+ Version: 1.2.0
4
4
  Summary: A Python SDK for FeatBit plateform
5
5
  Home-page: https://github.com/featbit/featbit-python-sdk
6
6
  Author: Dian SUN
@@ -51,6 +51,12 @@ Dynamic: summary
51
51
 
52
52
  # FeatBit Server-Side SDK for Python
53
53
 
54
+ > **Important upgrade notice:** Versions **1.1.8 and 1.1.9** contain a streaming
55
+ > regression that can stop flag updates after the first heartbeat, leaving
56
+ > applications evaluating cached values. Online streaming users should upgrade to
57
+ > **1.1.10** as soon as it is available and update pinned dependencies or lock files.
58
+ > See [issue #18](https://github.com/featbit/featbit-python-sdk/issues/18).
59
+
54
60
  ## Introduction
55
61
 
56
62
  This is the Python Server-Side SDK for the 100% open-source feature flags management platform [FeatBit](https://github.com/featbit/featbit).
@@ -39,4 +39,5 @@ tests/test_evaluator.py
39
39
  tests/test_event_processor.py
40
40
  tests/test_fbclient.py
41
41
  tests/test_flag_change_notification.py
42
+ tests/test_insights_enabled.py
42
43
  tests/test_runtime_safety.py
@@ -133,6 +133,8 @@ class FBClient:
133
133
 
134
134
  def _send_event_safely(self, event):
135
135
  try:
136
+ if isinstance(event, FlagEvent) and not event.is_send_event:
137
+ return
136
138
  if not self._closed:
137
139
  self._event_processor.send_event(event)
138
140
  except Exception:
@@ -273,7 +275,7 @@ class FBClient:
273
275
  def variation(self, key: str, user: dict, default: Any = None) -> Any:
274
276
  """Return the variation of a feature flag for a given user.
275
277
 
276
- This method will send an event back to feature flag center immediately if no error occurs.
278
+ This method collects an evaluation event if evaluation succeeds and insights are enabled for the flag.
277
279
 
278
280
  The default value should be a string, boolean, numeric, or json type.
279
281
 
@@ -297,7 +299,7 @@ class FBClient:
297
299
  """"Return the variation of a feature flag for a given user, but also provides additional information
298
300
  about how this value was calculated, in the property `data` of the :class:`fbclient.common_types.EvalDetail`.
299
301
 
300
- This method will send an event back to feature flag center immediately if no error occurs.
302
+ This method collects an evaluation event if evaluation succeeds and insights are enabled for the flag.
301
303
 
302
304
  The default value should be a string, boolean, numeric, or json type.
303
305
 
@@ -232,7 +232,7 @@ class AllFlagStates(BasicFlagState, Jsonfy):
232
232
  def get_variation(self, key_name: str, default: Any = None) -> Any:
233
233
  """Return the variation of a given feature flag key
234
234
 
235
- This method will send event to back to feature flag center immediately
235
+ This method submits the evaluation event only if insights were enabled when the flag was evaluated.
236
236
 
237
237
  The default value should be a string, boolean, numeric, or json type.
238
238
 
@@ -244,7 +244,7 @@ class AllFlagStates(BasicFlagState, Jsonfy):
244
244
  def get(self, key_name: str, default: Any = None) -> EvalDetail:
245
245
  """Return the flag evaluation details of a given feature flag key
246
246
 
247
- This method will send event to back to feature flag center immediately
247
+ This method submits the evaluation event only if insights were enabled when the flag was evaluated.
248
248
 
249
249
  The default value should be a string, boolean, numeric, or json type.
250
250
 
@@ -95,7 +95,9 @@ class Evaluator:
95
95
  finally:
96
96
  if er is not None:
97
97
  log.info('FB Python SDK: User %s, Feature Flag %s, Flag Value %s' % (user.get('KeyId'), er.key_name, er.value))
98
- if fb_event is not None:
98
+ # Match the server default: only an explicit boolean false
99
+ # disables insights; evaluation and experiment eligibility stay unchanged.
100
+ if fb_event is not None and flag.get('insightsEnabled') is not False:
99
101
  fb_event.add(FlagEventVariation(er))
100
102
 
101
103
  # return a value when flag is off
@@ -11,7 +11,7 @@ from fbclient.flag_change_notification import FlagChangedNotice
11
11
  from fbclient.interfaces import UpdateProcessor
12
12
  from fbclient.notice_broadcaster import NoticeBroadcater
13
13
  from fbclient.status import DataUpdateStatusProviderImpl
14
- from fbclient.status_types import (DATA_INVALID_ERROR, NETWORK_ERROR,
14
+ from fbclient.status_types import (NETWORK_ERROR,
15
15
  REQUEST_INVALID_ERROR, RUNTIME_ERROR,
16
16
  SYSTEM_QUIT, UNKNOWN_CLOSE_CODE,
17
17
  UNKNOWN_ERROR, WEBSOCKET_ERROR, State)
@@ -302,16 +302,21 @@ class Streaming(Thread, UpdateProcessor):
302
302
  log.trace('Streaming WebSocket data: %s' % msg) # type: ignore
303
303
  try:
304
304
  all_data = json.loads(msg)
305
- if not valide_all_data(all_data):
306
- raise ValueError('invalid streaming data')
307
- if not self._on_process_data(all_data['data']) and self.__wsapp:
308
- # state already updated in init or upsert, just reconn
309
- self.__self_closed = _SelfClosed(is_self_close=True, is_reconn=True, state=None)
310
- wsapp.close(status=WS_GOING_AWAY_CLOSE)
311
- except Exception as e:
312
- self.__self_closed = _SelfClosed(is_self_close=True, is_reconn=False,
313
- state=State.error_off_state(DATA_INVALID_ERROR, str(e)))
314
- wsapp.close(status=WS_GOING_AWAY_CLOSE)
305
+ except Exception:
306
+ log.exception('FB Python SDK: could not parse data-sync message; skipping message: %r', msg)
307
+ return
308
+ if isinstance(all_data, dict) and all_data.get('messageType') == 'data-sync':
309
+ try:
310
+ self._handle_data_sync_message(wsapp, all_data)
311
+ except Exception:
312
+ log.exception('FB Python SDK: could not process data-sync message; skipping message: %r', all_data)
313
+
314
+ def _handle_data_sync_message(self, wsapp: websocket.WebSocketApp, all_data):
315
+ if not valide_all_data(all_data):
316
+ log.error('FB Python SDK: invalid data-sync message; skipping message: %r', all_data)
317
+ return
318
+ if not self._on_process_data(all_data['data']):
319
+ log.error('FB Python SDK: could not apply data-sync message; skipping message: %r', all_data)
315
320
 
316
321
  def stop(self):
317
322
  log.info('FB Python SDK: Streaming is stopping...')
@@ -67,7 +67,7 @@ def unix_timestamp_in_milliseconds():
67
67
 
68
68
  def valide_all_data(all_data=None) -> bool:
69
69
  return isinstance(all_data, dict) \
70
- and all_data.get('messageType', 'pong') == 'data-sync' \
70
+ and all_data.get('messageType') == 'data-sync' \
71
71
  and 'data' in all_data and isinstance(all_data['data'], dict) \
72
72
  and all(k in all_data['data'] for k in ('eventType', 'featureFlags', 'segments')) \
73
73
  and any(k == all_data['data']['eventType'] for k in ('full', 'patch')) \
@@ -0,0 +1,3 @@
1
+ {
2
+ "version":"1.2.0"
3
+ }
@@ -0,0 +1,161 @@
1
+ import base64
2
+ import json
3
+ from pathlib import Path
4
+ from threading import Event
5
+ from unittest.mock import Mock
6
+
7
+ import pytest
8
+
9
+ from fbclient.client import FBClient
10
+ from fbclient.common_types import FBUser
11
+ from fbclient.config import Config
12
+ from fbclient.event_processor import DefaultEventProcessor, NullEventProcessor
13
+ from fbclient.event_types import FlagEvent, MetricEvent, UserEvent
14
+ from fbclient.streaming import Streaming
15
+ from fbclient.update_processor import NullUpdateProcessor
16
+
17
+
18
+ MISSING = object()
19
+ USER = {'key': 'test-user-1', 'name': 'Test User', 'country': 'us'}
20
+ FLAG_KEY = 'ff-test-bool'
21
+ SETTINGS = [MISSING, True, False, None, 0, 'false']
22
+
23
+
24
+ def payload(setting, event_type='full', revision=1):
25
+ message = json.loads(Path('tests/fbclient_test_data.json').read_text())
26
+ message['data']['eventType'] = event_type
27
+ for flag in message['data']['featureFlags']:
28
+ flag.pop('insightsEnabled', None)
29
+ if setting is not MISSING:
30
+ flag['insightsEnabled'] = setting
31
+ flag['updatedAt'] = '2030-01-01T00:00:%02dZ' % revision
32
+ return json.dumps(message)
33
+
34
+
35
+ @pytest.fixture
36
+ def client():
37
+ processor = Mock()
38
+ config = Config(base64.b64encode(b'insights-test').decode(),
39
+ event_url='http://fake', streaming_url='http://fake',
40
+ update_processor_imp=NullUpdateProcessor,
41
+ event_processor_imp=lambda *_: processor)
42
+ with FBClient(config) as instance:
43
+ yield instance
44
+
45
+
46
+ def sync(client, message):
47
+ # Exercise the real WebSocket message parser and storage update without
48
+ # starting a network connection or a ping thread.
49
+ streaming = Streaming(Config('unused', 'http://fake', 'http://fake', offline=True), client._broadcaster,
50
+ client._update_status_provider, Event())
51
+ streaming._on_message(None, message)
52
+
53
+
54
+ @pytest.mark.parametrize('setting', SETTINGS)
55
+ @pytest.mark.parametrize('entry', ['variation', 'variation_detail', 'get', 'get_variation'])
56
+ def test_synced_insights_setting_controls_all_evaluation_entries(client, setting, entry):
57
+ sync(client, payload(setting))
58
+ flag = client._get_flag_internal(FLAG_KEY)
59
+ if setting is MISSING:
60
+ assert 'insightsEnabled' not in flag
61
+ else:
62
+ assert flag['insightsEnabled'] == setting
63
+ assert type(flag['insightsEnabled']) is type(setting)
64
+
65
+ if entry in ('get', 'get_variation'):
66
+ states = client.get_all_latest_flag_variations(USER)
67
+ client._event_processor.send_event.assert_not_called()
68
+ result = getattr(states, entry)(FLAG_KEY, False)
69
+ else:
70
+ result = getattr(client, entry)(FLAG_KEY, USER, False)
71
+ assert (result.variation if entry in ('get', 'variation_detail') else result) is True
72
+
73
+ calls = client._event_processor.send_event.call_args_list
74
+ assert len(calls) == (0 if setting is False else 1)
75
+ if calls:
76
+ variation = calls[0][0][0].to_json_dict()['variations'][0]['variation']
77
+ assert variation['id'] == flag['fallthrough']['variations'][0]['id']
78
+ assert variation['value'] == 'true'
79
+
80
+
81
+ @pytest.mark.parametrize('setting', SETTINGS)
82
+ def test_external_json_preserves_insights_setting(setting):
83
+ with FBClient(Config('unused', 'http://fake', 'http://fake', offline=True)) as client:
84
+ assert client.initialize_from_external_json(payload(setting))
85
+ flag = client._get_flag_internal(FLAG_KEY)
86
+ assert flag.get('insightsEnabled', MISSING) == setting
87
+ event = FlagEvent(FBUser.from_dict(USER))
88
+ client._evaluator.evaluate(flag, FBUser.from_dict(USER), event)
89
+ assert bool(event.is_send_event) == (setting is not False)
90
+ # Enabling insights does not replace offline event suppression.
91
+ client.variation(FLAG_KEY, USER, False)
92
+ assert isinstance(client._event_processor, NullEventProcessor)
93
+
94
+
95
+ @pytest.mark.parametrize('key,user', [
96
+ ('ff-test-off', USER),
97
+ ('ff-evaluation-test', {'key': 'test-target-user'}),
98
+ ('ff-evaluation-test', {'key': 'rule-user', 'graduated': 'true'}),
99
+ ('ff-evaluation-test', {'key': 'fallthrough-user'}),
100
+ ])
101
+ def test_insights_does_not_change_evaluation_or_experiment_eligibility(client, key, user):
102
+ sync(client, payload(True))
103
+ flag = client._get_flag_internal(key)
104
+ user = FBUser.from_dict(dict(user, name='Test User'))
105
+ enabled_event = FlagEvent(user)
106
+ before = client._evaluator.evaluate(flag, user, enabled_event)
107
+ flag = dict(flag, insightsEnabled=False)
108
+ disabled_event = FlagEvent(user)
109
+ after = client._evaluator.evaluate(flag, user, disabled_event)
110
+ assert (after.id, after.value, after.reason, after.is_send_to_expt) == (
111
+ before.id, before.value, before.reason, before.is_send_to_expt)
112
+ assert enabled_event.is_send_event
113
+ assert not disabled_event.is_send_event
114
+
115
+
116
+ def test_patch_updates_future_evaluations_but_preserves_snapshot_eligibility(client):
117
+ sync(client, payload(True))
118
+ enabled_snapshot = client.get_all_latest_flag_variations(USER)
119
+ sync(client, payload(False, 'patch', 2))
120
+ assert client._get_flag_internal(FLAG_KEY)['insightsEnabled'] is False
121
+ disabled_snapshot = client.get_all_latest_flag_variations(USER)
122
+ assert client.variation(FLAG_KEY, USER) is True
123
+ client._event_processor.send_event.assert_not_called()
124
+ enabled_snapshot.get(FLAG_KEY)
125
+ assert client._event_processor.send_event.call_count == 1
126
+
127
+ sync(client, payload(True, 'patch', 3))
128
+ assert client._get_flag_internal(FLAG_KEY)['insightsEnabled'] is True
129
+ disabled_snapshot.get_variation(FLAG_KEY)
130
+ assert client._event_processor.send_event.call_count == 1
131
+ assert client.variation(FLAG_KEY, USER) is True
132
+ assert client._event_processor.send_event.call_count == 2
133
+
134
+
135
+ def test_metrics_and_identify_are_unaffected_and_closed_client_stays_silent(client):
136
+ sync(client, payload(False))
137
+ client.track_metric(USER, 'purchase')
138
+ client.identify(USER)
139
+ events = [call[0][0] for call in client._event_processor.send_event.call_args_list]
140
+ assert [type(event) for event in events] == [MetricEvent, UserEvent]
141
+ sync(client, payload(True, 'patch', 2))
142
+ client.stop()
143
+ client.variation(FLAG_KEY, USER)
144
+ assert client._event_processor.send_event.call_count == 2
145
+
146
+
147
+ def test_default_processor_only_delivers_enabled_evaluations(client):
148
+ sender = Mock()
149
+ processor = DefaultEventProcessor(client._config, sender)
150
+ client._event_processor = processor
151
+ sync(client, payload(True))
152
+ client.variation(FLAG_KEY, USER)
153
+ sync(client, payload(False, 'patch', 2))
154
+ client.variation(FLAG_KEY, USER)
155
+ client.get_all_latest_flag_variations(USER).get(FLAG_KEY)
156
+ # Shutdown drains accepted events, including the one collected before disable.
157
+ client.stop()
158
+ delivered = [event for call in sender.postJson.call_args_list
159
+ for event in json.loads(call[0][1])]
160
+ assert len(delivered) == 1
161
+ assert delivered[0]['variations'][0]['featureFlagKey'] == FLAG_KEY
@@ -5,6 +5,8 @@ from time import sleep
5
5
  from concurrent.futures import ThreadPoolExecutor
6
6
  from pathlib import Path
7
7
 
8
+ import pytest
9
+
8
10
  from fbclient.client import FBClient
9
11
  from fbclient.config import Config, HTTPConfig
10
12
  from fbclient.data_storage import InMemoryDataStorage
@@ -470,3 +472,88 @@ def test_streaming_does_not_block_on_another_clients_timeout_lock(monkeypatch):
470
472
  streaming.stop()
471
473
  broadcaster.stop()
472
474
  streaming_module._WEBSOCKET_TIMEOUT_LOCK.release()
475
+
476
+
477
+ class _RecordingWebSocketApp:
478
+ def __init__(self):
479
+ self.close_statuses = []
480
+
481
+ def close(self, status=None):
482
+ self.close_statuses.append(status)
483
+
484
+
485
+ def _idle_streaming():
486
+ broadcaster = NoticeBroadcater()
487
+ streaming = Streaming(Config(FAKE_ENV_SECRET, FAKE_URL, FAKE_URL), broadcaster,
488
+ DataUpdateStatusProviderImpl(InMemoryDataStorage()), threading.Event())
489
+ return streaming, broadcaster
490
+
491
+
492
+ @pytest.mark.parametrize('message', [
493
+ '{"messageType":"pong","data":{}}',
494
+ '{"messageType":"unknown","data":{}}',
495
+ '{"data":{}}',
496
+ '[]',
497
+ 'null',
498
+ ])
499
+ def test_streaming_non_data_sync_message_keeps_connection_open(message):
500
+ streaming, broadcaster = _idle_streaming()
501
+ wsapp = _RecordingWebSocketApp()
502
+ try:
503
+ streaming._on_message(wsapp, message)
504
+ assert wsapp.close_statuses == []
505
+ assert not streaming._Streaming__self_closed()
506
+ finally:
507
+ streaming.stop()
508
+ broadcaster.stop()
509
+
510
+
511
+ @pytest.mark.parametrize('message', [
512
+ 'invalid json',
513
+ '{"messageType":"data-sync","data":{}}',
514
+ '{"messageType":"data-sync","data":null}',
515
+ ])
516
+ def test_streaming_invalid_message_is_logged_and_next_sync_is_processed(message, mocker):
517
+ streaming, broadcaster = _idle_streaming()
518
+ wsapp = _RecordingWebSocketApp()
519
+ process_data = mocker.patch.object(streaming, '_on_process_data', return_value=True)
520
+ error_log = mocker.patch.object(streaming_module.log, 'error')
521
+ exception_log = mocker.patch.object(streaming_module.log, 'exception')
522
+ try:
523
+ streaming._on_message(wsapp, message)
524
+ assert wsapp.close_statuses == []
525
+ assert not streaming._Streaming__self_closed()
526
+ process_data.assert_not_called()
527
+ assert error_log.called or exception_log.called
528
+
529
+ streaming._on_message(wsapp, '{"messageType":"data-sync","data":'
530
+ '{"eventType":"full","featureFlags":[],"segments":[]}}')
531
+ process_data.assert_called_once_with({'eventType': 'full', 'featureFlags': [], 'segments': []})
532
+ assert wsapp.close_statuses == []
533
+ finally:
534
+ streaming.stop()
535
+ broadcaster.stop()
536
+
537
+
538
+ def test_streaming_data_processing_failure_keeps_connection_open(mocker):
539
+ streaming, broadcaster = _idle_streaming()
540
+ wsapp = _RecordingWebSocketApp()
541
+ streaming._Streaming__wsapp = wsapp
542
+ process_data = mocker.patch.object(streaming, '_on_process_data', side_effect=[False, True])
543
+ error_log = mocker.patch.object(streaming_module.log, 'error')
544
+ message = ('{"messageType":"data-sync","data":'
545
+ '{"eventType":"full","featureFlags":[],"segments":[]}}')
546
+ try:
547
+ streaming._on_message(wsapp, message)
548
+ assert wsapp.close_statuses == []
549
+ assert not streaming._Streaming__self_closed()
550
+ error_log.assert_called_once()
551
+
552
+ streaming._on_message(wsapp, message)
553
+ assert process_data.call_count == 2
554
+ assert wsapp.close_statuses == []
555
+ assert not streaming._Streaming__self_closed()
556
+ error_log.assert_called_once()
557
+ finally:
558
+ streaming.stop()
559
+ broadcaster.stop()
@@ -1,3 +0,0 @@
1
- {
2
- "version":"1.1.9"
3
- }
File without changes
File without changes
File without changes
File without changes