@@ -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+
513541def 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+
571624def 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