o
    wOiZ                     @   sT   d dl Z zd dlZW n ey   d dlmZ Y nw d dlmZ G dd deZdS )    N)mock)TestClientApiBasec                       s|   e Zd Zdd Z fddZdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Z  ZS )TestClientLockc                 C   sJ   dddddd| j jdddg}ddd	d|d
gdd}| d| d S )N/_locks/test_lock/12qwwwq!   )keyvaluemodifiedIndexcreatedIndexz/_locks/test_lock/34"   getT/_locks/test_lock)r   dirnodes)r   r   actionnode   )lockeruuid	_mock_api)selfr   d r   M/home/ubuntu/.local/lib/python3.10/site-packages/etcd/tests/unit/test_lock.pyrecursive_read   s   zTestClientLock.recursive_readc                    s"   t t|   t| jd| _d S )N	test_lock)superr   setUpetcdLockclientr   r   	__class__r   r   r      s   zTestClientLock.setUpc                 C   s4   |  | jjd |  | jjd |  | jjd dS )z<
        Verify the lock gets initialized correctly
        r   r   FN)assertEqualsr   namepathis_takenr#   r   r   r   test_initialization   s   z"TestClientLock.test_initializationc                 C   st   t | jd}tj|jdd|_tj|jdd|_ddd|jdd	}| d
| | 	|
 d | 	|jd dS )z@
        Acquiring a precedingly inexistent lock works.
        r   Fspecreturn_valueTset   r   r
   r   r	   r   r   1N)r    r!   r"   r   	MagicMock
_find_lock	_acquiredr   r   r&   acquire	_sequence)r   lr   r   r   r   test_acquire&   s   zTestClientLock.test_acquirec                 C   sF   d| j _ddd| j jdd}| d| d| j _| | j jd d	S )
z"
        Test is_acquired
        r1   r   r/   r   r0   r   r   TN)r   r6   r   r   r)   r&   is_acquiredr   r   r   r   r   test_is_acquired:   s   zTestClientLock.test_is_acquiredc                 C   s^   d| j _d| j _| | j jd d| j _| tj| j j | | j jd | | j jd dS )z+
        Test is_acquired failures
        2FTN)	r   r6   r)   r&   r9   _mock_exceptionr    EtcdKeyNotFoundlock_keyr#   r   r   r   test_is_not_acquiredK   s   z#TestClientLock.test_is_not_acquiredc                    s   d| j _d}tj| j j|d| j _| | j   | | j j d}tj|d| j _| | j jdd | | j j dd	d
| j j	dd}| 
d| ddg  fdd}tj| j j|d| j _| | j   dS )z/
        Test the acquiring primitives
        4/_locks/test_lock/4Nr+   )r   rC   )r-   F)blockingdeleter/   r   r0   r   r   c                            S Npopr   returnsr   r   side_effectp      z1TestClientLock.test_acquired.<locals>.side_effect)r,   rL   N)r   r6   r   r2   _get_locker
assertTruer4   r)   assertFalser   r   )r   retvalr   rL   r   rJ   r   test_acquiredW   s0   

zTestClientLock.test_acquiredc                    sz   d| j _ddtjddddfg  fdd	}d
dd| j jdd}| d| tj| j j|d| j _| 	| j 
  d S )N   rB   r   rC      )r   r
   r   c                      rF   rG   rH   r   rJ   r   r   rL   ~   rM   z<TestClientLock.test_acquired_no_timeout.<locals>.side_effectr   r/   r0   r   r   )rL   )r   r6   r    
EtcdResultr   r   r   create_autospecrN   rO   r4   )r   rL   r   r   rJ   r   test_acquired_no_timeoutw   s    
z'TestClientLock.test_acquired_no_timeoutc                 C   sN   |  t | jj W d   n1 sw   Y  d| j_| d| jj dS )z;
        Test responses from the lock_key property
        N5z/_locks/test_lock/5)assertRaises
ValueErrorr   r?   r6   r&   r#   r   r   r   test_lock_key   s
   
zTestClientLock.test_lock_keyc                 C   s    | j d | d| j j d S )Nz/_locks/test_lock/1010)r   _set_sequencer&   r6   r#   r   r   r   test_set_sequence   s   z TestClientLock.test_set_sequencec                 C   s   ddd| j jdd}| d| d| j _| | j   | tj| j j	 | 
| j   d | j _|   | | j   | | j jd d S )	Nr   r/   r   r0   r   r   r1   34)r   r   r   r6   rO   r3   r=   r    r>   r?   rP   r   r&   r:   r   r   r   test_find_lock   s   zTestClientLock.test_find_lockc                 C   s   |    | dtjdg dddd dd d dd
df| j  | tj d| j_| j  W d    d S 1 s9w   Y  d S )Nr   Fr   r   )
newKey	_childrenr   r
   r	   
expirationr   ttlr   r   rU   35)	r   r&   r    rV   r   rN   rZ   EtcdLockExpiredr6   r#   r   r   r   test_get_locker   s   ("zTestClientLock.test_get_lockerc                 C   sN   ddd| j jdd}| d| d| j _d| j _| j   | | j j d S )	NrE   r/   r   r0   r   r   rT   T)r   r   r   r6   r)   releaserP   r:   r   r   r   test_release   s   
zTestClientLock.test_release)__name__
__module____qualname__r   r   r*   r8   r;   r@   rR   rX   r\   r_   ra   rh   rj   __classcell__r   r   r$   r   r   	   s     	r   )r    r   ImportErrorunittestetcd.tests.unitr   r   r   r   r   r   <module>   s    