o
    3Ih                     @   s   d dl Z d dlZd dlZd dlZzd dlmZ W n ey'   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 G dd dejZdS )    N)Queue)MAX_MSG_SIZEConsumer)APIError)TEST_API_KEYc                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )TestConsumerc                 C   s4   t  }t|d}|d | }| |dg d S )N    )r   r   putnextassertEqual)selfqconsumerr    r   Z/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/posthog/test/test_consumer.py	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   s<   t  }t|t}dddd}|| | }| | d S Ntrackpython eventdistinct_idtypeeventr"   )r   r   r   r
   uploadr   )r   r   r   r    successr   r   r   test_upload+   s   

zTestConsumer.test_uploadc                 C   s   t  }d}t|td|d}td0}|  tddD ]}dd| d	d
}|| t	|d  q| 
|jd W d    d S 1 sEw   Y  d S )Ng333333?
   r   flush_intervalposthog.consumer.batch_postr      r    python event %dr"   r#   皙?r   r   r   mockpatchstartr   r
   timesleepr   
call_count)r   r   r+   r   	mock_postr   r    r   r   r   test_flush_interval3   s   
"z TestConsumer.test_flush_intervalc                 C   s   t  }d}d}t|t||d}td2}|  td|d D ]}dd| d	d
}|| q t	|d  | 
|jd W d    d S 1 sIw   Y  d S )Ng      ?r)   r*   r,   r      r    r.   r"   r#   r/   r0   )r   r   r+   r   r   r7   r   r    r   r   r   "test_multiple_uploads_per_intervalF   s"   "z/TestConsumer.test_multiple_uploads_per_intervalc                 C   s&   t d t}dddd}||g d S r   )r   r   request)r   r   r    r   r   r   test_request[   s   
zTestConsumer.test_requestc                    s    fddd_ tdtjdV dddd	} |jkr(||g n3z||g W n tyJ } z| | W Y d }~nd }~ww | d
|j  W d    d S W d    d S W d    d S 1 snw   Y  d S )Nc                     s     j d7  _ j  krd S )Nr	   )r6   )argskwargsexception_countexpected_exceptionr7   r   r   r7   a   s   
z3TestConsumer._test_request_retry.<locals>.mock_postr   r,   side_effectr    r!   r"   r#   zErequest() should raise an exception if still failing after %d retries)	r6   r1   r2   Mockretriesr;   r$   r   fail)r   r   rA   r@   r    excr   r?   r   _test_request_retry`   s6   
"z TestConsumer._test_request_retryc                 C   s   t d t}| |tdd t d t}| |tddd t d t}| |tddd t d t}tdd}z	| ||d	 W n	 tyH   Y nw | d
 t d tdd}| |tddd d S )Nzgeneric exceptionr9     zInternal Server Errori  zToo Many Requestsi  zClient Errorsr	   z+request() should not retry on client errorsr-   )rE   )r   r   rH   	Exceptionr   rF   )r   r   	api_errorr   r   r   test_request_retry   s    





zTestConsumer.test_request_retryc                 C   s"   t d t}|  | |j d S )N)r   r   pauseassertFalserunning)r   r   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| } fdd}t
jd|d'}	|  td|d D ]}
|| qJ|   |	jd W d    d S 1 shw   Y  d S )Ni r-   r*   r   rI   ,one_long_property_value_to_build_a_big_eventr    r!   r"   )r$   r%   r"   
propertiesi z c                    s2   t  }d|_t| } |dk d|  |S )N   g      VAz!batch size (%d) higher than limit)r1   rD   status_codelenencoder   )_datar>   resrequest_sizer   r   r   mock_post_fn   s   z6TestConsumer.test_max_batch_size.<locals>.mock_post_fnzposthog.request._session.postrB   r9   )r   r   r   r   strrU   jsondumpsrV   intr1   r2   r3   r
   joinr   r6   )r   r   r   rR   nr    msg_sizen_msgsr\   r7   rW   r   r[   r   test_max_batch_size   s.   "z TestConsumer.test_max_batch_sizeN)__name__
__module____qualname__r   r   r   r(   r8   r:   r<   rH   rL   rP   re   r   r   r   r   r      s    		"r   )r^   r4   unittestr1   queuer   ImportErrorposthog.consumerr   r   posthog.requestr   posthog.test.test_utilsr   TestCaser   r   r   r   r   <module>   s    