o
    3iP#                     @   s   d dl Z d dlZd dlZd dlmZ d dlZd dlmZ zd dlmZ W n e	y3   d dlmZ Y nw d dl
mZmZ d dlmZ d dlmZ dd	ed
eeef fddZG dd dejZdS )    N)Any)parameterized)Queue)MAX_MSG_SIZEConsumerAPIError)TEST_API_KEYpython event
event_namereturnc                 C   s   d| ddS )Ntrackdistinct_id)typeeventr    )r   r   r   h/lsinfo/ai/hellotax_ai/llm_service/venv_embed/lib/python3.10/site-packages/posthog/test/test_consumer.py_track_event   s   r   c                	   @   s&  e Zd Zd6ddZd6ddZd6ddZd6d	d
Zd6ddZd6ddZd6ddZ		d7de
dededdfddZede
ddfdedddfdedddfgd ede
deddfd!d"Zd6d#d$Zd6d%d&Zd6d'd(Zd6d)d*Zd6d+d,Zd6d-d.Zd6d/d0Zed1d2gd ed3eddfd4d5ZdS )8TestConsumerr   Nc                 C   s4   t  }t|d}|d | }| |dg d S )N    )r   r   putnextassertEqual)selfqconsumerr   r   r   r   	test_next   s
   

zTestConsumer.test_nextc                 C   sN   t  }d}t|d|}tdD ]}|| q| }| |tt| d S )N2   r   i'  )r   r   ranger   r   r   list)r   r   flush_atr   ir   r   r   r   test_next_limit   s   zTestConsumer.test_next_limitc                 C   sL   t  }t|d}ddt i}|| | }| |g  | |  d S )Nr   mx)r   r   r   r   r   r   
assertTrueempty)r   r   r   oversize_msgr   r   r   r   test_dropping_oversize_msg(   s   

z'TestConsumer.test_dropping_oversize_msgc                 C   s2   t  }t|t}|t  | }| | d S N)r   r   r	   r   r   uploadr&   )r   r   r   successr   r   r   test_upload1   s
   
zTestConsumer.test_uploadc                 C   s   t  }d}t|td|d}td+}|  tdD ]}|td|  t	
|d  q| |jd W d    d S 1 s@w   Y  d S )Ng333333?
   r!   flush_intervalposthog.consumer.batch_post   python event %d皙?r   r   r	   mockpatchstartr   r   r   timesleepr   
call_count)r   r   r0   r   	mock_postr"   r   r   r   test_flush_interval8   s   "z TestConsumer.test_flush_intervalc                 C   s   t  }d}d}t|t||d}td-}|  t|d D ]}|td|  qt	
|d  | |jd W d    d S 1 sDw   Y  d S )Ng      ?r.   r/   r1      r3   r4   r5   )r   r   r0   r!   r   r<   r"   r   r   r   "test_multiple_uploads_per_intervalF   s   "z/TestConsumer.test_multiple_uploads_per_intervalc                 C   s   t d t}|t g d S r*   )r   r	   requestr   r   r   r   r   r   test_requestV   s   
zTestConsumer.test_requestr.   	exceptionexception_countretriesc              	      s   dg dt dt dd f fdd}td t|d}tdtj|d	C |kr0|t g n&| t	 |t g W d    n1 sIw   Y  W d    d S W d    d S W d    d S 1 siw   Y  d S )
Nr   argskwargsr   c                     s$    d  d7  <  d krd S Nr   r   r   rF   rG   r;   rC   rD   r   r   r<   _      z/TestConsumer._run_retry_test.<locals>.mock_postrE   r1   side_effect)
r   r   r	   r6   r7   Mockr@   r   assertRaisesr   )r   rC   rD   rE   r<   r   r   rJ   r   _run_retry_testZ   s    "zTestConsumer._run_retry_testgeneral_errorszgeneric exceptionr>   server_errors  Internal Server Errorrate_limit_errors  Too Many Requests_namec                 C   s   |  || d S r*   )rQ   )r   rY   rC   rD   r   r   r   (test_request_retries_on_retriable_errorsn   s   
z5TestConsumer.test_request_retries_on_retriable_errorsc                 C   sB   |  t | tddd W d    d S 1 sw   Y  d S )Ni  zClient Errorsr   )rP   r   rQ   r   r   r   r   )test_request_does_not_retry_client_errorsz   s   "z6TestConsumer.test_request_does_not_retry_client_errorsc                 C   s   | j tddddd d S )NrT   rU      r2   rL   )rQ   r   r[   r   r   r   1test_request_fails_when_exceptions_exceed_retries~   s   z>TestConsumer.test_request_fails_when_exceptions_exceed_retriesc                 C   s"   t d t}|  | |j d S r*   )r   r	   pauseassertFalserunningrA   r   r   r   
test_pause   s   
zTestConsumer.test_pausec                    s   t  }t|tddd}i }tddD ]}d|t|< qddd	|d
}tt| }t	d| }dtdtdt
dtjf fdd}tjd|d'}	|  td|d D ]}
|| qT|   |	jd W d    d S 1 srw   Y  d S )Ni r2   r/   r   rT   ,one_long_property_value_to_build_a_big_eventr   r
   r   )r   r   r   
propertiesi z _datarG   r   c                    s2   t  }d|_t| } |dk d|  |S )N   g      VAz!batch size (%d) higher than limit)r6   rO   status_codelenencoder&   )re   rf   rG   resrequest_sizer[   r   r   mock_post_fn   s   z6TestConsumer.test_max_batch_size.<locals>.mock_post_fnzposthog.request._session.postrM   r>   )r   r   r	   r   strri   jsondumpsrj   intr   r6   rO   r7   r8   r   joinr   r;   )r   r   r   rd   nr   msg_sizen_msgsrm   r<   re   r   r[   r   test_max_batch_size   s.    "z TestConsumer.test_max_batch_sizec              	      s   t dddddg dtdtdd f fd	d
}td tdd}tjd|d2 td}|t g |d W d    n1 sCw   Y  W d    d S W d    d S 1 s[w   Y  d S )NrW   rX   g      @)retry_afterr   rF   rG   r   c                     s$    d  d7  <  d dkrd S rH   r   rI   r;   errorr   r   r<      rK   zDTestConsumer.test_request_sleeps_with_retry_after.<locals>.mock_postr2   rL   r1   rM   posthog.consumer.time.sleep)	r   r   r   r	   r6   r7   r@   r   assert_called_once_withr   r<   r   
mock_sleepr   rx   r   $test_request_sleeps_with_retry_after   s   Pz1TestConsumer.test_request_sleeps_with_retry_afterc              
      s   t dddg dtdtdd f fdd}td td	d
}tjd|d@ td"}|t g | |j	t
dt
dt
dg W d    n1 sOw   Y  W d    d S W d    d S 1 sgw   Y  d S )Ni  zService Unavailabler   rF   rG   r   c                     s$    d  d7  <  d dkrd S )Nr   r   r2   r   rI   rx   r   r   r<      rK   zYTestConsumer.test_request_uses_exponential_backoff_without_retry_after.<locals>.mock_postr2   rL   r1   rM   rz   r   r>   r]   )r   r   r   r	   r6   r7   r@   r   r   call_args_listcallr|   r   rx   r   9test_request_uses_exponential_backoff_without_retry_after   s$   
PzFTestConsumer.test_request_uses_exponential_backoff_without_retry_afterc              	      s   dg dt dt dd f fdd}td tdd}tjd	|d
5 td |t g |  d d W d    n1 s>w   Y  W d    d S W d    d S 1 sVw   Y  d S )Nr   rF   rG   r   c                     s*    d  d7  <  d dkrt ddd S )Nr   r   i  zRequest Timeoutr   rI   r;   r   r   r<      s   
z;TestConsumer.test_request_retries_on_408.<locals>.mock_postr2   rL   r1   rM   rz   r>   )r   r   r	   r6   r7   r@   r   r   )r   r<   r   r   r   r   test_request_retries_on_408   s   Pz(TestConsumer.test_request_retries_on_408)on_error_succeedsF)on_error_raisesTr   c                    s   g  dt dttttf  dd f fdd}t }t|t|d}t }|| t	j
j|dt dd	 | }W d    n1 sBw   Y  | | | t d
 | t d d d |  d d
 |g d S )Nebatchr   c                    s     | |f rtdd S )Nzon_error failed)append	Exception)r   r   on_error_calledr   r   r   on_error   s   zVTestConsumer.test_upload_exception_calls_on_error_and_does_not_raise.<locals>.on_error)r   r@   zrequest failedrM   r   r   )r   r    dictrn   r   r   r	   r   r   r6   r7   objectr+   r`   r   ri   )r   rY   r   r   r   r   r   resultr   r   r   7test_upload_exception_calls_on_error_and_does_not_raise   s   	(



zDTestConsumer.test_upload_exception_calls_on_error_and_does_not_raise)r   N)r.   )__name__
__module____qualname__r   r#   r)   r-   r=   r?   rB   r   rq   rQ   r   expandr   rn   rZ   r\   r^   rb   rv   r~   r   r   boolr   r   r   r   r   r      sh    


	
	








$

r   )r
   )ro   r9   unittesttypingr   r6   r   queuer   ImportErrorposthog.consumerr   r   posthog.requestr   posthog.test.test_utilsr	   rn   r   r   TestCaser   r   r   r   r   <module>   s     