o
    GXîh™%  ã                   @   sN  d dl mZ d dlmZ d dlmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZ
d dlZe ¡  e de¡ d dlZd dlZW d  ƒ n1 sOw   Y  ejZdZG dd„ dejƒZG d	d
„ d
ejƒZdd„ ZG dd„ de
jƒZG dd„ deƒZG dd„ de
jƒZG dd„ de
jƒZG dd„ de
jƒZedkr¥e
  ¡  dS dS )é    )Úsupport)Úsocket_helper)Úthreading_helperNÚignores   QUIT
c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Úecho_serveré   c                 C   s<   t j | ¡ || _t tjtj¡| _t 	| j¡| _
d | _d S ©N)Ú	threadingÚThreadÚ__init__ÚeventÚsocketÚAF_INETÚSOCK_STREAMÚsockr   Z	bind_portÚportÚstart_resend_event)Úselfr   © r   ú</opt/python-3.10.19/usr/lib/python3.10/test/test_asynchat.pyr      s
   
zecho_server.__init__c                 C   sØ   | j  ¡  | j ¡  | j  ¡ \}}d| _t| jvr,| d¡}|s!n| j| | _t| jvs| j td¡| _| j	r<| j	 
¡  z | jr[| | jd | j… ¡}t d¡ | j|d … | _| js@W n   Y | ¡  | j  ¡  d S )Nó    r   gü©ñÒMbP?)r   Úlistenr   ÚsetÚacceptÚbufferÚSERVER_QUITÚrecvÚreplacer   ÚwaitÚsendÚ
chunk_sizeÚtimeÚsleepÚclose)r   ÚconnÚclientÚdataÚnr   r   r   Úrun'   s0   




ü

ý€zecho_server.runN)Ú__name__Ú
__module__Ú__qualname__r    r   r(   r   r   r   r   r      s    	r   c                   @   s>   e Zd Zdd„ Zdd„ Zejdkrdd„ Zdd	„ Zd
d„ Z	dS )Úecho_clientc                 C   sD   t j | ¡ g | _|  tjtj¡ |  t	|f¡ |  
|¡ d| _d S ©Nr   )ÚasynchatÚ
async_chatr   ÚcontentsZcreate_socketr   r   r   ÚconnectÚHOSTÚset_terminatorr   )r   Ú
terminatorZserver_portr   r   r   r   I   s   

zecho_client.__init__c                 C   ó   d S r   r   ©r   r   r   r   Úhandle_connectQ   ó   zecho_client.handle_connectÚdarwinc                 C   r5   r   r   r6   r   r   r   Úhandle_exptW   r8   zecho_client.handle_exptc                 C   s   |  j |7  _ d S r   )r   )r   r&   r   r   r   Úcollect_incoming_dataZ   s   z!echo_client.collect_incoming_datac                 C   s   | j  | j¡ d| _d S r-   )r0   Úappendr   r6   r   r   r   Úfound_terminator]   s   
zecho_client.found_terminatorN)
r)   r*   r+   r   r7   ÚsysÚplatformr:   r;   r=   r   r   r   r   r,   G   s    
r,   c                  C   s:   t  ¡ } t| ƒ}| ¡  |  ¡  |  ¡  t d¡ || fS )Nç{®Gáz„?)r	   ÚEventr   Ústartr   Úclearr!   r"   )r   Úsr   r   r   Ústart_echo_servera   s   
rE   c                   @   sˆ   e Zd 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d„ Zdd„ Zdd„ Zdd„ Zd S )!ÚTestAsynchatFc                 C   s   t  ¡ | _d S r   )r   Zthreading_setupÚ_threadsr6   r   r   r   ÚsetUpn   ó   zTestAsynchat.setUpc                 C   s   t j| jŽ  d S r   )r   Zthreading_cleanuprG   r6   r   r   r   ÚtearDownq   s   zTestAsynchat.tearDownc                 C   s¦   t  ¡ }t|ƒ}||_| ¡  | ¡  | ¡  t d¡ t	||j
ƒ}| d¡ | d| ¡ | d| ¡ | t¡ tj| jddd t |¡ |  |jddg¡ d S )Nr@   s   hello s   worldó   I'm not dead yet!é,  ©Zuse_pollÚcountÚtimeoutó   hello world)r	   rA   r   r    rB   r   rC   r!   r"   r,   r   Úpushr   ÚasyncoreÚloopÚusepollr   Újoin_threadÚassertEqualr0   )r   ZtermZserver_chunkr   rD   Úcr   r   r   Úline_terminator_checkt   s   



z"TestAsynchat.line_terminator_checkc                 C   ó   dD ]}|   d|¡ qd S )N©r   é   é   ó   
©rX   ©r   Úlr   r   r   Útest_line_terminator1Š   ó   ÿz"TestAsynchat.test_line_terminator1c                 C   rY   )NrZ   s   
r^   r_   r   r   r   Útest_line_terminator2   rb   z"TestAsynchat.test_line_terminator2c                 C   rY   )NrZ   s   qqqr^   r_   r   r   r   Útest_line_terminator3”   rb   z"TestAsynchat.test_line_terminator3c                 C   sf   t ƒ \}}t||jƒ}d}| |¡ | t¡ tj| jddd t 	|¡ |  
|j|d |… g¡ d S ©Ns   hello world, I'm not dead yet!
rL   r@   rM   ©rE   r,   r   rQ   r   rR   rS   rT   r   rU   rV   r0   )r   ZtermlenrD   r   rW   r&   r   r   r   Únumeric_terminator_check™   s   



z%TestAsynchat.numeric_terminator_checkc                 C   ó   |   d¡ d S )Nr   ©rg   r6   r   r   r   Útest_numeric_terminator1¥   s   z%TestAsynchat.test_numeric_terminator1c                 C   rh   )Né   ri   r6   r   r   r   Útest_numeric_terminator2ª   rI   z%TestAsynchat.test_numeric_terminator2c                 C   sj   t ƒ \}}td |jƒ}d}| |¡ | t¡ tj| jddd t 	|¡ |  
|jg ¡ |  
|j|¡ d S re   )rE   r,   r   rQ   r   rR   rS   rT   r   rU   rV   r0   r   ©r   rD   r   rW   r&   r   r   r   Útest_none_terminator­   s   



z!TestAsynchat.test_none_terminatorc                 C   sh   t ƒ \}}td|jƒ}d}tj|t dd}| |¡ tj| j	ddd t
 |¡ |  |jdd	g¡ d S )
Nr]   ó   hello world
I'm not dead yet!
é   )Zbuffer_sizerL   r@   rM   rP   rK   )rE   r,   r   r.   Zsimple_producerr   Úpush_with_producerrR   rS   rT   r   rU   rV   r0   )r   rD   r   rW   r&   Úpr   r   r   Útest_simple_producerº   s   


z!TestAsynchat.test_simple_producerc                 C   sZ   t ƒ \}}td|jƒ}d}| |t ¡ tj| jddd t 	|¡ |  
|jddg¡ d S )Nr]   ro   rL   r@   rM   rP   rK   )rE   r,   r   rq   r   rR   rS   rT   r   rU   rV   r0   rm   r   r   r   Útest_string_producerÅ   s   

z!TestAsynchat.test_string_producerc                 C   s\   t ƒ \}}td|jƒ}| d¡ | t¡ tj| jddd t 	|¡ |  
|jg d¢¡ d S )Nr]   s   hello world

I'm not dead yet!
rL   r@   rM   )rP   r   rK   rf   ©r   rD   r   rW   r   r   r   Útest_empty_lineÏ   s   



ÿzTestAsynchat.test_empty_linec                 C   s†   t ƒ \}}t ¡ |_td|jƒ}| d¡ | t¡ | ¡  t	j
| jddd |j ¡  t |¡ |  |jg ¡ |  t|jƒd¡ d S )Nr]   ro   rL   r@   rM   r   )rE   r	   rA   r   r,   r   rQ   r   Zclose_when_donerR   rS   rT   r   r   rU   rV   r0   ZassertGreaterÚlenr   ru   r   r   r   Útest_close_when_doneÛ   s   





z!TestAsynchat.test_close_when_donec                 C   sœ   t ƒ \}}td|jƒ}d}| |¡ | t|ƒ¡ | t|ƒ¡ |  t|jd¡ |  t|jd¡ | t¡ t	j
| jddd t |¡ |  |jg d¢¡ d S )	Nr]   s   bytes
é
   ÚunicoderL   r@   rM   )ó   bytesr{   r{   )rE   r,   r   rQ   Ú	bytearrayÚ
memoryviewÚassertRaisesÚ	TypeErrorr   rR   rS   rT   r   rU   rV   r0   rm   r   r   r   Ú	test_pushñ   s   



zTestAsynchat.test_pushN)r)   r*   r+   rT   rH   rJ   rX   ra   rc   rd   rg   rj   rl   rn   rs   rt   rv   rx   r€   r   r   r   r   rF   k   s"    
rF   c                   @   s   e Zd ZdZdS )ÚTestAsynchat_WithPollTN)r)   r*   r+   rT   r   r   r   r   r     s    r   c                   @   ó   e Zd Zdd„ ZdS )ÚTestAsynchatMockedc                 C   s~   t j ¡ }ttjƒ|j_t 	¡ }| 
|¡ |  |j¡ t jj |d¡}| ¡  W d   ƒ n1 s2w   Y  |  |j¡ d S )NZhandle_error)ÚunittestZmockZMockÚBlockingIOErrorÚerrnoÚEAGAINr   Zside_effectr.   r/   Z
set_socketZ
addCleanupZdel_channelZpatchÚobjectZhandle_readZassertFalseZcalled)r   r   Ú
dispatcherÚerrorr   r   r   Útest_blockingioerror  s   


ÿz'TestAsynchatMocked.test_blockingioerrorN)r)   r*   r+   r‹   r   r   r   r   rƒ     ó    rƒ   c                   @   r‚   )ÚTestHelperFunctionsc                 C   s,   |   t dd¡d¡ |   t dd¡d¡ d S )Nzqwertyz
r   Z
qwertydkjfr   )rV   r.   Zfind_prefix_at_endr6   r   r   r   Útest_find_prefix_at_end  s   z+TestHelperFunctions.test_find_prefix_at_endN)r)   r*   r+   rŽ   r   r   r   r   r     rŒ   r   c                   @   r‚   )ÚTestNotConnectedc                 C   s   t  ¡ }|  t|jd¡ d S )Néÿÿÿÿ)r.   r/   r~   Ú
ValueErrorr3   )r   r%   r   r   r   Ú!test_disallow_negative_terminator  s   z2TestNotConnected.test_disallow_negative_terminatorN)r)   r*   r+   r’   r   r   r   r   r     rŒ   r   Ú__main__)!Útestr   Ztest.supportr   r   r†   r   r>   r	   r!   r„   Zunittest.mockÚwarningsÚcatch_warningsÚsimplefilterÚDeprecationWarningr.   rR   r2   r   r
   r   r/   r,   rE   ZTestCaserF   r   rƒ   r   r   r)   Úmainr   r   r   r   Ú<module>   s<   

ý.
 ÿ