
Wz                 @   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 m Z y d d l	 Z	 Wn e
 k
 r d Z	 Yn Xd d l Z d d l m Z Gd d   d e j  Z e d k r e j   d S)zTests for streams.py.    N)mock)
test_utilsc                   s  e  Z d  Z d Z d d   Z   f d d   Z e j d  d d    Z d	 d
   Z	 d d   Z
 e j e e d  d  d d    Z d d   Z e j e d k d  d d    Z e j e d k d  e j e e d  d  d d     Z d d   Z d d   Z e j e e d  d  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  d1 d2   Z! d3 d4   Z" d5 d6   Z# d7 d8   Z$ d9 d:   Z% d; d<   Z& d= d>   Z' d? d@   Z( dA dB   Z) dC dD   Z* dE dF   Z+ dG dH   Z, dI dJ   Z- dK dL   Z. dM dN   Z/ dO dP   Z0 dQ dR   Z1 dS dT   Z2 dU dV   Z3 dW dX   Z4 dY dZ   Z5 d[ d\   Z6 d] d^   Z7 d_ d`   Z8 e j e e d  d  da db    Z9 e j e: j; dc k dd  de df    Z< dg dh   Z= di dj   Z> dk dl   Z? dm dn   Z@ do dp   ZA dq dr   ZB ds dt   ZC du dv   ZD dw dx   ZE dy dz   ZF   S){StreamReaderTestss   line1
line2
line3
c             C   s#   t  j   |  _ |  j |  j  d  S)N)asyncioZnew_event_looploopset_event_loop)self r	   A/afs/.cs/s/python-3.5.2/amd64_ubu14/lib/python3.5/test_streams.pysetUp   s    zStreamReaderTests.setUpc                s8   t  j |  j  |  j j   t j   t   j   d  S)N)r   run_brieflyr   closegcZcollectsupertearDown)r   )	__class__r	   r
   r      s    
zStreamReaderTests.tearDownzasyncio.streams.eventsc             C   s)   t  j   } |  j | j | j j  d  S)N)r   StreamReaderassertIs_loopZget_event_loopreturn_value)r   Zm_eventsstreamr	   r	   r
   test_ctor_global_loop$   s    z'StreamReaderTests.test_ctor_global_loopc             C   s   |  j  j |  \ } } | j d  | j   } |  j  j |  } |  j | d  | j   } |  j  j |  } |  j | j d   | j   d  S)Ns   GET / HTTP/1.0

s   HTTP/1.0 200 OK
s   

Test message)	r   run_until_completewritereadlineassertEqualread
assertTrueendswithr   )r   open_connection_futreaderwriterfdatar	   r	   r
   _basetest_open_connection)   s    z+StreamReaderTests._basetest_open_connectionc             C   sB   t  j   / } t j | j d |  j  } |  j |  Wd  QRXd  S)Nr   )r   run_test_serverr   open_connectionaddressr   r$   )r   httpdconn_futr	   r	   r
   test_open_connection4   s    z&StreamReaderTests.test_open_connectionAF_UNIXzNo UNIX Socketsc             C   sB   t  j   / } t j | j d |  j } |  j |  Wd  QRXd  S)Nr   )r   run_test_unix_serverr   open_unix_connectionr'   r   r$   )r   r(   r)   r	   r	   r
   test_open_unix_connection:   s    z+StreamReaderTests.test_open_unix_connectionc             C   s|   z |  j  j |  \ } } Wd  t j d   X| j d  | j   } |  j  j |  } |  j | j d   | j   d  S)Ns   GET / HTTP/1.0

s   

Test message)	r   r   r   r   r   r   r   r   r   )r   r   r    r!   r"   r#   r	   r	   r
   %_basetest_open_connection_no_loop_sslA   s    z7StreamReaderTests._basetest_open_connection_no_loop_sslNzNo ssl modulec             C   sT   t  j d d  ; } t j | j d t  j   d |  j  } |  j |  Wd  QRXd  S)Nuse_sslTsslr   )r   r%   r   r&   r'   dummy_ssl_contextr   r/   )r   r(   r)   r	   r	   r
    test_open_connection_no_loop_sslM   s    	z2StreamReaderTests.test_open_connection_no_loop_sslc             C   sZ   t  j d d  A } t j | j d t  j   d d d |  j } |  j |  Wd  QRXd  S)Nr0   Tr1   Zserver_hostname r   )r   r,   r   r-   r'   r2   r   r/   )r   r(   r)   r	   r	   r
   %test_open_unix_connection_no_loop_sslW   s    	z7StreamReaderTests.test_open_unix_connection_no_loop_sslc          	   C   s|   |  j  j |  \ } } | j j t    | j   } |  j t   |  j  j |  Wd  QRX| j   t j	 |  j   d  S)N)
r   r   Z	_protocolZconnection_lostZeroDivisionErrorr   assertRaisesr   r   r   )r   r   r    r!   r"   r	   r	   r
   _basetest_open_connection_errorc   s    
z1StreamReaderTests._basetest_open_connection_errorc             C   sB   t  j   / } t j | j d |  j  } |  j |  Wd  QRXd  S)Nr   )r   r%   r   r&   r'   r   r8   )r   r(   r)   r	   r	   r
   test_open_connection_errorl   s    z,StreamReaderTests.test_open_connection_errorc             C   sB   t  j   / } t j | j d |  j } |  j |  Wd  QRXd  S)Nr   )r   r,   r   r-   r'   r   r8   )r   r(   r)   r	   r	   r
   test_open_unix_connection_errorr   s    z1StreamReaderTests.test_open_unix_connection_errorc             C   s9   t  j d |  j  } | j d  |  j d | j  d  S)Nr       )r   r   r   	feed_datar   _buffer)r   r   r	   r	   r
   test_feed_empty_datay   s    z&StreamReaderTests.test_feed_empty_datac             C   s?   t  j d |  j  } | j |  j  |  j |  j | j  d  S)Nr   )r   r   r   r<   DATAr   r=   )r   r   r	   r	   r
   test_feed_nonempty_data   s    z)StreamReaderTests.test_feed_nonempty_datac             C   sj   t  j d |  j  } | j |  j  |  j j | j d   } |  j d |  |  j |  j | j  d  S)Nr   r   r;   )	r   r   r   r<   r?   r   r   r   r=   )r   r   r#   r	   r	   r
   test_read_zero   s
    z StreamReaderTests.test_read_zeroc                s   t  j d   j   t  j  j d  d   j }    f d d   }   j j |    j j |  }   j   j |    j d  j	  d  S)Nr      c                  s    j    j  d  S)N)r<   r?   r	   )r   r   r	   r
   cb   s    z'StreamReaderTests.test_read.<locals>.cbr;   )
r   r   r   Taskr   	call_soonr   r   r?   r=   )r   	read_taskrC   r#   r	   )r   r   r
   	test_read   s    !zStreamReaderTests.test_readc             C   sq   t  j d |  j  } | j d  | j d  |  j j | j d   } |  j d |  |  j d | j  d  S)Nr   s   line1s   line2   )r   r   r   r<   r   r   r   r=   )r   r   r#   r	   r	   r
   test_read_line_breaks   s    z'StreamReaderTests.test_read_line_breaksc                s   t  j d |  j    t  j   j d  d |  j }   f d d   } |  j j |  |  j j |  } |  j d |  |  j d   j  d  S)Nr   i   c                  s     j    d  S)N)feed_eofr	   )r   r	   r
   rC      s    z+StreamReaderTests.test_read_eof.<locals>.cbr;   )	r   r   r   rD   r   rE   r   r   r=   )r   rF   rC   r#   r	   )r   r
   test_read_eof   s    !zStreamReaderTests.test_read_eofc                s   t  j d |  j    t  j   j d  d |  j }   f d d   } |  j j |  |  j j |  } |  j d |  |  j d   j  d  S)Nr      c                  s(     j  d    j  d    j   d  S)Ns   chunk1
s   chunk2)r<   rJ   r	   )r   r	   r
   rC      s    z1StreamReaderTests.test_read_until_eof.<locals>.cbs   chunk1
chunk2r;   )	r   r   r   rD   r   rE   r   r   r=   )r   rF   rC   r#   r	   )r   r
   test_read_until_eof   s    !z%StreamReaderTests.test_read_until_eofc             C   s   t  j d |  j  } | j d  |  j j | j d   } |  j d |  | j t    |  j	 t |  j j | j d   d  S)Nr   s   line
   s   li)
r   r   r   r<   r   r   r   set_exception
ValueErrorr7   )r   r   r#   r	   r	   r
   test_read_exception   s    z%StreamReaderTests.test_read_exceptionc             C   sj   |  j  t d   t j d d d |  j  Wd  QRX|  j  t d   t j d d d |  j  Wd  QRXd  S)NZimitlimitr   r   rL   rM   )assertRaisesRegexrQ   r   r   r   )r   r	   r	   r
   test_invalid_limit   s     z$StreamReaderTests.test_invalid_limitc             C   sj   t  j d d d |  j  } | j d  |  j j | j d   } |  j d |  |  j d | j  d  S)NrS      r   s   chunkrH   r;   )r   r   r   r<   r   r   r   r=   )r   r   r#   r	   r	   r
   test_read_limit   s
    z!StreamReaderTests.test_read_limitc                s   t  j d |  j      j d  t  j   j   d |  j }   f d d   } |  j j |  |  j j |  } |  j d |  |  j d   j	  d  S)Nr   s   chunk1 c                  s+     j  d    j  d    j  d  d  S)Ns   chunk2 s   chunk3 s   
 chunk4)r<   r	   )r   r	   r
   rC      s    z+StreamReaderTests.test_readline.<locals>.cbs   chunk1 chunk2 chunk3 
s    chunk4)
r   r   r   r<   rD   r   rE   r   r   r=   )r   rF   rC   liner	   )r   r
   test_readline   s    zStreamReaderTests.test_readlinec             C   s   t  j d d d |  j  } | j d  | j d  |  j t |  j j | j    |  j d | j	  t  j d d d |  j  } | j d  | j d  | j d  |  j t |  j j | j    |  j d | j	  d  S)	NrS   rV   r   s   lis
   ne1
line2
s   line2
s   ne1r;   )
r   r   r   r<   r7   rQ   r   r   r   r=   )r   r   r	   r	   r
   &test_readline_limit_with_existing_data   s    z8StreamReaderTests.test_readline_limit_with_existing_datac             C   s   t  j d |  j  } |  j | j    | j d  |  j | j    |  j j | j    |  j | j    | j d  | j   |  j j | j    |  j	 | j    d  S)Nr   s
   some data
)
r   r   r   ZassertFalseZat_eofr<   r   r   rJ   r   )r   r   r	   r	   r
   test_at_eof  s    
zStreamReaderTests.test_at_eofc                s  t  j d d d |  j      f d d   } |  j j |  |  j t |  j j   j    |  j d   j	  t  j d d d |  j      f d d   } |  j j |  |  j t |  j j   j    |  j d   j	  t  j d d d |  j      j
 d	  |  j j   j    } |  j d	 |  |  j d   j	    j
 d
  |  j t   } |  j j   j    Wd  QRX|  j d   j	    j
 d  |  j t   } |  j j   j    Wd  QRX|  j d   j	  d  S)NrS      r   c                  s5     j  d    j  d    j  d    j   d  S)Ns   chunk1s   chunk2s   chunk3
)r<   rJ   r	   )r   r	   r
   rC     s    z1StreamReaderTests.test_readline_limit.<locals>.cbr;   c                  s5     j  d    j  d    j  d    j   d  S)Ns   chunk1s   chunk2
s   chunk3
)r<   rJ   r	   )r   r	   r
   rC   *  s    s   chunk3
s   1234567
s	   12345678
s   12345678)r   r   r   rE   r7   rQ   r   r   r   r=   r<   )r   rC   rX   cmr	   )r   r
   test_readline_limit  s2    z%StreamReaderTests.test_readline_limitc             C   s   t  j d |  j  } | j |  j d  d   | j |  j d d    |  j j | j    } |  j d |  |  j d | j  d  S)Nr      s   line1
s   line2
line3
)	r   r   r   r<   r?   r   r   r   r=   )r   r   rX   r	   r	   r
   test_readline_nolimit_nowaitF  s    z.StreamReaderTests.test_readline_nolimit_nowaitc             C   sX   t  j d |  j  } | j d  | j   |  j j | j    } |  j d |  d  S)Nr   s	   some data)r   r   r   r<   rJ   r   r   r   )r   r   rX   r	   r	   r
   test_readline_eofR  s
    
z#StreamReaderTests.test_readline_eofc             C   sK   t  j d |  j  } | j   |  j j | j    } |  j d |  d  S)Nr   r;   )r   r   r   rJ   r   r   r   )r   r   rX   r	   r	   r
   test_readline_empty_eofZ  s    
z)StreamReaderTests.test_readline_empty_eofc             C   s}   t  j d |  j  } | j |  j  |  j j | j    |  j j | j d   } |  j d |  |  j d | j	  d  S)Nr   r\   s   line2
ls   ine3
)
r   r   r   r<   r?   r   r   r   r   r=   )r   r   r#   r	   r	   r
   test_readline_read_byte_counta  s    z/StreamReaderTests.test_readline_read_byte_countc             C   s   t  j d |  j  } | j d  |  j j | j    } |  j d |  | j t    |  j	 t |  j j | j    |  j d | j
  d  S)Nr   s   line
r;   )r   r   r   r<   r   r   r   rP   rQ   r7   r=   )r   r   r#   r	   r	   r
   test_readline_exceptionl  s    z)StreamReaderTests.test_readline_exceptionc             C   sO   t  j d |  j  } |  j t d  ! |  j j | j d d   Wd  QRXd  S)Nr   zSeparator should be	separatorr;   )r   r   r   rT   rQ   r   	readuntil)r   r   r	   r	   r
   test_readuntil_separatorx  s    z*StreamReaderTests.test_readuntil_separatorc             C   s   t  j d |  j  } | j d  |  j j | j d d   } |  j d |  |  j d | j  | j d  |  j j | j d   } |  j d |  |  j d | j  | j d  |  j j | j d   } |  j d |  |  j d | j  d  S)Nr   s   lineAAAre   s   AAAr;   s
   lineAAAxxxs   xxx)r   r   r   r<   r   rf   r   r=   )r   r   r#   r	   r	   r
   test_readuntil_multi_chunks}  s    z-StreamReaderTests.test_readuntil_multi_chunksc             C   s  t  j d |  j  } | j d  | j d  | j d  |  j j | j d   } |  j d |  |  j d | j  | j d  | j d  | j d	  |  j j | j d   } |  j d |  |  j d | j  | j d  |  j j | j d   } |  j d |  |  j d | j  | j d
  |  j j | j d   } |  j d
 |  |  j d | j  | j d  | j d  | j d  | j d  |  j j | j d   } |  j d |  |  j d | j  d  S)Nr   s   QWEaas   XYaa   as   aaas
   QWEaaXYaaar;   s   XYas   aas   Xaaas   XXXs   XXXaaa)r   r   r   r<   r   rf   r   r=   )r   r   r#   r	   r	   r
   test_readuntil_multi_chunks_1  s8    z/StreamReaderTests.test_readuntil_multi_chunks_1c          
   C   s   t  j d |  j  } | j d  | j   |  j t  j    } |  j j | j d   Wd  QRX|  j	 | j
 j d  |  j | j
 j  |  j	 d | j  d  S)Nr   s   some dataAAs   AAAr;   )r   r   r   r<   rJ   r7   IncompleteReadErrorr   rf   r   	exceptionpartialassertIsNoneexpectedr=   )r   r   r]   r	   r	   r
   test_readuntil_eof  s    
 z$StreamReaderTests.test_readuntil_eofc             C   s   t  j d |  j d d  } | j d  |  j t  j d    } |  j j | j d   Wd  QRX|  j d | j	  | j d  |  j t  j d    } |  j j | j d   Wd  QRX|  j d	 | j	  d  S)
Nr   rS   rV   s   some dataAAz	not founds   AAA   Azis founds   some dataAAA)
r   r   r   r<   rT   ZLimitOverrunErrorr   rf   r   r=   )r   r   r]   r	   r	   r
   test_readuntil_limit_found_sep  s      z0StreamReaderTests.test_readuntil_limit_found_sepc          
   C   s   t  j d |  j  } | j |  j  |  j j | j d   } |  j d |  |  j |  j | j  |  j	 t
 d   |  j j | j d   Wd  QRX|  j |  j | j  d  S)Nr   r   r;   zless than zerorL   rM   )r   r   r   r<   r?   r   readexactlyr   r=   rT   rQ   )r   r   r#   r	   r	   r
   test_readexactly_zero_or_less  s     z/StreamReaderTests.test_readexactly_zero_or_lessc                s   t  j d   j   d t   j  } t  j  j |  d   j }    f d d   }   j j |    j j |  }   j	   j   j |    j	   j  j
  d  S)Nr   rO   c                  s4    j    j   j    j   j    j  d  S)N)r<   r?   r	   )r   r   r	   r
   rC     s    z.StreamReaderTests.test_readexactly.<locals>.cb)r   r   r   lenr?   rD   rs   rE   r   r   r=   )r   nrF   rC   r#   r	   )r   r   r
   test_readexactly  s    !z"StreamReaderTests.test_readexactlyc             C   sj   t  j d d d |  j  } | j d  |  j j | j d   } |  j d |  |  j d | j  d  S)NrS   rV   r   s   chunkrH   r;   )r   r   r   r<   r   rs   r   r=   )r   r   r#   r	   r	   r
   test_readexactly_limit  s
    z(StreamReaderTests.test_readexactly_limitc          
      s   t  j d   j   d t   j  } t  j  j |  d   j }    f d d   }   j j |    j t  j	   }   j j
 |  Wd  QRX  j | j j   j    j | j j |    j t | j  d    j d  j  d  S)Nr   rO   c                  s    j    j   j   d  S)N)r<   r?   rJ   r	   )r   r   r	   r
   rC     s    z2StreamReaderTests.test_readexactly_eof.<locals>.cbz-18 bytes read on a total of 36 expected bytesr;   )r   r   r   ru   r?   rD   rs   rE   r7   rk   r   r   rl   rm   ro   strr=   )r   rv   rF   rC   r]   r	   )r   r   r
   test_readexactly_eof  s    !z&StreamReaderTests.test_readexactly_eofc             C   s   t  j d |  j  } | j d  |  j j | j d   } |  j d |  | j t    |  j	 t |  j j | j d   d  S)Nr   s   line
rO   s   li)
r   r   r   r<   r   rs   r   rP   rQ   r7   )r   r   r#   r	   r	   r
   test_readexactly_exception  s    z,StreamReaderTests.test_readexactly_exceptionc             C   sX   t  j d |  j  } |  j | j    t   } | j |  |  j | j   |  d  S)Nr   )r   r   r   rn   rl   rQ   rP   r   )r   r   excr	   r	   r
   test_exception  s
    	z StreamReaderTests.test_exceptionc                s   t  j d |  j    t  j   f d d    } t  j   j   d |  j } t  j |   d |  j } |  j j t  j | | g d |  j  |  j t	 | j
  d  S)Nr   c                  s     j  t    d  S)N)rP   rQ   r	   )r   r	   r
   set_err  s    z8StreamReaderTests.test_exception_waiter.<locals>.set_err)r   r   r   	coroutinerD   r   r   waitr7   rQ   result)r   r~   t1Zt2r	   )r   r
   test_exception_waiter  s    (z'StreamReaderTests.test_exception_waiterc             C   s   t  j d |  j  } t  j | j   d |  j } t j |  j  | j   t j |  j  | j t	 d   t j |  j  |  j
 | j d   d  S)Nr   message)r   r   r   rD   r   r   r   cancelrP   RuntimeErrorr   _waiter)r   r   tr	   r	   r
   test_exception_cancel'  s    
z'StreamReaderTests.test_exception_cancelc                s   Gd d   d  } t  j   f d d    } |   j  } | j   }   j j t  j | |  d   j  } | j     j | d  |   j  } | j   }   j j t  j | |  d   j  } | j     j | d  d  S)Nc               @   sa   e  Z d  Z d d   Z e j d d    Z d d   Z d d   Z d	 d
   Z	 d d   Z
 d S)z5StreamReaderTests.test_start_server.<locals>.MyServerc             S   s   d  |  _  | |  _ d  S)N)serverr   )r   r   r	   r	   r
   __init__7  s    	z>StreamReaderTests.test_start_server.<locals>.MyServer.__init__c             s   s;   | j    Ed  H} | j |  | j   Ed  H| j   d  S)N)r   r   drainr   )r   client_readerclient_writerr#   r	   r	   r
   handle_client;  s    zCStreamReaderTests.test_start_server.<locals>.MyServer.handle_clientc             S   sS   t  j    } | j d  |  j j t j |  j d | d |  j  |  _ | j   S)N	127.0.0.1r   sockr   )r   r   )	socketbindr   r   r   start_serverr   r   getsockname)r   r   r	   r	   r
   startB  s    	z;StreamReaderTests.test_start_server.<locals>.MyServer.startc             S   s    |  j  j |  j | |   d  S)N)r   create_taskr   )r   r   r   r	   r	   r
   handle_client_callbackK  s    zLStreamReaderTests.test_start_server.<locals>.MyServer.handle_client_callbackc          	   S   sq   t  j    } | j d  | j   } | j   |  j j t j |  j d | d d | d d |  j  |  _	 | S)N	127.0.0.1r   hostportrL   r   )r   r   )
r   r   r   r   r   r   r   r   r   r   )r   r   addrr	   r	   r
   start_callbackO  s    
	zDStreamReaderTests.test_start_server.<locals>.MyServer.start_callbackc             S   sB   |  j  d  k	 r> |  j  j   |  j j |  j  j    d  |  _  d  S)N)r   r   r   r   wait_closed)r   r	   r	   r
   stopZ  s    z:StreamReaderTests.test_start_server.<locals>.MyServer.stopN)__name__
__module____qualname__r   r   r   r   r   r   r   r   r	   r	   r	   r
   MyServer5  s   	r   c             3   sO   t  j |  d   j  Ed  H\ } } | j d  | j   Ed  H} | j   | S)Nr   s   hello world!
)r   r&   r   r   r   r   )r   r    r!   msgback)r   r	   r
   client`  s    
z3StreamReaderTests.test_start_server.<locals>.clientr   s   hello world!
)	r   r   r   r   r   rD   r   r   r   )r   r   r   r   r   msgr	   )r   r
   test_start_server3  s    +

z#StreamReaderTests.test_start_serverc                s  Gd d   d  } t  j   f d d    } t j   g } |   j |  } | j     j j t  j | |  d   j  } | j     j	 | d  Wd  QRXt j   g } |   j |  } | j
     j j t  j | |  d   j  } | j     j	 | d  Wd  QRXd  S)Nc               @   sa   e  Z d  Z d d   Z e j d d    Z d d   Z d d   Z d	 d
   Z	 d d   Z
 d S)z:StreamReaderTests.test_start_unix_server.<locals>.MyServerc             S   s   d  |  _  | |  _ | |  _ d  S)N)r   r   path)r   r   r   r	   r	   r
   r     s    		zCStreamReaderTests.test_start_unix_server.<locals>.MyServer.__init__c             s   s;   | j    Ed  H} | j |  | j   Ed  H| j   d  S)N)r   r   r   r   )r   r   r   r#   r	   r	   r
   r     s    zHStreamReaderTests.test_start_unix_server.<locals>.MyServer.handle_clientc             S   s7   |  j  j t j |  j d |  j d |  j   |  _ d  S)Nr   r   )r   r   r   start_unix_serverr   r   r   )r   r	   r	   r
   r     s    		z@StreamReaderTests.test_start_unix_server.<locals>.MyServer.startc             S   s    |  j  j |  j | |   d  S)N)r   r   r   )r   r   r   r	   r	   r
   r     s    zQStreamReaderTests.test_start_unix_server.<locals>.MyServer.handle_client_callbackc             S   s=   t  j |  j d |  j d |  j } |  j j |  |  _ d  S)Nr   r   )r   r   r   r   r   r   r   )r   r   r	   r	   r
   r     s    	zIStreamReaderTests.test_start_unix_server.<locals>.MyServer.start_callbackc             S   sB   |  j  d  k	 r> |  j  j   |  j j |  j  j    d  |  _  d  S)N)r   r   r   r   r   )r   r	   r	   r
   r     s    z?StreamReaderTests.test_start_unix_server.<locals>.MyServer.stopN)r   r   r   r   r   r   r   r   r   r   r   r	   r	   r	   r
   r   ~  s   r   c             3   sO   t  j |  d   j Ed  H\ } } | j d  | j   Ed  H} | j   | S)Nr   s   hello world!
)r   r-   r   r   r   r   )r   r    r!   r   )r   r	   r
   r     s    
z8StreamReaderTests.test_start_unix_server.<locals>.clientr   s   hello world!
)r   r   r   Zunix_socket_pathr   r   r   rD   r   r   r   )r   r   r   r   r   r   r	   )r   r
   test_start_unix_server{  s     $



z(StreamReaderTests.test_start_unix_serverwin32zDon't have pipesc                sn  d } t  j   \ } } t j d | t |  g } t | d d  } t j d |  j d d  } t j	 | d |  j   |  j j
 |  j j   f d d	   |   \ } } t j   }	 |	 j |  j  zZ t j |	  t j | d
 | h d |  j  }
 |  j j
 |
  } |  j j
 | j    Wd  t j d   Xt  j |  |  j j
 | j d   } |  j | d  d  S)NzHimport os, sys
fd = int(sys.argv[1])
os.write(fd, b'data')
os.close(fd)
z-crbr   r   rS   rL   c                  s     S)Nr	   r	   )protocolr	   r
   <lambda>  s    zBStreamReaderTests.test_read_all_from_pipe_reader.<locals>.<lambda>pass_fdss   datarM   )ospipesys
executablery   openr   r   r   StreamReaderProtocolr   Zconnect_read_pipeZSafeChildWatcherZattach_loopZset_child_watcherZcreate_subprocess_execr   r   r   r   )r   codeZrfdZwfdargsr   r    Z	transport_Zwatchercreateprocr#   r	   )r   r
   test_read_all_from_pipe_reader  s*    	*	z0StreamReaderTests.test_read_all_from_pipe_readerc             C   sI   |  j  t j d   t j |  j  t j   } |  j | j |  j  d  S)N)
addCleanupr   r   r   r   r   r   )r   r    r	   r	   r
   test_streamreader_constructor  s    z/StreamReaderTests.test_streamreader_constructorc             C   sX   |  j  t j d   t j |  j  t j   } t j |  } |  j | j |  j  d  S)N)	r   r   r   r   r   Mockr   r   r   )r   r    r   r	   r	   r
   %test_streamreaderprotocol_constructor  s
    z7StreamReaderTests.test_streamreaderprotocol_constructorc          
      s   t  j       f d d   } t j  f d d    } t j d |  } | j d  | j     j   }  j	 t
 t f    j j | |    Wd  QRX| j   d  S)Nc           
      sm   t  j    }  |  T |  j d  |  j d  |  j   }   j |  |  j   \ } } | j   Wd  QRXd  S)N	localhostr   rL   )z	localhostr   )r   r   listenr   putacceptr   )r   r   Zcltr   )qr	   r
   r     s    z3StreamReaderTests.test_drain_raises.<locals>.serverc             3   sM   t  j |  | d   j Ed  H\ } } x  | j d  | j   Ed  Hq) Wd  S)Nr   s   foo
)r   r&   r   r   r   )r   r   r    r!   )r   r	   r
   r     s
     z3StreamReaderTests.test_drain_raises.<locals>.clienttargetT)queueQueuer   r   	threadingThread	setDaemonr   getr7   ConnectionResetErrorBrokenPipeErrorr   r   join)r   r   r   threadr   r	   )r   r   r
   test_drain_raises  s    

z#StreamReaderTests.test_drain_raisesc             C   s/   t  j d |  j  } |  j d t |   d  S)Nr   z<StreamReader>)r   r   r   r   repr)r   r   r	   r	   r
   test___repr__%  s    zStreamReaderTests.test___repr__c             C   s5   t  j d |  j d d  } |  j d t |   d  S)Nr   rS   {   z<StreamReader l=123>)r   r   r   r   r   )r   r   r	   r	   r
   test___repr__nondefault_limit)  s    z/StreamReaderTests.test___repr__nondefault_limitc             C   s9   t  j d |  j  } | j   |  j d t |   d  S)Nr   z<StreamReader eof>)r   r   r   rJ   r   r   )r   r   r	   r	   r
   test___repr__eof-  s    
z"StreamReaderTests.test___repr__eofc             C   s<   t  j d |  j  } | j d  |  j d t |   d  S)Nr   s   dataz<StreamReader 4 bytes>)r   r   r   r<   r   r   )r   r   r	   r	   r
   test___repr__data2  s    z#StreamReaderTests.test___repr__datac             C   sE   t  j d |  j  } t   } | j |  |  j d t |   d  S)Nr   z<StreamReader e=RuntimeError()>)r   r   r   r   rP   r   r   )r   r   r|   r	   r	   r
   test___repr__exception7  s    	z(StreamReaderTests.test___repr__exceptionc             C   s   t  j d |  j  } t  j d |  j  | _ |  j t |  d  | j j d   |  j j | j  d  | _ |  j	 d t |   d  S)Nr   z'<StreamReader w=<Future pending[\S ]*>>z<StreamReader>)
r   r   r   Futurer   ZassertRegexr   
set_resultr   r   )r   r   r	   r	   r
   test___repr__waiter=  s    		z%StreamReaderTests.test___repr__waiterc             C   s_   t  j d |  j  } t j   | _ t j   | j _ d | j j _ |  j d t	 |   d  S)Nr   z<Transport>z<StreamReader t=<Transport>>)
r   r   r   r   r   Z
_transport__repr__r   r   r   )r   r   r	   r	   r
   test___repr__transportH  s
    z(StreamReaderTests.test___repr__transport)Gr   r   r   r?   r   r   r   Zpatchr   r$   r*   unittestZ
skipUnlesshasattrr   r.   r/   ZskipIfr1   r3   r5   r8   r9   r:   r>   r@   rA   rG   rI   rK   rN   rR   rU   rW   rY   rZ   r[   r^   r`   ra   rb   rc   rd   rg   rh   rj   rp   rr   rt   rw   rx   rz   r{   r}   r   r   r   r   r   platformr   r   r   r   r   r   r   r   r   r   r   r	   r	   )r   r
   r      sv   '$
*	'	/#H'D''	
,r   __main__)__doc__r   r   r   r   r   r   r   r   r1   ImportErrorr   r   ZTestCaser   r   mainr	   r	   r	   r
   <module>   s(      ?