B
    u9a<%  ã               @   s  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
d dlZd dlZejZd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 )é    )ÚsupportNs   QUIT
g      @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   ú#/usr/lib/python3.7/test_asynchat.pyr      s
    zecho_server.__init__c             C   sØ   | j  ¡  | j ¡  | j  ¡ \}}d| _x*t| jkrR| d¡}|sDP | j| | _q*W | j td¡| _| j	rt| j	 
¡  y@x:| jr°| | jd | j… ¡}t d¡ | j|d … | _qxW W n   Y nX | ¡  | j  ¡  d S )Nó    r   gü©ñÒMbP?)r   Zlistenr   ÚsetZacceptÚbufferÚSERVER_QUITÚrecvÚreplacer   ÚwaitÚsendÚ
chunk_sizeÚtimeÚsleepÚclose)r   ZconnÚclientÚdataÚnr   r   r   Úrun#   s*    




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dd„ ZdS )Úecho_clientc             C   s^   t j | ¡ g | _|  tjtj¡ |  t	|f¡ |  
|¡ d| _dd„ }tjdkrZdd„ }d S )Nr   c             S   s   d S )Nr   )r   r   r   r   Úhandle_connectM   s    z,echo_client.__init__.<locals>.handle_connectÚdarwinc             S   s   d S )Nr   )r   r   r   r   Úhandle_exptS   s    z)echo_client.__init__.<locals>.handle_expt)ÚasynchatÚ
async_chatr   ÚcontentsZcreate_socketr	   r
   r   ZconnectÚHOSTÚset_terminatorr   ÚsysÚplatform)r   Ú
terminatorZserver_portr&   r(   r   r   r   r   E   s    

zecho_client.__init__c             C   s   |  j |7  _ d S )N)r   )r   r   r   r   r   Úcollect_incoming_dataV   s    z!echo_client.collect_incoming_datac             C   s   | j  | j¡ d| _d S )Nr   )r+   Úappendr   )r   r   r   r   Úfound_terminatorY   s    zecho_client.found_terminatorN)r"   r#   r$   r   r1   r3   r   r   r   r   r%   C   s   r%   c              C   s:   t  ¡ } t| ƒ}| ¡  |  ¡  |  ¡  t d¡ || fS )Ng{®Gáz„?)r   ÚEventr   Ústartr   Úclearr   r   )r   Úsr   r   r   Ústart_echo_server]   s    
r8   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 )N)r   Zthreading_setupÚ_threads)r   r   r   r   ÚsetUpj   s    zTestAsynchat.setUpc             C   s   t j| jŽ  d S )N)r   Zthreading_cleanupr:   )r   r   r   r   ÚtearDownm   s    zTestAsynchat.tearDownc             C   sª   t  ¡ }t|ƒ}||_| ¡  | ¡  | ¡  t d¡ t	||j
ƒ}| d¡ | d| ¡ | d| ¡ | t¡ tj| jddd tj|td |  |jddg¡ d S )	Ng{®Gáz„?s   hello s   worlds   I'm not dead yet!i,  )Úuse_pollÚcountÚtimeout)r?   s   hello world)r   r4   r   r   r5   r   r6   r   r   r%   r   Úpushr   ÚasyncoreÚloopÚusepollr   Újoin_threadÚTIMEOUTÚassertEqualr+   )r   ZtermZserver_chunkr   r7   Úcr   r   r   Úline_terminator_checkp   s    


z"TestAsynchat.line_terminator_checkc             C   s   xdD ]}|   d|¡ qW d S )N)r   é   é   ó   
)rH   )r   Úlr   r   r   Útest_line_terminator1†   s    
z"TestAsynchat.test_line_terminator1c             C   s   xdD ]}|   d|¡ qW d S )N)r   rI   rJ   s   
)rH   )r   rL   r   r   r   Útest_line_terminator2‹   s    
z"TestAsynchat.test_line_terminator2c             C   s   xdD ]}|   d|¡ qW d S )N)r   rI   rJ   s   qqq)rH   )r   rL   r   r   r   Útest_line_terminator3   s    
z"TestAsynchat.test_line_terminator3c             C   sj   t ƒ \}}t||jƒ}d}| |¡ | t¡ tj| jddd tj	|t
d |  |j|d |… g¡ d S )Ns   hello world, I'm not dead yet!
i,  g{®Gáz„?)r=   r>   r?   )r?   )r8   r%   r   r@   r   rA   rB   rC   r   rD   rE   rF   r+   )r   Ztermlenr7   r   rG   r   r   r   r   Únumeric_terminator_check•   s    


z%TestAsynchat.numeric_terminator_checkc             C   s   |   d¡ d S )Nr   )rP   )r   r   r   r   Útest_numeric_terminator1¡   s    z%TestAsynchat.test_numeric_terminator1c             C   s   |   d¡ d S )Né   )rP   )r   r   r   r   Útest_numeric_terminator2¦   s    z%TestAsynchat.test_numeric_terminator2c             C   sn   t ƒ \}}td |jƒ}d}| |¡ | t¡ tj| jddd tj	|t
d |  |jg ¡ |  |j|¡ d S )Ns   hello world, I'm not dead yet!
i,  g{®Gáz„?)r=   r>   r?   )r?   )r8   r%   r   r@   r   rA   rB   rC   r   rD   rE   rF   r+   r   )r   r7   r   rG   r   r   r   r   Útest_none_terminator©   s    


z!TestAsynchat.test_none_terminatorc             C   sl   t ƒ \}}td|jƒ}d}tj|t dd}| |¡ tj| j	ddd t
j|td |  |jd	d
g¡ d S )NrK   s   hello world
I'm not dead yet!
é   )Úbuffer_sizei,  g{®Gáz„?)r=   r>   r?   )r?   s   hello worlds   I'm not dead yet!)r8   r%   r   r)   Zsimple_producerr   Úpush_with_producerrA   rB   rC   r   rD   rE   rF   r+   )r   r7   r   rG   r   Úpr   r   r   Útest_simple_producer¶   s    

z!TestAsynchat.test_simple_producerc             C   s^   t ƒ \}}td|jƒ}d}| |t ¡ tj| jddd tj	|t
d |  |jddg¡ d S )	NrK   s   hello world
I'm not dead yet!
i,  g{®Gáz„?)r=   r>   r?   )r?   s   hello worlds   I'm not dead yet!)r8   r%   r   rW   r   rA   rB   rC   r   rD   rE   rF   r+   )r   r7   r   rG   r   r   r   r   Útest_string_producerÁ   s    
z!TestAsynchat.test_string_producerc             C   sb   t ƒ \}}td|jƒ}| d¡ | t¡ tj| jddd tj	|t
d |  |jddd	g¡ d S )
NrK   s   hello world

I'm not dead yet!
i,  g{®Gáz„?)r=   r>   r?   )r?   s   hello worldr   s   I'm not dead yet!)r8   r%   r   r@   r   rA   rB   rC   r   rD   rE   rF   r+   )r   r7   r   rG   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|td |  |jg ¡ |  t|jƒd¡ d S )NrK   s   hello world
I'm not dead yet!
i,  g{®Gáz„?)r=   r>   r?   )r?   r   )r8   r   r4   r   r%   r   r@   r   Zclose_when_donerA   rB   rC   r   r   rD   rE   rF   r+   ZassertGreaterÚlenr   )r   r7   r   rG   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|td |  |jd	d	d	g¡ d S )
NrK   s   bytes
é
   Zunicodei,  g{®Gáz„?)r=   r>   r?   )r?   s   bytes)r8   r%   r   r@   Ú	bytearrayÚ
memoryviewÚassertRaisesÚ	TypeErrorr   rA   rB   rC   r   rD   rE   rF   r+   )r   r7   r   rG   r   r   r   r   Ú	test_pushí   s    


zTestAsynchat.test_pushN)r"   r#   r$   rC   r;   r<   rH   rM   rN   rO   rP   rQ   rS   rT   rY   rZ   r[   r]   rc   r   r   r   r   r9   g   s    
r9   c               @   s   e Zd ZdZdS )ÚTestAsynchat_WithPollTN)r"   r#   r$   rC   r   r   r   r   rd   þ   s   rd   c               @   s   e Zd Zdd„ ZdS )ÚTestAsynchatMockedc          	   C   sj   t j ¡ }ttjƒ|j_t 	¡ }| 
|¡ |  |j¡ t jj |d¡}| ¡  W d Q R X |  |j¡ d S )NZhandle_error)ÚunittestZmockZMockÚBlockingIOErrorÚerrnoZEAGAINr   Zside_effectr)   r*   Z
set_socketZ
addCleanupZdel_channelZpatchÚobjectZhandle_readZassertFalseZcalled)r   r   Z
dispatcherÚerrorr   r   r   Útest_blockingioerror  s    

z'TestAsynchatMocked.test_blockingioerrorN)r"   r#   r$   rk   r   r   r   r   re     s   re   c               @   s   e Zd Zdd„ ZdS )ÚTestHelperFunctionsc             C   s,   |   t dd¡d¡ |   t dd¡d¡ d S )Nzqwertyz
r   Z
qwertydkjfr   )rF   r)   Zfind_prefix_at_end)r   r   r   r   Útest_find_prefix_at_end  s    z+TestHelperFunctions.test_find_prefix_at_endN)r"   r#   r$   rm   r   r   r   r   rl     s   rl   c               @   s   e Zd Zdd„ ZdS )ÚTestNotConnectedc             C   s   t  ¡ }|  t|jd¡ d S )Néÿÿÿÿ)r)   r*   ra   Ú
ValueErrorr-   )r   r   r   r   r   Ú!test_disallow_negative_terminator  s    z2TestNotConnected.test_disallow_negative_terminatorN)r"   r#   r$   rq   r   r   r   r   rn     s   rn   Ú__main__)Ztestr   r)   rA   rh   r	   r.   Ú_threadZthreadr   r   rf   Zunittest.mockr,   r   rE   r   r   r*   r%   r8   ZTestCaser9   rd   re   rl   rn   r"   Úmainr   r   r   r   Ú<module>   s0   .
 