
    Vh;                        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*      Z9y")#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                       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y)StreamReceiverTestsz%
    Tests for L{StreamReceiver}
    streamsrandomreturnNc                    t               }|D cg c]  }|j                          }}|j                  |       t        t	        ||            }|j                         D ]  \  }}|D ]  }	|j                  ||	          |j                  |       |D ci c]  }||j                  |       }
}t        |
t        t        |                   yc c}w c c}w )z
        All data passed to L{StreamReceiver.write} is returned by a call to
        L{StreamReceiver.finish} with a matching C{streamId}.
        N)r#   openshuffledictzipitemswritefinishr
   r   r   )selfr-   r.   receiver_	streamIdsexpectedDatastreamIdstringss
actualDatas              T/home/dcms/DCMS/lib/python3.12/site-packages/twisted/trial/_dist/test/test_stream.pytest_streamReceivedz'StreamReceiverTests.test_streamReceived'   s     "#.56X]]_6	6 	y!C	734!-!3!3!5 	,Hg ,x+,	,
 	y!JSThh 99T
TJH\$: ;< 7 Us   CCdatar=   c                     t               }t        t        |j                        j	                  ||      t        t                     y)z
        L{StreamReceiver.write} raises L{KeyError} if called with a
        streamId not associated with an open stream.
        N)r#   r
   r   r6   	with_argsr   KeyError)r8   r=   rC   r9   s       rA   test_writeBadStreamIdz)StreamReceiverTests.test_writeBadStreamId?   s1     "#GHNN+55hEvhGWX    c                     t               }t        t        |j                        j	                  |      t        t                     y)z
        L{StreamReceiver.finish} raises L{KeyError} if called with a
        streamId not associated with an open stream.
        N)r#   r
   r   r7   rE   r   rF   )r8   r=   r9   s      rA   test_badFinishStreamIdz*StreamReceiverTests.test_badFinishStreamIdH   s/     "#GHOO,66x@&BRSrH   c                     t               }|j                         }|j                  |       t        t	        |j                        j                  |      t        t                     y)zI
        L{StreamReceiver.finish} removes the identified stream.
        N)r#   r1   r7   r
   r   rE   r   rF   )r8   r9   r=   s      rA   test_finishRemovesStreamz,StreamReceiverTests.test_finishRemovesStreamQ   sF     "#==?!GHOO,66x@&BRSrH   )r/   N)__name__
__module____qualname____doc__r   r   r   r   r   bytesr   rB   r   r   intstrrG   rJ   rL    rH   rA   r,   r,   "   s     5vx!79-=4U+< =f =QU = .=. 8:tF|$Yc Y Y Y %Y 8:Ts Tt T TTrH   r,   c                   H    e Zd ZdZ e e        ed            d        Zy)
ChunkTestsz
    Tests for ``chunk``.
    r'   )	min_value)rC   	chunkSizec                     t        t        ||            }t        |t        t	        t        t        |                  t        t        |      |dd                    y)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   )r8   rC   rX   chunkss       rA   
test_chunkzChunkTests.test_chunk`   sT     eD),-4Y?@ x~tBQx0		
rH   N)rM   rN   rO   rP   r   r   r   r\   rT   rH   rA   rV   rV   [   s)     (Q"78
 9
rH   rV   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y)
AMPStreamReceiverz6
    A simple AMP interface to L{StreamReceiver}.
    r-   r/   Nc                     || _         y N)r-   )r8   r-   s     rA   __init__zAMPStreamReceiver.__init__x   s	    rH   c                 :    d| j                   j                         iS )Nr=   )r-   r1   r8   s    rA   
streamOpenzAMPStreamReceiver.streamOpen{   s    DLL--/00rH   r=   rC   c                 >    | j                   j                  ||       i S r`   )r-   r6   )r8   r=   rC   s      rA   streamWritezAMPStreamReceiver.streamWrite   s    8T*	rH   )rM   rN   rO   rP   r#   ra   r"   	responderr   rS   objectrd   r$   rR   rQ   rf   rT   rH   rA   r^   r^   s   s|     4  1Df- 1 1 C u c6k9J  rH   r^   serverclientinteractionr/   c           	      x   ddt         ffd}dt        t        t         f   ddffd}t        | t	        | d      |t	        |d            }t        j                   |             }|j                  |       |j                          r"t        t              rj                          S t        d	      )
zO
    Let C{server} and C{client} exchange bytes while C{interaction} runs.
    Fr/   c                  $   K     d {   S 7 wr`   rT   )rk   s   rA   to_coroutinezinteract.<locals>.to_coroutine   s         s   rNc                     d| y )NTrT   )ro   finishedresults    rA   collect_resultz interact.<locals>.collect_result   s    rH   T)isServerz'Interaction failed to produce a result.)r*   r   r   r   r   r   fromCoroutineaddBothflush
isinstanceraiseException	Exception)	ri   rj   rk   rn   rs   pumpinteractingrq   rr   s	     `    @@rA   interactr}      s     H! !%
+  
 ft,fu-	D ((8K'JJLfg&!!#
=
>>rH   c                       e Zd ZdZd Zd Zy)InteractTestsz0
    Tests for the test helper L{interact}.
    c           	           G d dt               }| j                  |      5  t        t               t               t	         |                    ddd       y# 1 sw Y   yxY w)zg
        If the interaction results in a failure then L{interact} raises an
        exception.
        c                       e Zd Zy)6InteractTests.test_failure.<locals>.ArbitraryExceptionN)rM   rN   rO   rT   rH   rA   ArbitraryExceptionr      s    rH   r   N)rz   assertRaisesr}   r   r   )r8   r   s     rA   test_failurezInteractTests.test_failure   sQ    	 	 12 	IXZT2D2F-GH	I 	I 	Is   ,AAc                     | j                  t              5  t        t               t               t	                      ddd       y# 1 sw Y   yxY w)zl
        If the interaction fails to produce a result then L{interact} raises
        an exception.
        N)r   rz   r}   r   r   rc   s    rA   test_incompletezInteractTests.test_incomplete   s9    
 y) 	9XZXZ8	9 	9 	9s   &AAN)rM   rN   rO   rP   r   r   rT   rH   rA   r   r      s    
I9rH   r   c                   R    e Zd ZdZ e e e                   dee   ddfd       Z	y)StreamTestsz
    Tests for L{stream}.
    r[   r/   Nc           
          t               }t               }t        t        |      |t	        |t        |                  }t        |j                  |      t        t        |                   y)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}   r^   r&   iterr
   r7   r   r   )r8   r[   senderr-   r=   s        rA   test_streamzStreamTests.test_stream   sS      "g&vtF|0L
 	GNN8,c(62B.CDrH   )
rM   rN   rO   rP   r   r   r   r   rQ   r   rT   rH   rA   r   r      s<     5?
E$u+ 
E$ 
E 
ErH   r   N):rP   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,   rV   r^   r}   r   r   rT   rH   rA   <module>r      s     8 8	 	 	  N N 1 1 . % * 5 6 K K *CL6T- 6Tr
$ 
0 $?Y ?	 ?	! ?QR ?B9' 94E% ErH   