o
    ¯b
  ã                   @   s    d Z ddlZddlZddlmZ zddlZW n ey!   dZY nw dZddlm	Z	 ddl
mZmZ G dd	„ d	ƒZe	 e¡ G d
d„ deƒZG dd„ deƒZdS )z)
Tests for L{twisted.python.threadable}.
é    N)ÚskipIfTF)Ú
threadable)ÚFailTestÚSynchronousTestCasec                   @   s"   e Zd ZdgZdZdZdd„ ZdS )Ú
TestObjectÚaMethodéÿÿÿÿé   c                 C   sL   t dƒD ]}| j| j| _| _| j| j | _| jdks#J d| jf ƒ‚qd S )Né
   r   zz == %d, not 0 as expected)ÚrangeÚyÚxÚz)ÚselfÚi© r   ú>/usr/lib/python3/dist-packages/twisted/test/test_threadable.pyr      s
   ýzTestObject.aMethodN)Ú__name__Ú
__module__Ú__qualname__Úsynchronizedr   r   r   r   r   r   r   r      s
    r   c                   @   sH   e Zd Zdd„ Zdd„ Zeedƒdd„ ƒZeedƒdd	„ ƒZd
d„ Z	dS )ÚSynchronizationTestsc                 C   s    |   tjt ¡ ¡ t d¡ dS )zì
        Reduce the CPython check interval so that thread switches happen much
        more often, hopefully exercising more possible race conditions.  Also,
        delay actual test startup until the reactor has been started.
        gH¯¼šò×z>N)Ú
addCleanupÚsysÚsetswitchintervalÚgetswitchinterval©r   r   r   r   ÚsetUp)   s   zSynchronizationTests.setUpc                 C   s   |   dtjj¡ dS )zk
        The name of a synchronized method is inaffected by the synchronization
        decorator.
        r   N)ÚassertEqualr   r   r   r   r   r   r   Útest_synchronizedName2   s   z*SynchronizationTests.test_synchronizedNameú!Platform does not support threadsc                    sT   t  ¡  g ‰ tj‡ fdd„d�}| ¡  | ¡  |  ˆ d d¡ |  t  ¡ d¡ dS )z˜
        L{threadable.isInIOThread} returns C{True} if and only if it is called
        in the same thread as L{threadable.registerAsIOThread}.
        c                      s   ˆ   t ¡ ¡S ©N)Úappendr   ÚisInIOThreadr   ©ÚforeignResultr   r   Ú<lambda>B   s    z8SynchronizationTests.test_isInIOThread.<locals>.<lambda>©Útargetr   z#Non-IO thread reported as IO threadz#IO thread reported as not IO threadN)	r   ÚregisterAsIOThreadÚ	threadingÚThreadÚstartÚjoinÚassertFalseÚ
assertTruer#   )r   Útr   r$   r   Útest_isInIOThread9   s   
ÿÿz&SynchronizationTests.test_isInIOThreadc                    sj   t ƒ ‰g ‰ ‡ ‡fdd„}g }tdƒD ]}tj|d�}| |¡ | ¡  q|D ]}| ¡  q&ˆ r3tˆ ƒ‚d S )Nc               
      sR   zt dƒD ]} ˆ ¡  qW d S  ty( } zˆ  t|ƒ¡ W Y d }~d S d }~ww ©Niè  )r   r   ÚAssertionErrorr"   Ústr)r   Úe©ÚerrorsÚor   r   ÚcallMethodLotsQ   s   
ÿ€ÿzHSynchronizationTests.testThreadedSynchronization.<locals>.callMethodLotsé   r'   )r   r   r*   r+   r"   r,   r-   r   )r   r9   Úthreadsr   r0   r   r6   r   ÚtestThreadedSynchronizationK   s   


ÿz0SynchronizationTests.testThreadedSynchronizationc                 C   s    t ƒ }tdƒD ]}| ¡  qd S r2   )r   r   r   )r   r8   r   r   r   r   ÚtestUnthreadedSynchronizationd   s   
ÿz2SynchronizationTests.testUnthreadedSynchronizationN)
r   r   r   r   r   r   ÚthreadingSkipr1   r<   r=   r   r   r   r   r   (   s    	

r   c                   @   s&   e Zd Zeedƒdd„ ƒZdd„ ZdS )ÚSerializationTestsr    c                 C   s4   t  ¡ }t|ƒ}t |¡}t |¡}|  ||¡ d S r!   )r   ÚXLockÚtypeÚpickleÚdumpsÚloadsÚassertIsInstance)r   ÚlockÚlockTypeÚ
lockPickleÚnewLockr   r   r   ÚtestPicklingk   s
   

zSerializationTests.testPicklingc                 C   s(   d}t  |¡}t  |d¡}t  |¡ d S )Ns6   ctwisted.python.threadable
unpickle_lock
p0
(tp1
Rp2
.é   )rB   rD   rC   )r   rH   rF   Ú	newPickler   r   r   ÚtestUnpicklings   s   
z!SerializationTests.testUnpicklingN)r   r   r   r   r>   rJ   rM   r   r   r   r   r?   j   s    
r?   )Ú__doc__rB   r   Úunittestr   r*   ÚImportErrorr>   Útwisted.pythonr   Útwisted.trial.unittestr   r   r   Úsynchronizer   r?   r   r   r   r   Ú<module>   s    ÿ
B