
    Vh1                         d Z 	 ddl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 ddlmZmZ  G d	 d
      Z G d de
      Z e       j'                  ej)                                y# e$ r dZY iw xY w)z6
Tests for implementations of L{IReactorWin32Events}.
    N)verifyObject)Deferred)IReactorWin32Events)ReactorBuilder)Failure)getThreadIDisInIOThreadc                   D    e Zd ZdZdZdxZxZZd Zd Z	d Z
d Zd Zd	 Zy)
Listenera  
    L{Listener} is an object that can be added to a L{IReactorWin32Events}
    reactor to receive callback notification when a Windows event is set.  It
    records what thread its callback is invoked in and fires a Deferred.

    @ivar success: A flag which is set to C{True} when the event callback is
        called.

    @ivar logThreadID: The id of the thread in which the C{logPrefix} method is
        called.

    @ivar eventThreadID: The id of the thread in which the event callback is
        called.

    @ivar connLostThreadID: The id of the thread in which the C{connectionLost}
        method is called.

    @ivar _finished: The L{Deferred} which will be fired when the event callback
        is called.
    FNc                     || _         y N)	_finished)selffinisheds     V/home/dcms/DCMS/lib/python3.12/site-packages/twisted/internet/test/test_win32events.py__init__zListener.__init__/   s	    !    c                 "    t               | _        y)Nr   )r   logThreadIDr   s    r   	logPrefixzListener.logPrefix2   s    &=r   c                 f    d| _         t               | _        | j                  j	                  d        y )NT)successr   eventThreadIDr   callbackr   s    r   occurredzListener.occurred6   s%    (]%r   c                     t        d      )NzSome problem)RuntimeErrorr   s    r   brokenOccurredzListener.brokenOccurred;   s    >**r   c                     t        d      S )NzEntirely different problem)EnvironmentErrorr   s    r   returnValueOccurredzListener.returnValueOccurred>   s     <==r   c                 X    t               | _        | j                  j                  |       y r   )r   connLostThreadIDr   errback)r   reasons     r   connectionLostzListener.connectionLostA   s     +v&r   )__name__
__module____qualname____doc__r   r   r   r$   r   r   r   r   r"   r'    r   r   r   r      s<    * G599K9-"2"&
+>'r   r   c                   :    e Zd ZdZegZd Zd Zd Zd Z	d Z
d Zy)	Win32EventsTestsBuilderzD
    Builder defining tests relating to L{IReactorWin32Events}.
    c                 D    | j                         }t        t        |       y)zn
        An instance of the reactor has all of the methods defined on
        L{IReactorWin32Events}.
        N)buildReactorr   r   )r   reactors     r   test_interfacez&Win32EventsTestsBuilder.test_interfaceM   s    
 ##%('2r   c                    t               }| j                         t        j                  dddd      }t	               }|j                  fd       t        |      }j                  ||d       j                  t        j                  |       | j                         | j                  |j                         | j                  ||j                         | j                  ||j                         y)z
        When an event which has been added to the reactor is set, the action
        associated with the event is invoked in the reactor thread.
        NFc                 $    j                         S r   stopignoredr1   s    r   <lambda>z7Win32EventsTestsBuilder.test_addEvent.<locals>.<lambda>^   s    W\\^ r   r   )r   r0   
win32eventCreateEventr   addCallbackr   addEventcallWhenRunningSetEvent
runReactor
assertTruer   assertEqualr   r   )r   reactorThreadIDeventr   listenerr1   s        @r   test_addEventz%Win32EventsTestsBuilder.test_addEventU   s    
 &-##%&&tUE4@:;<H%*5
 3 3U; (()(*>*>?(*@*@Ar   c                    g fd}| j                         t        j                  dddd      }t               }t	        |      }|j                  |       j                  ||d       j                  t        j                  |       | j                         | j                  |j                         | j                  dg       y)z{
        Using L{IReactorWin32Events.addEvent} does not change which thread is
        reported as the I/O thread.
        c                 X    j                  t                      j                          y r   )appendr	   r6   )r8   r1   resultss    r   checkzAWin32EventsTestsBuilder.test_ioThreadDoesNotChange.<locals>.checkn   s    NN<>*LLNr   NFr   T)r0   r:   r;   r   r   r<   r=   r>   r?   r@   rA   r   rB   )r   rK   rD   r   rE   r1   rJ   s        @@r   test_ioThreadDoesNotChangez2Win32EventsTestsBuilder.test_ioThreadDoesNotChangeg   s    
 	 ##%&&tUE4@:H%U#*5
 3 3U; (()$)r   c                 t   t               }| j                         t        j                  dddd      }g }t	               }|j                  |j                         |j                  fd       t        |      }j                  ||d       j                  t        j                  |       | j                         | j                  |d   t               |d   j                  t               | j!                  ||j"                         | j!                  dt%        | j'                  t                           y)z
        If the event handler raises an exception, the event is removed from the
        reactor and the handler's C{connectionLost} method is called in the I/O
        thread and the exception is logged.
        NFc                 $    j                         S r   r5   r7   s    r   r9   zBWin32EventsTestsBuilder.test_disconnectedOnError.<locals>.<lambda>        r   r   r      )r   r0   r:   r;   r   addBothrI   r   r=   r>   r?   r@   assertIsInstancer   trapr   rB   r$   lenflushLoggedErrorsr   rC   rD   resultr   rE   r1   s         @r   test_disconnectedOnErrorz0Win32EventsTestsBuilder.test_disconnectedOnError}   s     &-##%&&tUE4@:'78H%*:;
 3 3U; fQi1q	|$(*C*CDC 6 6| DEFr   c                    t               }| j                         t        j                  dddd      }g }t	               }|j                  |j                         |j                  fd       t        |      }j                  ||d       j                  t        j                  |       | j                         | j                  |d   t               |d   j                  t               | j!                  ||j"                         y)z
        If the event handler returns a value, the event is removed from the
        reactor and the handler's C{connectionLost} method is called in the I/O
        thread.
        NFc                 $    j                         S r   r5   r7   s    r   r9   zFWin32EventsTestsBuilder.test_disconnectOnReturnValue.<locals>.<lambda>   rO   r   r"   r   )r   r0   r:   r;   r   rQ   rI   r   r=   r>   r?   r@   rR   r   rS   r!   rB   r$   rV   s         @r   test_disconnectOnReturnValuez4Win32EventsTestsBuilder.test_disconnectOnReturnValue   s     &-##%&&tUE4@:'78H%*?@
 3 3U; fQi1q	'((*C*CDr   c                 2   | j                         }t        j                  dddd      }t               }t	        |      }|j                  ||d       |j                  |j                         | j                  |       | j                  |j                         y)z
        Event handlers added with L{IReactorWin32Events.addEvent} do not have
        C{connectionLost} called on them if they are still active when the
        reactor shuts down.
        NFr   )r0   r:   r;   r   r   r=   r>   r6   r@   assertIsNoner$   )r   r1   rD   r   rE   s        r   test_notDisconnectedOnShutdownz6Win32EventsTestsBuilder.test_notDisconnectedOnShutdown   s}     ##%&&tUE4@:H%*5- (334r   N)r(   r)   r*   r+   r   requiredInterfacesr2   rF   rL   rX   r[   r^   r,   r   r   r.   r.   F   s3     ..3B$*,G4E25r   r.   )r+   r:   ImportErrorzope.interface.verifyr   twisted.internet.deferr   twisted.internet.interfacesr   #twisted.internet.test.reactormixinsr   twisted.python.failurer   twisted.python.threadabler   r	   r   r.   globalsupdatemakeTestCaseClassesr,   r   r   <module>rj      ss    / + ; > * ?-' -'`w5n w5t 	  (<<> ?m  Js   A( (A21A2