
    Yf;                        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          Z3 G d de*          Z4 G d de*          Z5 G d de#          Z6dededee3         de3fdZ7 G d de*          Z8 G 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                   J   e Zd ZdZ e e e e                                 e                      deee	                  de
ddfd            Z e e             ed                    dededdfd	            Z e e                      deddfd
            ZddZdS )StreamReceiverTestsz%
    Tests for L{StreamReceiver}
    streamsrandomreturnNc                   	 t                      		fd|D             }|                    |           t          t          ||                    }|                                D ] \  }}|D ]}	                    ||           !|                    |           	f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                 8    g | ]}                                 S  )open).0_receivers     V/var/www/html/env/lib/python3.11/site-packages/twisted/trial/_dist/test/test_stream.py
<listcomp>z;StreamReceiverTests.test_streamReceived.<locals>.<listcomp>.   s!    666X]]__666    c                 <    i | ]}|                     |          S r2   )finish)r4   streamIdr6   s     r7   
<dictcomp>z;StreamReceiverTests.test_streamReceived.<locals>.<dictcomp>;   s'    TTThh 9 9TTTr9   N)	r#   shuffledictzipitemswriter
   r   r   )
selfr-   r.   	streamIdsexpectedDatar<   stringss
actualDatar6   s
            @r7   test_streamReceivedz'StreamReceiverTests.test_streamReceived'   s     "##6666g666	 	y!!!C	73344!-!3!3!5!5 	, 	,Hg , ,x++++, 	y!!!TTTT)TTT
JH\$:$: ; ;<<<<<r9   datar<   c                     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   rB   	with_argsr   KeyError)rC   r<   rJ   r6   s       r7   test_writeBadStreamIdz)StreamReceiverTests.test_writeBadStreamId?   sG     "##GHN++55hEEvhGWGWXXXXXr9   c                     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   r;   rL   r   rM   )rC   r<   r6   s      r7   test_badFinishStreamIdz*StreamReceiverTests.test_badFinishStreamIdH   sE     "##GHO,,66x@@&BRBRSSSSSr9   c                    t                      }|                                }|                    |           t          t	          |j                                      |          t          t                               dS )zI
        L{StreamReceiver.finish} removes the identified stream.
        N)r#   r3   r;   r
   r   rL   r   rM   )rC   r6   r<   s      r7   test_finishRemovesStreamz,StreamReceiverTests.test_finishRemovesStreamQ   sd     "##==??!!!GHO,,66x@@&BRBRSSSSSr9   )r/   N)__name__
__module____qualname____doc__r   r   r   r   r   bytesr   rI   r   r   intstrrN   rP   rR   r2   r9   r7   r,   r,   "   s>         U55vvxx!!7799--=4U+< =f =QU = = = .-=. U88::ttF||$$Yc Y Y Y Y Y %$Y U88::Ts Tt T T T TT T T T T Tr9   r,   c                   b    e Zd ZdZ e e             ed                    d             ZdS )
ChunkTestsz
    Tests for ``chunk``.
    r'   )	min_value)rJ   	chunkSizec                    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   )rC   rJ   r]   chunkss       r7   
test_chunkzChunkTests.test_chunk`   s     eD),,--4Y??@@  x~~tBQBx00	 	
 	
 	
 	
 	
r9   N)rS   rT   rU   rV   r   r   r   ra   r2   r9   r7   r[   r[   [   s\          U((Q"7"7"7888
 
 98
 
 
r9   r[   c            	           e Zd ZdZdeddfdZej        dee	e
f         fd            Zej        dededee	e
f         fd	            ZdS )
AMPStreamReceiverz6
    A simple AMP interface to L{StreamReceiver}.
    r-   r/   Nc                     || _         d S N)r-   )rC   r-   s     r7   __init__zAMPStreamReceiver.__init__x   s    r9   c                 8    d| j                                         iS )Nr<   )r-   r3   rC   s    r7   
streamOpenzAMPStreamReceiver.streamOpen{   s    DL--//00r9   r<   rJ   c                 <    | j                             ||           i S re   )r-   rB   )rC   r<   rJ   s      r7   streamWritezAMPStreamReceiver.streamWrite   s    8T***	r9   )rS   rT   rU   rV   r#   rf   r"   	responderr   rY   objectri   r$   rX   rW   rk   r2   r9   r7   rc   rc   s   s          4     1Df- 1 1 1 1 C u c6k9J      r9   rc   serverclientinteractionr/   c           	         ddt           ffd}dt          t          t           f         ddffd}t          | t	          | d          |t	          |d                    }t          j         |                      }|                    |           |                                 r+t          t                    r
                                 S t          d	          )
zO
    Let C{server} and C{client} exchange bytes while C{interaction} runs.
    Fr/   c                     K     d {V S re   r2   )rp   s   r7   to_coroutinezinteract.<locals>.to_coroutine   s              r9   rNc                     d| d S )NTr2   )rt   finishedresults    r7   collect_resultz interact.<locals>.collect_result   s    r9   T)isServerz'Interaction failed to produce a result.)r*   r   r   r   r   r   fromCoroutineaddBothflush
isinstanceraiseException	Exception)	rn   ro   rp   rs   rx   pumpinteractingrv   rw   s	     `    @@r7   interactr      s    H! ! ! ! ! ! !%
+        
 ft,,,fu---	 D (88K'''JJLLL fg&& 	$!!###
=
>
>>r9   c                       e Zd ZdZd Zd ZdS )InteractTestsz0
    Tests for the test helper L{interact}.
    c           	          G d dt                     }|                     |          5  t          t                      t                      t	           |                                 ddd           dS # 1 swxY w Y   dS )zg
        If the interaction results in a failure then L{interact} raises an
        exception.
        c                       e Zd ZdS )6InteractTests.test_failure.<locals>.ArbitraryExceptionN)rS   rT   rU   r2   r9   r7   ArbitraryExceptionr      s        Dr9   r   N)r   assertRaisesr   r   r   )rC   r   s     r7   test_failurezInteractTests.test_failure   s    	 	 	 	 	 	 	 	 122 	I 	IXZZT2D2D2F2F-G-GHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Is   ?A55A9<A9c                     |                      t                    5  t          t                      t                      t	                                 ddd           dS # 1 swxY w Y   dS )zl
        If the interaction fails to produce a result then L{interact} raises
        an exception.
        N)r   r   r   r   r   rh   s    r7   test_incompletezInteractTests.test_incomplete   s    
 y)) 	9 	9XZZXZZ888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9s   6AA"%A"N)rS   rT   rU   rV   r   r   r2   r9   r7   r   r      s?         
I 
I 
I9 9 9 9 9r9   r   c                   r    e Zd ZdZ e e e                                dee         ddfd            Z	dS )StreamTestsz
    Tests for L{stream}.
    r`   r/   Nc           
      *   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#   r   rc   r&   iterr
   r;   r   r   )rC   r`   senderr-   r<   s        r7   test_streamzStreamTests.test_stream   sz      ""g&&vtF||0L0L
 
 	GNN8,,c(62B2B.C.CDDDDDr9   )
rS   rT   rU   rV   r   r   r   r   rW   r   r2   r9   r7   r   r      sq          U55??
E$u+ 
E$ 
E 
E 
E 
E 
E 
Er9   r   N):rV   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,   r[   rc   r   r   r   r2   r9   r7   <module>r      s          8 8 8 8 8 8 8 8 8 8 8 8 8 8	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	       N N N N N N N N N N N N N N N N 1 1 1 1 1 1 1 1 1 1 1 1 1 1 . . . . . . % % % % % % * * * * * * 5 5 5 5 5 5 5 5 6 6 6 6 6 6 K K K K K K K K K K K K K K * * * * * * * *GCLL6T 6T 6T 6T 6T- 6T 6T 6Tr
 
 
 
 
$ 
 
 
0       $?Y ?	 ?	! ?QR ? ? ? ?B9 9 9 9 9' 9 9 94E E E E E% E E E E Er9   