o
    >h;                     @   s  d Z ddlmZ ddlmZmZmZmZmZ ddl	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZmZmZmZmZmZ ddlmZmZ ddlmZ dd	l m!Z! dd
l"m#Z# ddl$m%Z% ddl&m'Z'm(Z( ddl)m*Z* ddl+m,Z,m-Z-m.Z.m/Z/m+Z+ ddl0m1Z1m2Z2 edZ3G dd de*Z4G dd de*Z5G dd de#Z6dededee3 de3fddZ7G dd  d e*Z8G d!d" d"e*Z9d#S )$z*
Tests for L{twisted.trial._dist.stream}.
    )Random)	AwaitableDictListTypeVarUnion)all_ofassert_thatcallingequal_to
has_lengthis_less_than_or_equal_toraises)given)binaryintegersjustlistsrandomstext)Deferredfail)	IProtocol)Protocol)AMP)Failure)FakeTransportconnect)SynchronousTestCase   )
StreamOpenStreamReceiverStreamWritechunkstream   )HasSumIsSequenceOfTc                   @   s   e Zd ZdZeeee e deee	  de
ddfddZee edd	ededdfd
dZee d	eddfddZdddZdS )StreamReceiverTestsz%
    Tests for L{StreamReceiver}
    streamsrandomreturnNc           	         s   t    fdd|D }|| tt||}| D ]\}}|D ]} || q"q||  fdd|D }t|tt| dS )z
        All data passed to L{StreamReceiver.write} is returned by a call to
        L{StreamReceiver.finish} with a matching C{streamId}.
        c                    s   g | ]}   qS  )open).0_receiverr.   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/trial/_dist/test/test_stream.py
<listcomp>.   s    z;StreamReceiverTests.test_streamReceived.<locals>.<listcomp>c                    s   i | ]}|  |qS r.   )finish)r0   streamIdr2   r.   r4   
<dictcomp>;   s    z;StreamReceiverTests.test_streamReceived.<locals>.<dictcomp>N)	r"   shuffledictzipitemswriter	   r   r   )	selfr+   r,   	streamIdsexpectedDatar7   stringss
actualDatar.   r2   r4   test_streamReceived'   s   

z'StreamReceiverTests.test_streamReceiveddatar7   c                 C   s&   t  }tt|j||tt dS )z
        L{StreamReceiver.write} raises L{KeyError} if called with a
        streamId not associated with an open stream.
        N)r"   r	   r
   r=   	with_argsr   KeyError)r>   r7   rE   r3   r.   r.   r4   test_writeBadStreamId?   s    z)StreamReceiverTests.test_writeBadStreamIdc                 C   s$   t  }tt|j|tt dS )z
        L{StreamReceiver.finish} raises L{KeyError} if called with a
        streamId not associated with an open stream.
        N)r"   r	   r
   r6   rF   r   rG   )r>   r7   r3   r.   r.   r4   test_badFinishStreamIdH   s   z*StreamReceiverTests.test_badFinishStreamIdc                 C   s6   t  }| }|| tt|j|tt dS )zI
        L{StreamReceiver.finish} removes the identified stream.
        N)r"   r/   r6   r	   r
   rF   r   rG   )r>   r3   r7   r.   r.   r4   test_finishRemovesStreamQ   s   
z,StreamReceiverTests.test_finishRemovesStream)r-   N)__name__
__module____qualname____doc__r   r   r   r   r   bytesr   rD   r   r   intstrrH   rI   rJ   r.   r.   r.   r4   r*   "   s     r*   c                   @   s,   e Zd ZdZee eddddd ZdS )
ChunkTestsz
    Tests for ``chunk``.
    r&   )	min_value)rE   	chunkSizec              	   C   s@   t t||}t|tttt|tt||dd  dS )z
        L{chunk} returns an iterable of L{str} where each element is no
        longer than the given limit.  The concatenation of the strings is also
        equal to the original input string.
        Nr   )	listr$   r	   r   r(   r   r   r'   r   )r>   rE   rT   chunksr.   r.   r4   
test_chunk`   s   
zChunkTests.test_chunkN)rK   rL   rM   rN   r   r   r   rW   r.   r.   r.   r4   rR   [   s    rR   c                	   @   sb   e Zd ZdZdeddfddZejdee	e
f fddZejd	ed
edee	e
f fddZdS )AMPStreamReceiverz6
    A simple AMP interface to L{StreamReceiver}.
    r+   r-   Nc                 C   s
   || _ d S N)r+   )r>   r+   r.   r.   r4   __init__x   s   
zAMPStreamReceiver.__init__c                 C   s   d| j  iS )Nr7   )r+   r/   r>   r.   r.   r4   
streamOpen{   s   zAMPStreamReceiver.streamOpenr7   rE   c                 C   s   | j || i S rY   )r+   r=   )r>   r7   rE   r.   r.   r4   streamWrite   s   zAMPStreamReceiver.streamWrite)rK   rL   rM   rN   r"   rZ   r!   	responderr   rQ   objectr\   r#   rP   rO   r]   r.   r.   r.   r4   rX   s   s    $rX   serverclientinteractionr-   c                    s   d dt ffdd}dttt f ddf fdd}t| t| d	d
|t|dd
}t| }|| |   rFt	trD
  S td)zO
    Let C{server} and C{client} exchange bytes while C{interaction} runs.
    Fr-   c                      s    I d H S rY   r.   r.   )rb   r.   r4   to_coroutine   s   
zinteract.<locals>.to_coroutinerNc                    s   d | d S )NTr.   )rd   )finishedresultr.   r4   collect_result   s   z interact.<locals>.collect_resultT)isServerz'Interaction failed to produce a result.)r)   r   r   r   r   r   fromCoroutineaddBothflush
isinstanceraiseException	Exception)r`   ra   rb   rc   rg   pumpinteractingr.   )re   rb   rf   r4   interact   s"    



rq   c                   @   s    e Zd ZdZdd Zdd ZdS )InteractTestsz0
    Tests for the test helper L{interact}.
    c                 C   sV   G dd dt }| | tt t t|  W d   dS 1 s$w   Y  dS )zg
        If the interaction results in a failure then L{interact} raises an
        exception.
        c                   @   s   e Zd ZdS )z6InteractTests.test_failure.<locals>.ArbitraryExceptionN)rK   rL   rM   r.   r.   r.   r4   ArbitraryException   s    rs   N)rn   assertRaisesrq   r   r   )r>   rs   r.   r.   r4   test_failure   s   "zInteractTests.test_failurec                 C   sB   |  t tt t t  W d   dS 1 sw   Y  dS )zl
        If the interaction fails to produce a result then L{interact} raises
        an exception.
        N)rt   rn   rq   r   r   r[   r.   r.   r4   test_incomplete   s   "zInteractTests.test_incompleteN)rK   rL   rM   rN   ru   rv   r.   r.   r.   r4   rr      s    rr   c                   @   s4   e Zd ZdZeee dee ddfddZ	dS )StreamTestsz
    Tests for L{stream}.
    rV   r-   Nc                 C   sB   t  }t }tt||t|t|}t||tt	| dS )z
        All of the chunks passed to L{stream} are sent in order over a
        stream using the given AMP connection.
        N)
r   r"   rq   rX   r%   iterr	   r6   r   r   )r>   rV   senderr+   r7   r.   r.   r4   test_stream   s   zStreamTests.test_stream)
rK   rL   rM   rN   r   r   r   r   rO   rz   r.   r.   r.   r4   rw      s    rw   N):rN   r,   r   typingr   r   r   r   r   hamcrestr   r	   r
   r   r   r   r   r   
hypothesisr   hypothesis.strategiesr   r   r   r   r   r   twisted.internet.deferr   r   twisted.internet.interfacesr   twisted.internet.protocolr   twisted.protocols.ampr   twisted.python.failurer   twisted.test.iosimr   r   twisted.trial.unittestr   r%   r!   r"   r#   r$   matchersr'   r(   r)   r*   rR   rX   rq   rr   rw   r.   r.   r.   r4   <module>   s,    (
 9!