a
    åze ‘  ã                   @   sÂ   d 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m	Z	 ddl
mZ zddlZW n ey~   dZY n0 ddlZddlmZ dd„ ZG dd	„ d	ejƒZed
kr¾e ¡  dS )zTests for streams.py.é    N)Úmock)Úsocket_helper)Úutilsc                   C   s   t  d ¡ d S ©N)ÚasyncioZset_event_loop_policy© r   r   ú4/usr/lib/python3.9/test/test_asyncio/test_streams.pyÚtearDownModule   s    r	   c                       sp  e Zd ZdZ‡ fdd„Z‡ fdd„Ze d¡dd„ ƒZd	d
„ Z	dd„ Z
ejdd„ ƒZdd„ Ze edu d¡dd„ ƒZeje edu d¡dd„ ƒƒZdd„ Zdd„ Zej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/d0„ Z d1d2„ Z!d3d4„ Z"d5d6„ Z#d7d8„ Z$d9d:„ Z%d;d<„ Z&d=d>„ Z'd?d@„ Z(dAdB„ Z)dCdD„ Z*dEdF„ Z+dGdH„ Z,dIdJ„ Z-dKdL„ Z.dMdN„ Z/dOdP„ Z0dQdR„ Z1dSdT„ Z2dUdV„ Z3dWdX„ Z4dYdZ„ Z5d[d\„ Z6d]d^„ Z7ejd_d`„ ƒZ8e e9j:dakdb¡dcdd„ ƒZ;dedf„ Z<dgdh„ Z=didj„ Z>dkdl„ Z?dmdn„ Z@dodp„ ZAdqdr„ ZBdsdt„ ZCdudv„ ZDdwdx„ ZEdydz„ ZFd{d|„ ZGd}d~„ ZHdd€„ ZIdd‚„ ZJdƒd„„ ZKd…d†„ ZL‡  ZMS )‡ÚStreamTestss   line1
line2
line3
c                    s$   t ƒ  ¡  t ¡ | _|  | j¡ d S r   )ÚsuperÚsetUpr   Znew_event_loopÚloopÚset_event_loop©Úself©Ú	__class__r   r   r      s    

zStreamTests.setUpc                    s,   t  | j¡ | j ¡  t ¡  tƒ  ¡  d S r   )Ú
test_utilsÚrun_brieflyr   ÚcloseÚgcZcollectr   ÚtearDownr   r   r   r   r   #   s    
zStreamTests.tearDownzasyncio.streams.eventsc                 C   s   t  ¡ }|  |j|jj¡ d S r   )r   ÚStreamReaderÚassertIsÚ_loopZget_event_loopÚreturn_value)r   Zm_eventsÚstreamr   r   r   Útest_ctor_global_loop+   s    z!StreamTests.test_ctor_global_loopc                    s¸   g ‰ | j  ‡ fdd„¡ |  t¡  | j  |¡\}}W d   ƒ n1 sH0    Y  | d¡ | ¡ }| j  |¡}|  |d¡ | ¡ }| j  |¡}|  	| 
d¡¡ | ¡  |  ˆ g ¡ d S )Nc                    s
   ˆ   |¡S r   ©Úappend©r   Úctx©Úmessagesr   r   Ú<lambda>2   ó    z7StreamTests._basetest_open_connection.<locals>.<lambda>ó   GET / HTTP/1.0

ó   HTTP/1.0 200 OK
ó   

Test message)r   Úset_exception_handlerÚassertWarnsÚDeprecationWarningÚrun_until_completeÚwriteÚreadlineÚassertEqualÚreadÚ
assertTrueÚendswithr   ©r   Úopen_connection_futÚreaderÚwriterÚfÚdatar   r"   r   Ú_basetest_open_connection0   s    .
z%StreamTests._basetest_open_connectionc                 C   sJ   t  ¡ .}tj|jd| jiŽ}|  |¡ W d   ƒ n1 s<0    Y  d S ©Nr   )r   Úrun_test_serverr   Úopen_connectionÚaddressr   r9   ©r   ÚhttpdZconn_futr   r   r   Útest_open_connection?   s
    

ÿz StreamTests.test_open_connectionc                 C   sH   t  ¡ ,}tj|j| jd}|  |¡ W d   ƒ n1 s:0    Y  d S ©N©r   )r   Úrun_test_unix_serverr   Úopen_unix_connectionr=   r   r9   r>   r   r   r   Útest_open_unix_connectionE   s
    
ÿz%StreamTests.test_open_unix_connectionc              	      s´   g ‰ | j  ‡ fdd„¡ zH|  t¡  | j  |¡\}}W d   ƒ n1 sJ0    Y  W t d ¡ nt d ¡ 0 | d¡ | ¡ }| j  |¡}|  	| 
d¡¡ | ¡  |  ˆ g ¡ d S )Nc                    s
   ˆ   |¡S r   r   r    r"   r   r   r$   N   r%   zCStreamTests._basetest_open_connection_no_loop_ssl.<locals>.<lambda>r&   r(   )r   r)   r*   r+   r,   r   r   r-   r0   r1   r2   r   r/   r3   r   r"   r   Ú%_basetest_open_connection_no_loop_sslL   s    0
z1StreamTests._basetest_open_connection_no_loop_sslNzNo ssl modulec                 C   sT   t jdd4}tj|jt  ¡ | jdœŽ}|  |¡ W d   ƒ n1 sF0    Y  d S )NT©Zuse_ssl)Ússlr   )r   r;   r   r<   r=   Údummy_ssl_contextr   rF   r>   r   r   r   Ú test_open_connection_no_loop_ssl\   s    ýz,StreamTests.test_open_connection_no_loop_sslc                 C   sT   t jdd4}tj|jt  ¡ d| jd}|  |¡ W d   ƒ n1 sF0    Y  d S )NTrG   Ú )rH   Zserver_hostnamer   )r   rC   r   rD   r=   rI   r   rF   r>   r   r   r   Ú%test_open_unix_connection_no_loop_sslf   s    üz1StreamTests.test_open_unix_connection_no_loop_sslc                    sÂ   g ‰ | j  ‡ fdd„¡ |  t¡  | j  |¡\}}W d   ƒ n1 sH0    Y  |j tƒ ¡ | ¡ }|  	t¡ | j  |¡ W d   ƒ n1 s”0    Y  | 
¡  t | j ¡ |  ˆ g ¡ d S )Nc                    s
   ˆ   |¡S r   r   r    r"   r   r   r$   t   r%   z=StreamTests._basetest_open_connection_error.<locals>.<lambda>)r   r)   r*   r+   r,   Z	_protocolZconnection_lostÚZeroDivisionErrorr0   ÚassertRaisesr   r   r   r/   )r   r4   r5   r6   r7   r   r"   r   Ú_basetest_open_connection_errorr   s    .*z+StreamTests._basetest_open_connection_errorc                 C   sJ   t  ¡ .}tj|jd| jiŽ}|  |¡ W d   ƒ n1 s<0    Y  d S r:   )r   r;   r   r<   r=   r   rO   r>   r   r   r   Útest_open_connection_error   s
    

ÿz&StreamTests.test_open_connection_errorc                 C   sH   t  ¡ ,}tj|j| jd}|  |¡ W d   ƒ n1 s:0    Y  d S rA   )r   rC   r   rD   r=   r   rO   r>   r   r   r   Útest_open_unix_connection_error…   s
    
ÿz+StreamTests.test_open_unix_connection_errorc                 C   s*   t j| jd}| d¡ |  d|j¡ d S ©NrB   r%   )r   r   r   Ú	feed_datar/   Ú_buffer©r   r   r   r   r   Útest_feed_empty_dataŒ   s    
z StreamTests.test_feed_empty_datac                 C   s.   t j| jd}| | j¡ |  | j|j¡ d S rA   )r   r   r   rS   ÚDATAr/   rT   rU   r   r   r   Útest_feed_nonempty_data’   s    z#StreamTests.test_feed_nonempty_datac                 C   sL   t j| jd}| | j¡ | j | d¡¡}|  d|¡ |  | j|j¡ d S )NrB   r   r%   )	r   r   r   rS   rW   r,   r0   r/   rT   ©r   r   r8   r   r   r   Útest_read_zero˜   s
    zStreamTests.test_read_zeroc                    sf   t jˆ jd‰ˆ j ˆ d¡¡}‡ ‡fdd„}ˆ j |¡ ˆ j |¡}ˆ  ˆ j|¡ ˆ  dˆj	¡ d S )NrB   é   c                      s   ˆ  ˆ j¡ d S r   ©rS   rW   r   rU   r   r   Úcb¦   s    z!StreamTests.test_read.<locals>.cbr%   )
r   r   r   Úcreate_taskr0   Ú	call_soonr,   r/   rW   rT   ©r   Ú	read_taskr]   r8   r   rU   r   Ú	test_read¡   s    zStreamTests.test_readc                 C   sR   t j| jd}| d¡ | d¡ | j | d¡¡}|  d|¡ |  d|j¡ d S )NrB   s   line1s   line2é   ©r   r   r   rS   r,   r0   r/   rT   rY   r   r   r   Útest_read_line_breaks®   s    

z!StreamTests.test_read_line_breaksc                    sb   t j| jd‰ | j ˆ  d¡¡}‡ fdd„}| j |¡ | j |¡}|  d|¡ |  dˆ j¡ d S )NrB   i   c                      s   ˆ   ¡  d S r   )Úfeed_eofr   ©r   r   r   r]   ¾   s    z%StreamTests.test_read_eof.<locals>.cbr%   ©	r   r   r   r^   r0   r_   r,   r/   rT   r`   r   rg   r   Útest_read_eof¹   s    zStreamTests.test_read_eofc                    sb   t j| jd‰ | j ˆ  d¡¡}‡ fdd„}| j |¡ | j |¡}|  d|¡ |  dˆ j¡ d S )NrB   éÿÿÿÿc                      s    ˆ   d¡ ˆ   d¡ ˆ  ¡  d S )Ns   chunk1
ó   chunk2©rS   rf   r   rg   r   r   r]   Ë   s    

z+StreamTests.test_read_until_eof.<locals>.cbs   chunk1
chunk2r%   rh   r`   r   rg   r   Útest_read_until_eofÆ   s    zStreamTests.test_read_until_eofc                 C   s^   t j| jd}| d¡ | j | d¡¡}|  d|¡ | tƒ ¡ |  	t| jj| d¡¡ d S ©NrB   ó   line
é   ó   li)
r   r   r   rS   r,   r0   r/   Úset_exceptionÚ
ValueErrorrN   rY   r   r   r   Útest_read_exceptionÖ   s    
ÿzStreamTests.test_read_exceptionc                 C   s|   |   td¡  tjd| jd W d   ƒ n1 s20    Y  |   td¡  tjd| jd W d   ƒ n1 sn0    Y  d S )NZimitr   ©Úlimitr   rj   )ÚassertRaisesRegexrs   r   r   r   r   r   r   r   Útest_invalid_limitá   s    .zStreamTests.test_invalid_limitc                 C   sJ   t jd| jd}| d¡ | j | d¡¡}|  d|¡ |  d|j¡ d S ©Né   ru   s   chunkrc   r%   rd   rY   r   r   r   Útest_read_limitè   s
    
zStreamTests.test_read_limitc                    sj   t j| jd‰ ˆ  d¡ | j ˆ  ¡ ¡}‡ fdd„}| j |¡ | j |¡}|  d|¡ |  dˆ j	¡ d S )NrB   s   chunk1 c                      s"   ˆ   d¡ ˆ   d¡ ˆ   d¡ d S )Ns   chunk2 s   chunk3 s   
 chunk4)rS   r   rg   r   r   r]   ö   s    

z%StreamTests.test_readline.<locals>.cbs   chunk1 chunk2 chunk3 
s    chunk4)
r   r   r   rS   r^   r.   r_   r,   r/   rT   )r   ra   r]   Úliner   rg   r   Útest_readlineï   s    
zStreamTests.test_readlinec                 C   sž   t jd| jd}| d¡ | d¡ |  t| jj| ¡ ¡ |  d|j	¡ t jd| jd}| d¡ | d¡ | d¡ |  t| jj| ¡ ¡ |  d|j	¡ d S )Nrz   ru   rq   s
   ne1
line2
s   line2
s   ne1r%   )
r   r   r   rS   rN   rs   r,   r.   r/   rT   rU   r   r   r   Ú&test_readline_limit_with_existing_data   s    

ÿ


ÿz2StreamTests.test_readline_limit_with_existing_datac                 C   s†   t j| jd}|  | ¡ ¡ | d¡ |  | ¡ ¡ | j | ¡ ¡ |  | ¡ ¡ | d¡ | ¡  | j | ¡ ¡ |  	| ¡ ¡ d S )NrB   s
   some data
)
r   r   r   ÚassertFalseZat_eofrS   r,   r.   rf   r1   rU   r   r   r   Útest_at_eof  s    

zStreamTests.test_at_eofc                    sˆ  t jd| jd‰ ‡ fdd„}| j |¡ |  t| jjˆ  ¡ ¡ |  dˆ j	¡ t jd| jd‰ ‡ fdd„}| j |¡ |  t| jjˆ  ¡ ¡ |  dˆ j	¡ t jd| jd‰ ˆ  
d¡ | j ˆ  ¡ ¡}|  d|¡ |  dˆ j	¡ ˆ  
d	¡ |  t¡ }| j ˆ  ¡ ¡ W d   ƒ n1 s0    Y  |  dˆ j	¡ ˆ  
d
¡ |  t¡ }| j ˆ  ¡ ¡ W d   ƒ n1 sl0    Y  |  dˆ j	¡ d S )Né   ru   c                      s*   ˆ   d¡ ˆ   d¡ ˆ   d¡ ˆ  ¡  d S )Nó   chunk1rk   ó   chunk3
rl   r   rg   r   r   r]   /  s    


z+StreamTests.test_readline_limit.<locals>.cbr%   c                      s*   ˆ   d¡ ˆ   d¡ ˆ   d¡ ˆ  ¡  d S )Nr‚   s   chunk2
rƒ   rl   r   rg   r   r   r]   =  s    


rƒ   s   1234567
s	   12345678
s   12345678)r   r   r   r_   rN   rs   r,   r.   r/   rT   rS   )r   r]   r|   Úcmr   rg   r   Útest_readline_limit*  s6    ÿÿ

0
0zStreamTests.test_readline_limitc                 C   sd   t j| jd}| | jd d… ¡ | | jdd … ¡ | j | ¡ ¡}|  d|¡ |  d|j¡ d S )NrB   é   s   line1
s   line2
line3
)	r   r   r   rS   rW   r,   r.   r/   rT   ©r   r   r|   r   r   r   Útest_readline_nolimit_nowaitY  s    z(StreamTests.test_readline_nolimit_nowaitc                 C   s@   t j| jd}| d¡ | ¡  | j | ¡ ¡}|  d|¡ d S )NrB   s	   some data)r   r   r   rS   rf   r,   r.   r/   r‡   r   r   r   Útest_readline_eofe  s
    
zStreamTests.test_readline_eofc                 C   s6   t j| jd}| ¡  | j | ¡ ¡}|  d|¡ d S rR   )r   r   r   rf   r,   r.   r/   r‡   r   r   r   Útest_readline_empty_eofm  s    z#StreamTests.test_readline_empty_eofc                 C   sZ   t j| jd}| | j¡ | j | ¡ ¡ | j | d¡¡}|  d|¡ |  d|j	¡ d S )NrB   r   s   line2
ls   ine3
)
r   r   r   rS   rW   r,   r.   r0   r/   rT   rY   r   r   r   Útest_readline_read_byte_countt  s    z)StreamTests.test_readline_read_byte_countc                 C   sh   t j| jd}| d¡ | j | ¡ ¡}|  d|¡ | tƒ ¡ |  	t| jj| ¡ ¡ |  d|j
¡ d S )NrB   ro   r%   )r   r   r   rS   r,   r.   r/   rr   rs   rN   rT   rY   r   r   r   Útest_readline_exception  s    
ÿz#StreamTests.test_readline_exceptionc                 C   sR   t j| jd}|  td¡$ | j |jdd¡ W d   ƒ n1 sD0    Y  d S )NrB   zSeparator should ber%   ©Ú	separator)r   r   r   rw   rs   r,   Ú	readuntilrU   r   r   r   Útest_readuntil_separator‹  s    z$StreamTests.test_readuntil_separatorc                 C   s¶   t j| jd}| d¡ | j |jdd¡}|  d|¡ |  d|j¡ | d¡ | j | d¡¡}|  d|¡ |  d|j¡ | d¡ | j | d¡¡}|  d|¡ |  d|j¡ d S )NrB   s   lineAAAó   AAAr   r%   s
   lineAAAxxxs   xxx©r   r   r   rS   r,   r   r/   rT   rY   r   r   r   Útest_readuntil_multi_chunks  s    


z'StreamTests.test_readuntil_multi_chunksc                 C   sf  t j| jd}| d¡ | d¡ | d¡ | j | d¡¡}|  d|¡ |  d|j¡ | d¡ | d¡ | d	¡ | j | d¡¡}|  d|¡ |  d|j¡ | d¡ | j | d¡¡}|  d|¡ |  d|j¡ | d
¡ | j | d¡¡}|  d
|¡ |  d|j¡ | d¡ | d¡ | d¡ | d¡ | j | d¡¡}|  d|¡ |  d|j¡ d S )NrB   s   QWEaas   XYaaó   as   aaas
   QWEaaXYaaar%   s   XYas   aas   Xaaas   XXXs   XXXaaar’   rY   r   r   r   Útest_readuntil_multi_chunks_1¢  s8    











z)StreamTests.test_readuntil_multi_chunks_1c                 C   s”   t j| jd}d}| |¡ | ¡  |  t jd¡"}| j | d¡¡ W d   ƒ n1 sZ0    Y  |  	|j
j|¡ |  |j
j¡ |  	d|j¡ d S )NrB   ó   some dataAAzundefined expected bytesr‘   r%   )r   r   r   rS   rf   rw   ÚIncompleteReadErrorr,   r   r/   Ú	exceptionÚpartialÚassertIsNoneÚexpectedrT   )r   r   r8   r„   r   r   r   Útest_readuntil_eofÅ  s    
ÿ0zStreamTests.test_readuntil_eofc                 C   sÄ   t j| jdd}| d¡ |  t jd¡"}| j | d¡¡ W d   ƒ n1 sP0    Y  |  d|j	¡ | d¡ |  t jd¡"}| j | d¡¡ W d   ƒ n1 s¨0    Y  |  d|j	¡ d S )	Nrz   ©r   rv   r–   z	not foundr‘   ó   Azis founds   some dataAAA)
r   r   r   rS   rw   ÚLimitOverrunErrorr,   r   r/   rT   )r   r   r„   r   r   r   Útest_readuntil_limit_found_sepÒ  s    
ÿ0
ÿ0z*StreamTests.test_readuntil_limit_found_sepc                 C   sš   t j| jd}| | j¡ | j | d¡¡}|  d|¡ |  | j|j¡ |  	t
d¡" | j | d¡¡ W d   ƒ n1 s|0    Y  |  | j|j¡ d S )NrB   r   r%   zless than zerorj   )r   r   r   rS   rW   r,   Úreadexactlyr/   rT   rw   rs   rY   r   r   r   Útest_readexactly_zero_or_lessâ  s    0z)StreamTests.test_readexactly_zero_or_lessc                    s|   t jˆ jd‰dtˆ jƒ }ˆ j ˆ |¡¡}‡ ‡fdd„}ˆ j |¡ ˆ j |¡}ˆ  	ˆ jˆ j |¡ ˆ  	ˆ jˆj
¡ d S )NrB   rp   c                      s(   ˆ  ˆ j¡ ˆ  ˆ j¡ ˆ  ˆ j¡ d S r   r\   r   rU   r   r   r]   ö  s    z(StreamTests.test_readexactly.<locals>.cb)r   r   r   ÚlenrW   r^   r¡   r_   r,   r/   rT   )r   Únra   r]   r8   r   rU   r   Útest_readexactlyï  s    zStreamTests.test_readexactlyc                 C   sJ   t jd| jd}| d¡ | j | d¡¡}|  d|¡ |  d|j¡ d S ry   )r   r   r   rS   r,   r¡   r/   rT   rY   r   r   r   Útest_readexactly_limit   s
    
z"StreamTests.test_readexactly_limitc                    sÆ   t jˆ jd‰dtˆ jƒ }ˆ j ˆ |¡¡}‡ ‡fdd„}ˆ j |¡ ˆ  t j	¡}ˆ j 
|¡ W d   ƒ n1 sv0    Y  ˆ  |jjˆ j¡ ˆ  |jj|¡ ˆ  t|jƒd¡ ˆ  dˆj¡ d S )NrB   rp   c                      s   ˆ  ˆ j¡ ˆ ¡  d S r   )rS   rW   rf   r   rU   r   r   r]     s    z,StreamTests.test_readexactly_eof.<locals>.cbz-18 bytes read on a total of 36 expected bytesr%   )r   r   r   r£   rW   r^   r¡   r_   rN   r—   r,   r/   r˜   r™   r›   ÚstrrT   )r   r¤   ra   r]   r„   r   rU   r   Útest_readexactly_eof  s    *ÿz StreamTests.test_readexactly_eofc                 C   s^   t j| jd}| d¡ | j | d¡¡}|  d|¡ | tƒ ¡ |  	t| jj| d¡¡ d S rn   )
r   r   r   rS   r,   r¡   r/   rr   rs   rN   rY   r   r   r   Útest_readexactly_exception  s    
ÿz&StreamTests.test_readexactly_exceptionc                 C   s@   t j| jd}|  | ¡ ¡ tƒ }| |¡ |  | ¡ |¡ d S rA   )r   r   r   rš   r˜   rs   rr   r   ©r   r   Úexcr   r   r   Útest_exception%  s
    
zStreamTests.test_exceptionc                    s`   t j| jd‰ ‡ fdd„}| j ˆ  ¡ ¡}| j |ƒ ¡}| j t  ||g¡¡ |  t|j	¡ d S )NrB   c                   “   s   ˆ   tƒ ¡ d S r   )rr   rs   r   rg   r   r   Úset_err0  s    z2StreamTests.test_exception_waiter.<locals>.set_err)
r   r   r   r^   r.   r,   ÚwaitrN   rs   Úresult)r   r­   Út1Út2r   rg   r   Útest_exception_waiter-  s    z!StreamTests.test_exception_waiterc                 C   sj   t j| jd}| j | ¡ ¡}t | j¡ | ¡  t | j¡ | t	dƒ¡ t | j¡ |  
|jd ¡ d S )NrB   Úmessage)r   r   r   r^   r.   r   r   Úcancelrr   ÚRuntimeErrorr   Ú_waiter)r   r   Útr   r   r   Útest_exception_cancel:  s    z!StreamTests.test_exception_cancelc                    s  G dd„ dƒ}‡fdd„}g ‰ ˆj  ‡ fdd„¡ |ˆj ƒ}ˆ t¡ | ¡ }W d   ƒ n1 sd0    Y  ˆj  ˆj  ||ƒ¡¡}| ¡  ˆ |d¡ |ˆj ƒ}ˆ t¡ | 	¡ }W d   ƒ n1 sÌ0    Y  ˆj  ˆj  ||ƒ¡¡}| ¡  ˆ |d¡ ˆ ˆ g ¡ d S )Nc                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )z/StreamTests.test_start_server.<locals>.MyServerc                 S   s   d | _ || _d S r   )Úserverr   )r   r   r   r   r   Ú__init__J  s    z8StreamTests.test_start_server.<locals>.MyServer.__init__c                 Ó   s@   |  ¡ I d H }| |¡ | ¡ I d H  | ¡  | ¡ I d H  d S r   ©r.   r-   Údrainr   Úwait_closed©r   Úclient_readerÚclient_writerr8   r   r   r   Úhandle_clientN  s
    
z=StreamTests.test_start_server.<locals>.MyServer.handle_clientc                 S   s0   t  d¡}| j tj| j|| jd¡| _| ¡ S )N©z	127.0.0.1r   )Úsockr   )	ÚsocketÚcreate_serverr   r,   r   Ústart_serverrÁ   r¹   Úgetsockname)r   rÃ   r   r   r   ÚstartU  s    
þÿz5StreamTests.test_start_server.<locals>.MyServer.startc                 S   s   | j  |  ||¡¡ d S r   ©r   r^   rÁ   ©r   r¿   rÀ   r   r   r   Úhandle_client_callback]  s    ÿzFStreamTests.test_start_server.<locals>.MyServer.handle_client_callbackc                 S   sF   t  d¡}| ¡ }| ¡  | j tj| j|d |d | jd¡| _	|S )NrÂ   r   é   )ÚhostÚportr   )
rÄ   rÅ   rÇ   r   r   r,   r   rÆ   rË   r¹   )r   rÃ   Úaddrr   r   r   Ústart_callbacka  s    
þÿz>StreamTests.test_start_server.<locals>.MyServer.start_callbackc                 S   s0   | j d ur,| j  ¡  | j | j  ¡ ¡ d | _ d S r   ©r¹   r   r   r,   r½   r   r   r   r   Ústopk  s    

z4StreamTests.test_start_server.<locals>.MyServer.stopN©	Ú__name__Ú
__module__Ú__qualname__rº   rÁ   rÈ   rË   rÐ   rÒ   r   r   r   r   ÚMyServerH  s   
r×   c                 “   sx   ˆ   t¡, tj| dˆ jiŽI d H \}}W d   ƒ n1 s<0    Y  | d¡ | ¡ I d H }| ¡  | ¡ I d H  |S )Nr   ó   hello world!
)	r*   r+   r   r<   r   r-   r.   r   r½   )rÏ   r5   r6   Úmsgbackr   r   r   Úclientq  s    ÿÿ.
z-StreamTests.test_start_server.<locals>.clientc                    s
   ˆ   |¡S r   r   r    r"   r   r   r$   ~  r%   z/StreamTests.test_start_server.<locals>.<lambda>rØ   )
r   r)   r*   r+   rÈ   r,   r^   rÒ   r/   rÐ   )r   r×   rÚ   r¹   rÏ   Úmsgr   ©r#   r   r   Útest_start_serverF  s"    )
&
&zStreamTests.test_start_serverc              	      sj  G dd„ dƒ}‡fdd„}g ‰ ˆj  ‡ fdd„¡ t ¡ z}|ˆj |ƒ}ˆ t¡ | ¡  W d   ƒ n1 sp0    Y  ˆj  ˆj  ||ƒ¡¡}| 	¡  ˆ 
|d¡ W d   ƒ n1 sº0    Y  t ¡ |}|ˆj |ƒ}ˆ t¡ | ¡  W d   ƒ n1 s0    Y  ˆj  ˆj  ||ƒ¡¡}| 	¡  ˆ 
|d¡ W d   ƒ n1 sP0    Y  ˆ 
ˆ g ¡ d S )Nc                   @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )z4StreamTests.test_start_unix_server.<locals>.MyServerc                 S   s   d | _ || _|| _d S r   )r¹   r   Úpath)r   r   rÞ   r   r   r   rº   —  s    z=StreamTests.test_start_unix_server.<locals>.MyServer.__init__c                 Ó   s@   |  ¡ I d H }| |¡ | ¡ I d H  | ¡  | ¡ I d H  d S r   r»   r¾   r   r   r   rÁ   œ  s
    
zBStreamTests.test_start_unix_server.<locals>.MyServer.handle_clientc                 S   s$   | j  tj| j| j| j d¡| _d S ©N)rÞ   r   )r   r,   r   Ústart_unix_serverrÁ   rÞ   r¹   r   r   r   r   rÈ   £  s    þÿz:StreamTests.test_start_unix_server.<locals>.MyServer.startc                 S   s   | j  |  ||¡¡ d S r   rÉ   rÊ   r   r   r   rË   ©  s    ÿzKStreamTests.test_start_unix_server.<locals>.MyServer.handle_client_callbackc                 S   s(   t j| j| j| jd}| j |¡| _d S rß   )r   rà   rË   rÞ   r   r,   r¹   )r   rÈ   r   r   r   rÐ   ­  s
    þzCStreamTests.test_start_unix_server.<locals>.MyServer.start_callbackc                 S   s0   | j d ur,| j  ¡  | j | j  ¡ ¡ d | _ d S r   rÑ   r   r   r   r   rÒ   ³  s    

z9StreamTests.test_start_unix_server.<locals>.MyServer.stopNrÓ   r   r   r   r   r×   •  s   r×   c                 “   sv   ˆ   t¡* tj| ˆ jdI d H \}}W d   ƒ n1 s:0    Y  | d¡ | ¡ I d H }| ¡  | ¡ I d H  |S )NrB   rØ   )	r*   r+   r   rD   r   r-   r.   r   r½   )rÞ   r5   r6   rÙ   r   r   r   rÚ   ¹  s    ÿ.
z2StreamTests.test_start_unix_server.<locals>.clientc                    s
   ˆ   |¡S r   r   r    r"   r   r   r$   Æ  r%   z4StreamTests.test_start_unix_server.<locals>.<lambda>rØ   )r   r)   r   Zunix_socket_pathr*   r+   rÈ   r,   r^   rÒ   r/   rÐ   )r   r×   rÚ   rÞ   r¹   rÛ   r   rÜ   r   Útest_start_unix_server’  s.    $
&ÿ*
(ÿ,z"StreamTests.test_start_unix_serverÚwin32zDon't have pipesc              	      s  d}t  ¡ \}}tjd|t|ƒg}t|ddƒ}tj| jdd}tj	|| jd‰ | j 
| j ‡ fdd	„|¡¡\}}t ¡ }	|	 | j¡ zFt |	¡ tj|d
|hiŽ}
| j 
|
¡}| j 
| ¡ ¡ W t d ¡ nt d ¡ 0 t  |¡ | j 
| d¡¡}|  |d¡ d S )NzHimport os, sys
fd = int(sys.argv[1])
os.write(fd, b'data')
os.close(fd)
z-cÚrbr   rÌ   r   rB   c                      s   ˆ S r   r   r   ©Úprotocolr   r   r$   ó  r%   z<StreamTests.test_read_all_from_pipe_reader.<locals>.<lambda>Úpass_fdsrj   ó   data)ÚosÚpipeÚsysÚ
executabler§   Úopenr   r   r   ÚStreamReaderProtocolr,   Zconnect_read_pipeZSafeChildWatcherZattach_loopZset_child_watcherZcreate_subprocess_execr®   r   r0   r/   )r   ÚcodeZrfdZwfdÚargsré   r5   Z	transportÚ_ZwatcherZcreateÚprocr8   r   rä   r   Útest_read_all_from_pipe_readerÞ  s0    ÿ
ÿþ
z*StreamTests.test_read_all_from_pipe_readerc                 C   s6   |   tjd ¡ t | j¡ t ¡ }|  |j| j¡ d S r   )Ú
addCleanupr   r   r   r   r   r   )r   r5   r   r   r   Útest_streamreader_constructor  s    z)StreamTests.test_streamreader_constructorc                 C   s@   |   tjd ¡ t | j¡ t ¡ }t |¡}|  |j| j¡ d S r   )	ró   r   r   r   r   ÚMockrí   r   r   )r   r5   rå   r   r   r   Ú%test_streamreaderprotocol_constructor  s
    
z1StreamTests.test_streamreaderprotocol_constructorc                    s¶   g ‰ ˆj  ‡ fdd„¡ t ¡ ‰‡fdd„}‡fdd„}tj|d}| d¡ | ¡  ˆ ¡ }ˆ 	t
ttf¡  ˆj  ||Ž ¡ W d   ƒ n1 s”0    Y  | ¡  ˆ g ˆ ¡ d S )	Nc                    s
   ˆ   |¡S r   r   r    r"   r   r   r$   "  r%   z/StreamTests.test_drain_raises.<locals>.<lambda>c                     sT   t  d¡6} |  ¡ }ˆ  |¡ |  ¡ \}}| ¡  W d   ƒ n1 sF0    Y  d S )N)Ú	localhostr   )rÄ   rÅ   rÇ   ÚputÚacceptr   )rÃ   rÏ   Zcltrð   )Úqr   r   r¹   %  s
    
z-StreamTests.test_drain_raises.<locals>.serverc                 “   sd   ˆ   t¡, tj| |ˆ jdI d H \}}W d   ƒ n1 s<0    Y  | d¡ | ¡ I d H  qFd S )NrB   s   foo
)r*   r+   r   r<   r   r-   r¼   )rÍ   rÎ   r5   r6   r   r   r   rÚ   -  s    ÿ.
z-StreamTests.test_drain_raises.<locals>.client)ÚtargetT)r   r)   ÚqueueÚQueueÚ	threadingÚThreadÚ	setDaemonrÈ   ÚgetrN   ÚConnectionResetErrorÚConnectionAbortedErrorÚBrokenPipeErrorr,   Újoinr/   )r   r¹   rÚ   ÚthreadrÏ   r   )r#   rú   r   r   Útest_drain_raises  s    

ÿ.zStreamTests.test_drain_raisesc                 C   s"   t j| jd}|  dt|ƒ¡ d S )NrB   ú<StreamReader>©r   r   r   r/   ÚreprrU   r   r   r   Útest___repr__F  s    zStreamTests.test___repr__c                 C   s$   t j| jdd}|  dt|ƒ¡ d S )Né{   r   z<StreamReader limit=123>r	  rU   r   r   r   Útest___repr__nondefault_limitJ  s    z)StreamTests.test___repr__nondefault_limitc                 C   s*   t j| jd}| ¡  |  dt|ƒ¡ d S )NrB   z<StreamReader eof>)r   r   r   rf   r/   r
  rU   r   r   r   Útest___repr__eofN  s    zStreamTests.test___repr__eofc                 C   s,   t j| jd}| d¡ |  dt|ƒ¡ d S )NrB   rç   z<StreamReader 4 bytes>)r   r   r   rS   r/   r
  rU   r   r   r   Útest___repr__dataS  s    
zStreamTests.test___repr__datac                 C   s2   t j| jd}tƒ }| |¡ |  dt|ƒ¡ d S )NrB   z'<StreamReader exception=RuntimeError()>)r   r   r   rµ   rr   r/   r
  rª   r   r   r   Útest___repr__exceptionX  s    
ÿz"StreamTests.test___repr__exceptionc                 C   sb   t j| jd}t j| jd|_|  t|ƒd¡ |j d ¡ | j |j¡ d |_|  	dt|ƒ¡ d S )NrB   z,<StreamReader waiter=<Future pending[\S ]*>>r  )
r   r   r   ÚFuturer¶   ZassertRegexr
  Ú
set_resultr,   r/   rU   r   r   r   Útest___repr__waiter_  s    þzStreamTests.test___repr__waiterc                 C   sB   t j| jd}t ¡ |_t ¡ |j_d|jj_|  dt	|ƒ¡ d S )NrB   z<Transport>z$<StreamReader transport=<Transport>>)
r   r   r   r   rõ   Z
_transportÚ__repr__r   r/   r
  rU   r   r   r   Útest___repr__transportj  s
    

z"StreamTests.test___repr__transportc              	   C   s˜   t  dd¡}ttjd ƒD ]x}| j|dX t tj||d¡}|  t	|ƒt	|ƒ¡ |  |j
|j
¡ |  |j|j¡ W d   ƒ q1 sˆ0    Y  qd S )Ns   abcé
   rÌ   ©Zpickle_protocolrä   )r   r—   ÚrangeÚpickleÚHIGHEST_PROTOCOLÚsubTestÚloadsÚdumpsr/   r§   r™   r›   ©r   ÚeÚprotoÚe2r   r   r   Ú#test_IncompleteReadError_pickleableq  s    z/StreamTests.test_IncompleteReadError_pickleablec              	   C   sˆ   t  dd¡}ttjd ƒD ]h}| j|dH t tj||d¡}|  t	|ƒt	|ƒ¡ |  |j
|j
¡ W d   ƒ q1 sx0    Y  qd S )Nr³   r  rÌ   r  rä   )r   rŸ   r  r  r  r  r  r  r/   r§   Úconsumedr  r   r   r   Ú!test_LimitOverrunError_pickleablez  s    z-StreamTests.test_LimitOverrunError_pickleablec              	   C   sø   t  ¡ Ü}|  t¡0 | j tj|jd| jiŽ¡\}}W d   ƒ n1 sJ0    Y  | 	d¡ | 
¡ }| j |¡}|  |d¡ | ¡ }| j |¡}|  | d¡¡ |  | ¡ ¡ | ¡  |  | ¡ ¡ | j | ¡ ¡ W d   ƒ n1 sê0    Y  d S )Nr   r&   r'   r(   )r   r;   r*   r+   r   r,   r   r<   r=   r-   r.   r/   r0   r1   r2   r   Z
is_closingr   r½   ©r   r?   ÚrdÚwrr7   r8   r   r   r   Útest_wait_closed_on_close‚  s     
ÿ&
z%StreamTests.test_wait_closed_on_closec              	   C   s¸   t  ¡ œ}|  t¡0 | j tj|jd| jiŽ¡\}}W d   ƒ n1 sJ0    Y  | 	d¡ | 
¡ }| j |¡}|  |d¡ | ¡  | j | ¡ ¡ W d   ƒ n1 sª0    Y  d S )Nr   r&   r'   )r   r;   r*   r+   r   r,   r   r<   r=   r-   r.   r/   r   r½   r%  r   r   r   Ú*test_wait_closed_on_close_with_unread_data”  s    
ÿ&
z6StreamTests.test_wait_closed_on_close_with_unread_datac                    sl   ‡fdd„}g ‰ ˆj  ‡ fdd„¡ t ¡  }ˆj  ||ƒ¡ W d   ƒ n1 sR0    Y  ˆ ˆ g ¡ d S )Nc                 “   sr   t j| jŽ I d H \}}| d¡ | ¡ I d H }ˆ  |d¡ | ¡ I d H }ˆ  | d¡¡ | 	¡  | 
¡ I d H  d S )Nr&   r'   r(   )r   r<   r=   r-   r.   r/   r0   r1   r2   r   r½   ©r?   r&  r'  r8   r   r   r   Úinner¢  s    
z0StreamTests.test_async_writer_api.<locals>.innerc                    s
   ˆ   |¡S r   r   r    r"   r   r   r$   ®  r%   z3StreamTests.test_async_writer_api.<locals>.<lambda>©r   r)   r   r;   r,   r/   ©r   r+  r?   r   rÜ   r   Útest_async_writer_api¡  s    
.z!StreamTests.test_async_writer_apic                    sl   ‡fdd„}g ‰ ˆj  ‡ fdd„¡ t ¡  }ˆj  ||ƒ¡ W d   ƒ n1 sR0    Y  ˆ ˆ g ¡ d S )Nc                 “   s¦   t j| jŽ I d H \}}| d¡ | ¡ I d H }ˆ  |d¡ | ¡ I d H }ˆ  | d¡¡ | 	¡  ˆ  
t¡( | d¡ | ¡ I d H  W d   ƒ n1 s˜0    Y  d S )Nr&   r'   r(   rç   )r   r<   r=   r-   r.   r/   r0   r1   r2   r   rN   r  r¼   r*  r   r   r   r+  ¶  s    

zFStreamTests.test_async_writer_api_exception_after_close.<locals>.innerc                    s
   ˆ   |¡S r   r   r    r"   r   r   r$   Ä  r%   zIStreamTests.test_async_writer_api_exception_after_close.<locals>.<lambda>r,  r-  r   rÜ   r   Ú+test_async_writer_api_exception_after_closeµ  s    
.z7StreamTests.test_async_writer_api_exception_after_closec              	      sÊ   g ‰ | j  ‡ fdd„¡ t ¡ Š}|  t¡0 | j  tj|j	d| j iŽ¡\}}W d   ƒ n1 sb0    Y  | 
¡  | ¡ }| j  |¡ | ¡ }| j  |¡}W d   ƒ n1 s°0    Y  |  ˆ g ¡ d S )Nc                    s
   ˆ   |¡S r   r   r    r"   r   r   r$   Î  r%   z?StreamTests.test_eof_feed_when_closing_writer.<locals>.<lambda>r   )r   r)   r   r;   r*   r+   r,   r   r<   r=   r   r½   r0   r/   r%  r   r"   r   Ú!test_eof_feed_when_closing_writerË  s     

ÿÿ&z-StreamTests.test_eof_feed_when_closing_writer)NrÔ   rÕ   rÖ   rW   r   r   r   Zpatchr   r9   r@   r   Zskip_unless_bind_unix_socketrE   rF   ÚunittestZskipIfrH   rJ   rL   rO   rP   rQ   rV   rX   rZ   rb   re   ri   rm   rt   rx   r{   r}   r~   r€   r…   rˆ   r‰   rŠ   r‹   rŒ   r   r“   r•   rœ   r    r¢   r¥   r¦   r¨   r©   r¬   r²   r¸   rÝ   rá   rê   Úplatformrò   rô   rö   r  r  r  r  r  r  r  r  r"  r$  r(  r)  r.  r/  r0  Ú__classcell__r   r   r   r   r
      s’   


	

	/#L
K
'	
-	r
   Ú__main__)Ú__doc__r   rè   rü   r  rÄ   rê   rþ   r1  r   Ztest.supportr   rH   ÚImportErrorr   Ztest.test_asyncior   r   r	   ZTestCaser
   rÔ   Úmainr   r   r   r   Ú<module>   s6   
       N