1313
1414from posthog import AsyncClient , AsyncPosthog , CaptureCompression
1515from posthog .capture_send import _CAPTURE_AI_V1_PATH , _CAPTURE_V1_PATH
16- from posthog .consumer import AI_MAX_MSG_SIZE , MAX_MSG_SIZE
16+ from posthog .consumer import AI_MAX_MSG_SIZE , AI_MAX_PROPERTIES_SIZE , MAX_MSG_SIZE
1717from posthog .contexts import (
1818 new_context ,
1919 set_capture_exception_code_variables_context ,
@@ -123,7 +123,10 @@ async def send_batch(api_key, host, batch, **kwargs):
123123
124124
125125@pytest .mark .asyncio
126- async def test_cross_thread_capture_rechecks_shutdown_before_queue_admission ():
126+ @pytest .mark .parametrize ("method_name" , ["capture" , "capture_ai" ])
127+ async def test_cross_thread_capture_rechecks_shutdown_before_queue_admission (
128+ method_name ,
129+ ):
127130 client = AsyncPosthog ("test-key" , flush_interval = 30 )
128131 client ._ensure_workers_started (client ._analytics_lane )
129132 scheduled_callbacks = []
@@ -136,7 +139,7 @@ async def test_cross_thread_capture_rechecks_shutdown_before_queue_admission():
136139 ):
137140 capture_thread = threading .Thread (
138141 target = lambda : capture_result .append (
139- client . capture ("threaded event" , distinct_id = "user-1" )
142+ getattr ( client , method_name ) ("threaded event" , distinct_id = "user-1" )
140143 ),
141144 daemon = True ,
142145 )
@@ -149,9 +152,12 @@ async def test_cross_thread_capture_rechecks_shutdown_before_queue_admission():
149152 capture_thread .join (timeout = 1 )
150153
151154 assert capture_result == [None ]
152- for task in client ._analytics_lane .worker_tasks :
155+ assert client ._ai_lane .worker_tasks == []
156+ assert client ._pending_queue_items () == 0
157+ worker_tasks = client ._all_worker_tasks ()
158+ for task in worker_tasks :
153159 task .cancel ()
154- await asyncio .gather (* client . _analytics_lane . worker_tasks , return_exceptions = True )
160+ await asyncio .gather (* worker_tasks , return_exceptions = True )
155161 await client ._close_transport ()
156162
157163
@@ -288,7 +294,8 @@ async def send_batch(api_key, host, batch, **kwargs):
288294 ("method_name" , "client_kwargs" , "payload_size" , "sent" ),
289295 [
290296 ("capture_immediate" , {}, MAX_MSG_SIZE , False ),
291- ("capture_ai_immediate" , {}, MAX_MSG_SIZE , True ),
297+ ("capture_ai_immediate" , {}, AI_MAX_PROPERTIES_SIZE , True ),
298+ ("capture_ai_immediate" , {}, AI_MAX_MSG_SIZE , False ),
292299 ("capture_ai_immediate" , {"capture_ai_max_event_bytes" : 1024 }, 2048 , False ),
293300 ],
294301)
@@ -359,7 +366,7 @@ async def send_batch(api_key, host, batch, **kwargs):
359366 client .capture_ai (
360367 "$ai_generation" ,
361368 distinct_id = "user-1" ,
362- properties = {"$ai_input" : "x" * MAX_MSG_SIZE },
369+ properties = {"$ai_input" : "x" * AI_MAX_PROPERTIES_SIZE },
363370 )
364371 await client .flush (timeout_seconds = 1 )
365372 assert sent == {
@@ -800,23 +807,31 @@ async def on_error(error, batch):
800807
801808
802809@pytest .mark .asyncio
803- @pytest .mark .parametrize ("immediate" , [False , True ])
804- async def test_failed_capture_does_not_log_server_response_detail (caplog , immediate ):
810+ @pytest .mark .parametrize (
811+ ("method_name" , "path" ),
812+ [
813+ ("capture" , _CAPTURE_V1_PATH ),
814+ ("capture_immediate" , _CAPTURE_V1_PATH ),
815+ ("capture_ai" , _CAPTURE_AI_V1_PATH ),
816+ ("capture_ai_immediate" , _CAPTURE_AI_V1_PATH ),
817+ ],
818+ )
819+ async def test_failed_capture_logs_one_loss_line_without_response_detail (
820+ caplog , method_name , path
821+ ):
805822 caplog .set_level (logging .DEBUG , logger = "posthog" )
806823 server_error = APIError (400 , "password=server-secret" )
807824
808825 with patch_async_capture_send (side_effect = server_error ):
809826 client = AsyncPosthog ("test-key" , flush_at = 1 , max_retries = 0 )
810- if immediate :
811- await client .capture_immediate ("event" , distinct_id = "user-1" )
812- else :
813- client .capture ("event" , distinct_id = "user-1" )
814- await client .flush (timeout_seconds = 1 )
827+ result = getattr (client , method_name )("$ai_generation" , distinct_id = "user-1" )
828+ if method_name .endswith ("_immediate" ):
829+ await result
830+ await client .flush (timeout_seconds = 1 )
815831 await client .shutdown ()
816832
817833 assert "server-secret" not in caplog .text
818- assert "APIError" in caplog .text
819- assert "status=400" in caplog .text
834+ assert f"1 event(s) not persisted by { path } : APIError (status=400)" in caplog .text
820835
821836
822837@pytest .mark .asyncio
0 commit comments