o
    >h!                     @  s   d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZmZmZ dd	lmZ ed
ZG dd deZG dd deZG dd dZG dd de
e ZG dd dZG dd deZG dd deZdS )z.
Tests for L{twisted._threads._threadworker}.
    )annotationsN)ThreadErrorlocal)CallableGenericTypeVar)SynchronousTestCase   )AlreadyQuit
LockWorkerThreadWorker)
SimpleLockTc                   @     e Zd ZdZdS )FakeQueueEmptyz8
    L{FakeQueue}'s C{get} has exhausted the queue.
    N__name__
__module____qualname____doc__ r   r   /var/www/vedio/testing/chatpythonscript.ninositsolution.com/env/lib/python3.10/site-packages/twisted/_threads/test/test_threadworker.pyr          r   c                   @  r   )WouldDeadlockzf
    If this were a real lock, you'd be deadlocked because the lock would be
    double-acquired.
    Nr   r   r   r   r   r      r   r   c                   @  s$   e Zd ZdZdddZddd	Zd
S )
FakeThreadz
    A fake L{threading.Thread}.

    @ivar target: A target function to run.

    @ivar started: Has this thread been started?
    @type started: L{bool}
    targetCallable[[], object]returnNonec                 C  s   || _ d| _dS )z7
        Create a L{FakeThread} with a target.
        FN)r   started)selfr   r   r   r   __init__-   s   
zFakeThread.__init__c                 C  
   d| _ dS )z)
        Set the "started" flag.
        TN)r   r    r   r   r   start4      
zFakeThread.startN)r   r   r   r   r   r   )r   r   r   r   r!   r$   r   r   r   r   r   #   s    
	r   c                   @  s.   e Zd ZdZdddZddd	Zdd
dZdS )	FakeQueuez
    A fake L{Queue} implementing C{put} and C{get}.

    @ivar items: A lit of items placed by C{put} but not yet retrieved by
        C{get}.
    @type items: L{list}
    r   r   c                 C  s
   g | _ dS )z(
        Create a L{FakeQueue}.
        N)itemsr#   r   r   r   r!   D   r%   zFakeQueue.__init__itemr   c                 C  s   | j | dS )zv
        Put an item into the queue for later retrieval by L{FakeQueue.get}.

        @param item: any object
        N)r(   append)r    r)   r   r   r   putJ   s   zFakeQueue.putc                 C  s   | j st | j dS )zR
        Get an item.

        @return: an item previously put by C{put}.
        r   )r(   r   popr#   r   r   r   getR   s   zFakeQueue.getNr&   )r)   r   r   r   )r   r   )r   r   r   r   r!   r+   r-   r   r   r   r   r'   ;   s
    

r'   c                   @  s.   e Zd ZdZdddZdddZdd	d
ZdS )FakeLockzi
    A stand-in for L{threading.Lock}.

    @ivar acquired: Whether this lock is presently acquired.
    r   r   c                 C  r"   )z9
        Create a lock in the un-acquired state.
        FNacquiredr#   r   r   r   r!   d   r%   zFakeLock.__init__boolc                 C  s   | j rt d| _ dS )zX
        Acquire the lock.  Raise an exception if the lock is already acquired.
        T)r0   r   r#   r   r   r   acquirej   s   zFakeLock.acquirec                 C  s   | j st d| _ dS )zf
        Release the lock.  Raise an exception if the lock is not presently
        acquired.
        FN)r0   r   r#   r   r   r   releases   s   
zFakeLock.releaseNr&   )r   r1   )r   r   r   r   r!   r2   r3   r   r   r   r   r.   ]   s
    

	r.   c                   @  s.   e Zd ZdZdddZdddZddd	Zd
S )ThreadWorkerTestsz$
    Tests for L{ThreadWorker}.
    r   r   c                   s&   g  _ d fdd}t|t  _dS )	z4
        Create a worker with fake threads.
        r   r   r   r   c                   s"   t | d}|   j| |S )N)r   )r   r$   fakeThreadsr*   )r   	newThreadr#   r   r   startThread   s   
z,ThreadWorkerTests.setUp.<locals>.startThreadN)r   r   r   r   )r5   r   r'   worker)r    r7   r   r#   r   setUp   s   zThreadWorkerTests.setUpc                   st   |  t| jd |  | jd jd d d
 fdd}| j| |   d | t| jd j |   d d	S )z
        L{ThreadWorker} calls its C{createThread} callable to create a thread,
        its C{createQueue} callable to create a queue, and then the thread's
        target pulls work from that queue.
           r   TFr   r   c                     s   d d S NTr   r   doner   r   doIt   s   z@ThreadWorkerTests.test_startsThreadAndPerformsWork.<locals>.doItNr&   )	assertEquallenr5   r   r8   doassertRaisesr   r   )r    r>   r   r<   r    test_startsThreadAndPerformsWork   s   z2ThreadWorkerTests.test_startsThreadAndPerformsWorkc                 C  s0   | j   | t| j j | t| j jt dS )z
        L{ThreadWorker.quit} causes future calls to L{ThreadWorker.do} and
        L{ThreadWorker.quit} to raise L{AlreadyQuit}.
        N)r8   quitrB   r
   rA   listr#   r   r   r   test_quitPreventsFutureCalls   s   
z.ThreadWorkerTests.test_quitPreventsFutureCallsNr&   )r   r   r   r   r9   rC   rF   r   r   r   r   r4   }   s
    

r4   c                   @  sV   e Zd ZdZdddZdddZddd	Zdd
dZdddZdddZ	dddZ
dS )LockWorkerTestsz"
    Tests for L{LockWorker}.
    r   r   c                 C  s    t  }|  | t|j dS )ze
        The L{FakeLock} test fixture will alert us if there's a potential
        deadlock.
        N)r.   r2   rB   r   )r    lockr   r   r   test_fakeDeadlock   s   z!LockWorkerTests.test_fakeDeadlockc                 C  sB   t  }| t|j |  | }| d| | t|j dS )zk
        The L{FakeLock} test fixture will alert us if there's a potential
        double-release.
        N)r.   rB   r   r3   r2   assertIs)r    rH   noResultr   r   r   test_fakeDoubleRelease   s   z&LockWorkerTests.test_fakeDoubleReleasec                   sd   t  }t t|}dd d fdd}|| | d |  d | jd dS )	zp
        L{LockWorker.do} immediately performs the work it's given, while the
        lock is acquired.
        Fr   r   c                     s   dj  d S r;   r/   r   r0   r=   rH   r   r   work      
z@LockWorkerTests.test_doExecutesImmediatelyWithLock.<locals>.workTNr&   )r   r.   r   rA   r?   r0   )r    storager8   rN   r   rM   r   "test_doExecutesImmediatelyWithLock   s   

z2LockWorkerTests.test_doExecutesImmediatelyWithLockc                   sd   t  tt g g  dd	 fdd | ddg |  ddg dS )
z
        If L{LockWorker.do} is called recursively, it postpones the inner call
        until the outer one is complete.
        r   r   r   c                     s@   d7     j tdk r d8 d S )Nr:   r	   )r*   r0   r@   rA   r   r0   levellevelsrH   rN   r8   r   r   rN      s   

z6LockWorkerTests.test_doUnwindsReentrancy.<locals>.workr:   TNr&   )r.   r   r   rA   r?   r#   r   rR   r   test_doUnwindsReentrancy   s   
	z(LockWorkerTests.test_doUnwindsReentrancyc                 C  sl   t  }t|}t|t }~| | d |  t  | 	| d | 
t|j | 
t|jt dS )z
        L{LockWorker.quit} frees the resources associated with its lock and
        causes further calls to C{do} and C{quit} to fail.
        N)r.   weakrefrefr   r   assertIsNotrD   gccollectrJ   rB   r
   rA   rE   )r    rH   rW   r8   r   r   r   	test_quit   s   
zLockWorkerTests.test_quitc                   s   t   t }t t dddd
fdd}d
 fdd| d	 d	  jd  t	  
| d dS )z
        If L{LockWorker.quit} is invoked during a call to L{LockWorker.do}, all
        recursive work scheduled with L{LockWorker.do} will be completed and
        the lock will be released.
        FNr   r   c                     s*       tj t d d S r;   )rA   rD   rB   r
   rE   r   )phase1completephase2r    r8   r   r   phase1  s   
z5LockWorkerTests.test_quitWhileWorking.<locals>.phase1c                     s   d j d S r;   r/   r   )rH   phase2acquiredphase2completer   r   r]     rO   z5LockWorkerTests.test_quitWhileWorking.<locals>.phase2Tr&   )r.   rV   rW   r   r   rA   r?   r0   rY   rZ   rJ   )r    rW   r^   r   )rH   r\   r]   r_   r`   r    r8   r   test_quitWhileWorking  s   

z%LockWorkerTests.test_quitWhileWorkingc                 C  s2   G dd dt }|t t }| t|jt dS )z
        If L{LockWorker.do} is called concurrently with L{LockWorker.quit}, and
        C{quit} wins the race before C{do} gets the lock attribute, then
        L{AlreadyQuit} will be raised.
        c                   @  s*   e Zd Zed	ddZejd
ddZdS )zALockWorkerTests.test_quitWhileGettingLock.<locals>.RacyLockWorkerr   SimpleLock | Nonec                 S  s   |    | jd }|S N_lock)rD   __dict__)r    itr   r   r   rd   /  s   
zGLockWorkerTests.test_quitWhileGettingLock.<locals>.RacyLockWorker._lockvaluer   c                 S  s   || j d< d S rc   )re   )r    rg   r   r   r   rd   5  s   N)r   rb   )rg   rb   r   r   )r   r   r   propertyrd   setterr   r   r   r   RacyLockWorker.  s
    rj   N)r   r.   r   rB   r
   rA   rE   )r    rj   r8   r   r   r   test_quitWhileGettingLock'  s   z)LockWorkerTests.test_quitWhileGettingLockNr&   )r   r   r   r   rI   rL   rQ   rU   r[   ra   rk   r   r   r   r   rG      s    

	



"rG   )r   
__future__r   rY   rV   	threadingr   r   typingr   r   r   twisted.trial.unittestr    r
   r   r   _threadworkerr   r   	Exceptionr   r   r   r'   r.   r4   rG   r   r   r   r   <module>   s"   " 1