Skip to content

Commit a269e0f

Browse files
committed
do not reset envid if missing on next event
1 parent 0fb7c36 commit a269e0f

2 files changed

Lines changed: 56 additions & 2 deletions

File tree

ldclient/impl/datasourcev2/streaming.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ def sync(self, ss: SelectorStore) -> Generator[Update, None, None]:
156156
if action.error is None:
157157
continue
158158

159-
envid = action.headers.get(_LD_ENVID_HEADER) if action.headers is not None else None
159+
if action.headers is not None:
160+
envid = action.headers.get(_LD_ENVID_HEADER, envid)
160161

161162
(update, should_continue) = self._handle_error(action.error, envid)
162163
if update is not None:
@@ -168,7 +169,7 @@ def sync(self, ss: SelectorStore) -> Generator[Update, None, None]:
168169

169170
if isinstance(action, Start) and action.headers is not None:
170171
fallback = action.headers.get(_LD_FD_FALLBACK_HEADER) == 'true'
171-
envid = action.headers.get(_LD_ENVID_HEADER)
172+
envid = action.headers.get(_LD_ENVID_HEADER, envid)
172173

173174
if fallback:
174175
self._record_stream_init(True)

ldclient/testing/impl/datasourcev2/test_streaming_synchronizer.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,34 @@ def test_envid_from_start_action(events): # pylint: disable=redefined-outer-nam
510510
assert updates[0].environment_id == 'test-env-123'
511511

512512

513+
def test_envid_not_cleared_from_next_start(events): # pylint: disable=redefined-outer-name
514+
"""Test that environment ID is captured from Start action headers"""
515+
start_action_with_headers = Start(headers={_LD_ENVID_HEADER: 'test-env-123'})
516+
start_action_without_headers = Start()
517+
518+
builder = list_sse_client(
519+
[
520+
start_action_with_headers,
521+
events[EventName.SERVER_INTENT],
522+
events[EventName.PAYLOAD_TRANSFERRED],
523+
start_action_without_headers,
524+
events[EventName.SERVER_INTENT],
525+
events[EventName.PAYLOAD_TRANSFERRED],
526+
]
527+
)
528+
529+
synchronizer = StreamingDataSource(Config(sdk_key="key"))
530+
synchronizer._sse_client_builder = builder
531+
updates = list(synchronizer.sync(MockSelectorStore(Selector.no_selector())))
532+
533+
assert len(updates) == 2
534+
assert updates[0].state == DataSourceState.VALID
535+
assert updates[0].environment_id == 'test-env-123'
536+
537+
assert updates[1].state == DataSourceState.VALID
538+
assert updates[1].environment_id == 'test-env-123'
539+
540+
513541
def test_envid_preserved_across_events(events): # pylint: disable=redefined-outer-name
514542
"""Test that environment ID is preserved across multiple events after being set on Start"""
515543
start_action = Start(headers={_LD_ENVID_HEADER: 'test-env-456'})
@@ -568,6 +596,31 @@ def test_envid_from_fault_action():
568596
assert updates[0].error.status_code == 401
569597

570598

599+
def test_envid_not_cleared_from_next_error():
600+
"""Test that environment ID is captured from Fault action headers"""
601+
error_with_headers_ = HTTPStatusError(408, headers={_LD_ENVID_HEADER: 'test-env-fault'})
602+
error_without_headers_ = HTTPStatusError(401)
603+
fault_action_with_headers = Fault(error=error_with_headers_)
604+
fault_action_without_headers = Fault(error=error_without_headers_)
605+
606+
builder = list_sse_client([fault_action_with_headers, fault_action_without_headers])
607+
608+
synchronizer = StreamingDataSource(Config(sdk_key="key"))
609+
synchronizer._sse_client_builder = builder
610+
updates = list(synchronizer.sync(MockSelectorStore(Selector.no_selector())))
611+
612+
assert len(updates) == 2
613+
assert updates[0].state == DataSourceState.INTERRUPTED
614+
assert updates[0].environment_id == 'test-env-fault'
615+
assert updates[0].error is not None
616+
assert updates[0].error.status_code == 408
617+
618+
assert updates[1].state == DataSourceState.OFF
619+
assert updates[1].environment_id == 'test-env-fault'
620+
assert updates[1].error is not None
621+
assert updates[1].error.status_code == 401
622+
623+
571624
def test_envid_from_fault_with_fallback():
572625
"""Test that environment ID and fallback are captured from Fault action"""
573626
error = HTTPStatusError(503, headers={_LD_ENVID_HEADER: 'test-env-503', _LD_FD_FALLBACK_HEADER: 'true'})

0 commit comments

Comments
 (0)