
    &i.                     l   d Z ddlZddlmc mZ ddlZddlZddl	Z	ddl
mZmZ ddlZej                  j                  dd       ddlmZmZmZ d Z G d d      Z G d	 d
      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Z G d d      Zy)u   
Tests for Story 9.03 — RedisEpochLock
File: tests/track_b/test_story_9_03.py

BB Tests: BB1, BB2, BB3, BB4
WB Tests: WB1, WB2, WB3, WB4
    N)	MagicMockcallz/mnt/e/genesis-systemRedisEpochLockEPOCH_LOCK_KEYEPOCH_LOCK_TTLc                  ^    t               } d| j                  _        d| j                  _        | S )z:Return a fresh MagicMock that models an empty Redis store.NT)r   getreturn_valueset)rs    6/mnt/e/genesis-system/tests/track_b/test_story_9_03.pymake_mock_redisr      s&    AAEEAEEH    c                   "    e Zd ZdZd Zd Zd Zy)'TestBB1_FirstAcquireSucceedsSecondFailszPBB1: First acquire returns True; second acquire (different epoch) returns False.c                 (   t               }t        |      }|j                  }d} ||      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }	t        t        j                  |	            d x}x}x}x}}y )	Ne1TiszM%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.acquire
}(%(py4)s)
} is %(py9)slockpy0py2py4py6py9assert %(py11)spy11)r   r   acquire
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation
selfr   r   @py_assert1@py_assert3@py_assert5@py_assert8@py_assert7@py_format10@py_format12s
             r   test_first_acquire_returns_truezGTestBB1_FirstAcquireSucceedsSecondFails.test_first_acquire_returns_true.   s    a ||)D)|D!)T)!T))))!T))))))t)))t)))|)))D)))!)))T))))))))r   c                 H   t               }dd g|j                  _        t        |      }|j                  }d} ||      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }	t        t        j                  |	            d x}x}x}x}}|j                  }d	} ||      }d
}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }	t        t        j                  |	            d x}x}x}x}}y )NTr   r   r   r   r   r   r    e2Fr   r   side_effectr   r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   s
             r   +test_second_acquire_returns_false_when_heldzSTestBB1_FirstAcquireSucceedsSecondFails.test_second_acquire_returns_false_when_held3   sQ   !4La ||)D)|D!)T)!T))))!T))))))t)))t)))|)))D)))!)))T)))))))||*D*|D!*U*!U****!U******t***t***|***D***!***U********r   c                 ^   t               }ddg|j                  _        t        |      }t        |      }|j                  }d} ||      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }	dd	|	iz  }
t        t        j                  |
            dx}x}x}x}}|j                  }d
} ||      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }	dd	|	iz  }
t        t        j                  |
            dx}x}x}x}}y)z8Simulates two separate instances competing for the lock.TNr   r   r   lock1r   r   r    r5   Flock2r6   )r+   r   r:   r;   r,   r-   r.   r/   r0   r1   r2   s              r   9test_second_acquire_with_different_instance_returns_falsezaTestBB1_FirstAcquireSucceedsSecondFails.test_second_acquire_with_different_instance_returns_false;   s[   !4Lq!q!}}*T*}T"*d*"d****"d******u***u***}***T***"***d*******}}+T+}T"+e+"e++++"e++++++u+++u+++}+++T+++"+++e++++++++r   N)__name__
__module____qualname____doc__r3   r8   r<    r   r   r   r   +   s    Z*
+,r   r   c                       e Zd ZdZd Zd Zy)TestBB2_ReleaseAllowsReacquirezABB2: release() frees the lock so a subsequent acquire() succeeds.c                    t               }d}t        j                         }t        j                         }| d| d| j                  d      }d|j                  _        t        |      }|j                  } ||      }d}	||	u }
|
st        j                  d|
fd||	f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |	      d	z  }d
d|iz  }t!        t        j"                  |            d x}x}x}
}	||j$                  _        |j'                  |       |j(                  j+                  t,               d|j                  _        |j                  j/                          t        |      }|j                  }d} ||      }d}||u }	|	st        j                  d|	fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t!        t        j"                  |            d x}x}x}x}	}y )Nr   :utf-8Tr   )zM%(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s.acquire
}(%(py3)s)
} is %(py8)sr   epoch_id)r   r   py3py5py8zassert %(py10)spy10e3r   r;   r   r   r    )r   socketgethostnameosgetpidencoder   r   r   r!   r"   r#   r$   r%   r&   r'   r(   r)   r
   releasedeleteassert_called_once_withr   
reset_mock)r+   r   rG   hostnamepid
lock_valuer   r,   @py_assert4r0   @py_assert6@py_format9@py_format11r;   r-   r.   r/   r1   r2   s                      r   test_release_then_reacquirez:TestBB2_ReleaseAllowsReacquire.test_release_then_reacquireH   s   %%'iik z8*AcU3::7C
 "a ||-|H%--%----%------t---t---|------H---H---%---------- (X	((8 "	q!}}*T*}T"*d*"d****"d******u***u***}***T***"***d********r   c                 N   t               }d}| dt        j                          dt        j                          j                         }d|j                  _        t        |      }|j                  |       |j                  }d }||u}|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                   |      ndt        j                   |      t        j                   |      dz  }dd	|iz  }	t#        t        j$                  |	            d x}x}}||j&                  _        |j)                  |       |j                  }d }||u }|st        j                  d
|fd||f      dt        j                         v st        j                  |      rt        j                   |      ndt        j                   |      t        j                   |      dz  }dd	|iz  }	t#        t        j$                  |	            d x}x}}y )Nr   rE   Tis not)z7%(py2)s
{%(py2)s = %(py0)s._held_value
} is not %(py5)sr   )r   r   rI   zassert %(py7)spy7r   )z3%(py2)s
{%(py2)s = %(py0)s._held_value
} is %(py5)s)r   rM   rN   rO   rP   rQ   r   r   r   r!   _held_valuer"   r#   r$   r%   r&   r'   r(   r)   r
   rR   )
r+   r   rG   rX   r   r,   rY   r-   @py_format6@py_format8s
             r   test_release_clears_held_valuez=TestBB2_ReleaseAllowsReacquire.test_release_clears_held_value_   s\    z6#5#5#7"8"))+GNNP
!a X+t+t++++t++++++t+++t++++++t+++++++'X'4'4''''4''''''t'''t''''''4'''''''r   N)r=   r>   r?   r@   r]   re   rA   r   r   rC   rC   E   s    K+.(r   rC   c                   "    e Zd ZdZd Zd Zd Zy)TestBB3_GetLockHolderzDBB3: get_lock_holder() returns value while held, None when released.c                    t               }d|j                  _        t        |      }|j	                  d       dt        j                          dt        j                          }|j                  d      |j                  _        |j                         }||k(  }|st        j                  d|fd||f      dt        j                         v st        j                   |      rt        j"                  |      ndd	t        j                         v st        j                   |      rt        j"                  |      nd	d
z  }dd|iz  }t%        t        j&                  |            d }y )NTr   e1:rE   rF   ==)z%(py0)s == %(py2)sholderexpectedr   r   assert %(py4)sr   )r   r   r   r   r!   rM   rN   rO   rP   rQ   r
   get_lock_holderr"   r#   r$   r%   r&   r'   r(   r)   )r+   r   r   rm   rl   r,   @py_format3@py_format5s           r   "test_returns_lock_value_while_heldz8TestBB3_GetLockHolder.test_returns_lock_value_while_heldp   s    !a T++-.a		}=%__W5%%'!!!!v!!!!!!v!!!v!!!!!!!!!!!!!!!!r   c                    t               }d |j                  _        t        |      }|j                  } |       }d }||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}}y )Nr   zN%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.get_lock_holder
}()
} is %(py7)sr   r   r   r   ra   assert %(py9)sr   )r   r
   r   r   rp   r"   r#   r$   r%   r&   r'   r(   r)   )	r+   r   r   r,   r-   rZ   r.   rd   r1   s	            r   test_returns_none_when_not_heldz5TestBB3_GetLockHolder.test_returns_none_when_not_held|   s    !a ##-#%--%----%------t---t---#---%----------r   c                 @   t               }d|j                  _        d|j                  _        t	        |      }|j                  d       |j                         }t        |t              }|sddt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  |      rt        j                  |      nddt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      dz  }t        t        j                  |            d }d	}||k(  }|st        j                   d
|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }dd|iz  }	t        t        j                  |	            d x}}y )NTs   e1:myhost:1234r   z5assert %(py4)s
{%(py4)s = %(py0)s(%(py1)s, %(py2)s)
}
isinstancerl   str)r   py1r   r   ze1:myhost:1234rj   z%(py0)s == %(py3)sr   rH   assert %(py5)srI   )r   r   r   r
   r   r!   rp   rz   r{   r$   r%   r"   r&   r'   r(   r)   r#   )
r+   r   r   rl   r-   rr   @py_assert2r,   @py_format4rc   s
             r   test_returns_string_not_bytesz3TestBB3_GetLockHolder.test_returns_string_not_bytes   s*   !.a T%%'&#&&&&&&&&z&&&z&&&&&&&&&&&&&&&&&#&&&#&&&&&&&&&&))v)))))v)))))))v)))v)))))))))))r   N)r=   r>   r?   r@   rs   rx   r   rA   r   r   rg   rg   m   s    N
".*r   rg   c                   (    e Zd ZdZd Zd Zd Zd Zy)TestBB4_NoRedisClientzPBB4: When redis_client is None, acquire always returns True, release is a no-op.c                    t        d       }|j                  }d} ||      }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }dd	|iz  }t        t        j                  |            d x}x}x}x}}y )
Nredis_clientr   Tr   r   r   r   r   r    
r   r!   r"   r#   r$   r%   r&   r'   r(   r)   	r+   r   r,   r-   r.   r/   r0   r1   r2   s	            r   'test_acquire_without_redis_returns_truez=TestBB4_NoRedisClient.test_acquire_without_redis_returns_true   s    40||)D)|D!)T)!T))))!T))))))t)))t)))|)))D)))!)))T))))))))r   c                 
   t        d      }|j                  }d} ||      }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx}x}x}x}}|j                  }d} ||      }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx}x}x}x}}|j                  }d} ||      }d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      dz  }d	d
|iz  }t        t        j                  |            dx}x}x}x}}y)z0Multiple acquires all return True with no Redis.Nr   r   Tr   r   r   r   r   r    r5   rL   r   r   s	            r   &test_acquire_always_true_without_redisz<TestBB4_NoRedisClient.test_acquire_always_true_without_redis   s   40||)D)|D!)T)!T))))!T))))))t)))t)))|)))D)))!)))T)))))))||)D)|D!)T)!T))))!T))))))t)))t)))|)))D)))!)))T)))))))||)D)|D!)T)!T))))!T))))))t)))t)))|)))D)))!)))T))))))))r   c                 `    t        d       }|j                  d       |j                  d       y )Nr   r   )r   r!   rR   )r+   r   s     r   "test_release_without_redis_is_noopz8TestBB4_NoRedisClient.test_release_without_redis_is_noop   s$    40TTr   c                    t        d       }|j                  d       |j                  } |       }d }||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}}y )	Nr   r   r   ru   r   rv   rw   r   )r   r!   rp   r"   r#   r$   r%   r&   r'   r(   r)   )r+   r   r,   r-   rZ   r.   rd   r1   s           r   /test_get_lock_holder_without_redis_returns_nonezETestBB4_NoRedisClient.test_get_lock_holder_without_redis_returns_none   s    40T##-#%--%----%------t---t---#---%----------r   N)r=   r>   r?   r@   r   r   r   r   rA   r   r   r   r      s    Z**.r   r   c                   "    e Zd ZdZd Zd Zd Zy)TestWB1_AtomicSetNxExzBWB1: acquire() calls redis.set with nx=True and ex=EPOCH_LOCK_TTL.c                    t               }d|j                  _        t        |      }|j	                  d       |j                  }|j
                  }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      dz  }t        t        j                  |            d x}}|j                  j                  \  }}|j                  }d} ||      }d}	||	u }
|
st        j                  d|
fd||	f      d	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      t        j                  |      t        j                  |      t        j                  |	      d
z  }t        j                   d      dz   d|iz  }t        t        j                  |            d x}x}x}x}
}	|j                  }d} ||      }|t"        k(  }
|
s+t        j                  d|
fd|t"        f      d	t        j                         v st        j                  |      rt        j                  |      nd	t        j                  |      t        j                  |      t        j                  |      dt        j                         v st        j                  t"              rt        j                  t"              nddz  }t        j                   dt"               dz   d|iz  }t        t        j                  |            d x}x}x}}
y )NTr   zCassert %(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.set
}.called
}r   )r   r   r   nxr   )zI%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.get
}(%(py4)s)
} is %(py9)skwargsr   z-nx=True required for atomic set-if-not-existsz
>assert %(py11)sr    exrj   )zI%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.get
}(%(py4)s)
} == %(py8)sr   )r   r   r   r   rJ   zex must be z
>assert %(py10)srK   )r   r   r   r   r!   calledr$   r%   r"   r&   r'   r(   r)   	call_argsr
   r#   _format_assertmsgr   )r+   r   r   r,   r-   rr   _r   r.   r/   r0   r1   r2   r[   r\   s                  r   test_set_called_with_nx_and_exz4TestWB1_AtomicSetNxEx.test_set_called_with_nx_and_ex   s   !a Tuuu|||qqu|EEOO	6zzX$Xz$X4X4'XXX4XXXXXXvXXXvXXXzXXX$XXXXXX4XXX)XXXXXXXXzzQ$Qz$Q>1QQQ>QQQQQQvQQQvQQQzQQQ$QQQQQQQQQ>QQQ>QQQQ[@P3QQQQQQQQr   c                    t               }d|j                  _        t        |      }|j	                  d       |j                  j
                  \  }}|d   }|t        k(  }|st        j                  d|fd|t        f      t        j                  |      dt        j                         v st        j                  t              rt        j                  t              nddz  }dd	|iz  }t        t        j                  |            d x}}y )
NTr   r   rj   )z%(py1)s == %(py3)sr   )r|   rH   r   rI   )r   r   r   r   r!   r   r   r"   r#   r'   r$   r%   r&   r(   r)   )	r+   r   r   argsr   @py_assert0r   r   rc   s	            r    test_set_called_with_correct_keyz6TestWB1_AtomicSetNxEx.test_set_called_with_correct_key   s    !a T%%//aAw(w.((((w.(((w((((((.(((.(((((((r   c                 ,   t               }d|j                  _        t        |      }|j	                  d       |j                  j
                  \  }}|d   }|j                  }d} ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }	t        t        j                  |	            d x}x}}y )NTepoch_20260225   zepoch_20260225:Lassert %(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.startswith
}(%(py4)s)
}rX   r   r   r   r   r   r   r   r   r!   r   
startswithr$   r%   r"   r&   r'   r(   r)   )
r+   r   r   r   r   rX   r,   r-   r.   @py_format7s
             r   !test_lock_value_contains_epoch_idz7TestWB1_AtomicSetNxEx.test_lock_value_contains_epoch_id   s    !a %&%%//a!W
$$7%67$%677777777z777z777$777%67777777777r   N)r=   r>   r?   r@   r   r   r   rA   r   r   r   r      s    L	R)8r   r   c                   "    e Zd ZdZd Zd Zd Zy)TestWB2_LockValueFormatzOWB2: Lock value contains epoch_id as prefix (used by release startswith check).c                 ,   t               }d|j                  _        t        |      }|j	                  d       |j                  j
                  \  }}|d   }|j                  }d} ||      }|sddt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      dz  }	t        t        j                  |	            d x}x}}y )NTmyepochr   zmyepoch:r   valuer   r   )
r+   r   r   r   r   r   r,   r-   r.   r   s
             r   $test_lock_value_starts_with_epoch_idz<TestWB2_LockValueFormat.test_lock_value_starts_with_epoch_id   s    !a Y%%//aQ+
+
++++++++u+++u++++++
++++++++++r   c                    t               }d|j                  _        t        |      }|j	                  d       |j                  j
                  \  }}|d   }t        j                  } |       }||v }|st        j                  d|fd||f      dt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      nddz  }	d	d
|	iz  }
t        t        j                  |
            d x}x}}y )NTr   r   in)zJ%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.gethostname
}()
} in %(py6)srM   r   r   assert %(py8)srJ   )r   r   r   r   r!   r   rM   rN   r"   r#   r$   r%   r&   r'   r(   r)   )r+   r   r   r   r   r   r,   r-   r.   r   r[   s              r   !test_lock_value_contains_hostnamez9TestWB2_LockValueFormat.test_lock_value_contains_hostname   s    !a T%%//aQ!!,!#,#u,,,,#u,,,,,,v,,,v,,,!,,,#,,,,,,u,,,u,,,,,,,r   c           	         t               }d|j                  _        t        |      }|j	                  d       |j                  j
                  \  }}|d   }t        j                  } |       }t        |      }||v }	|	sSt        j                  d|	fd||f      dt        j                         v st        j                  t              rt        j                  t              nddt        j                         v st        j                  t              rt        j                  t              ndt        j                  |      t        j                  |      t        j                  |      dt        j                         v st        j                  |      rt        j                  |      ndd	z  }
d
d|
iz  }t        t        j                   |            d x}x}x}}	y )NTr   r   r   )zc%(py7)s
{%(py7)s = %(py0)s(%(py5)s
{%(py5)s = %(py3)s
{%(py3)s = %(py1)s.getpid
}()
})
} in %(py9)sr{   rO   r   )r   r|   rH   rI   ra   r   r   r    )r   r   r   r   r!   r   rO   rP   r{   r"   r#   r$   r%   r&   r'   r(   r)   )r+   r   r   r   r   r   r   rY   rZ   r/   r1   r2   s               r   test_lock_value_contains_pidz4TestWB2_LockValueFormat.test_lock_value_contains_pid   s   !a T%%//aQ99(9;(s;(5((((5((((((s(((s((((((2(((2(((9(((;(((((((((5(((5((((((((r   N)r=   r>   r?   r@   r   r   r   rA   r   r   r   r      s    Y,-)r   r   c                   "    e Zd ZdZd Zd Zd Zy)TestWB3_ReleaseOwnershipCheckz?WB3: release() only deletes if lock value starts with epoch_id.c                     t               }d|j                  _        d|j                  _        t	        |      }|j                  d       |j                  d       |j                  j                  t               y )NT   e1:myhost:9999r   )
r   r   r   r
   r   r!   rR   rS   rT   r   r+   r   r   s      r   *test_release_deletes_when_epoch_id_matcheszHTestWB3_ReleaseOwnershipCheck.test_release_deletes_when_epoch_id_matches   sV    !.a TT	((8r   c                     t               }d|j                  _        d|j                  _        t	        |      }|j                  d       |j                  d       |j                  j                          y )NTr   r   r5   	r   r   r   r
   r   r!   rR   rS   assert_not_calledr   s      r   +test_release_does_not_delete_wrong_epoch_idzITestWB3_ReleaseOwnershipCheck.test_release_does_not_delete_wrong_epoch_id   sT    !.a TT	""$r   c                     t               }d|j                  _        d |j                  _        t	        |      }|j                  d       |j                  d       |j                  j                          y )NTr   r   r   s      r   $test_release_noop_when_key_not_foundzBTestWB3_ReleaseOwnershipCheck.test_release_noop_when_key_not_found  sT    !!a TT	""$r   N)r=   r>   r?   r@   r   r   r   rA   r   r   r   r      s    I9%%r   r   c                   "    e Zd ZdZd Zd Zd Zy)TestWB4_Constantsz;WB4: EPOCH_LOCK_KEY and EPOCH_LOCK_TTL have correct values.c                 p   d}t         |k(  }|st        j                  d|fdt         |f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |      dz  }dd|iz  }t        t        j                  |            d x}}y )Nepoch:lock:nightlyrj   r}   r   r~   r   rI   )	r   r"   r#   r$   r%   r&   r'   r(   r)   r+   r   r,   r   rc   s        r   test_epoch_lock_key_valuez+TestWB4_Constants.test_epoch_lock_key_value  s^    !55~!55555~!5555555~555~555!55555555r   c                 p   d}t         |k(  }|st        j                  d|fdt         |f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |      dz  }dd|iz  }t        t        j                  |            d x}}y )N   rj   r}   r   r~   r   rI   	r   r"   r#   r$   r%   r&   r'   r(   r)   r   s        r   test_epoch_lock_ttl_valuez+TestWB4_Constants.test_epoch_lock_ttl_value  s[    !%%~%%%%~%%%%%%~%%%~%%%%%%%%%%r   c                    d}d}||z  }t         |k(  }|st        j                  d|fdt         |f      dt        j                         v st        j
                  t               rt        j                  t               ndt        j                  |      t        j                  |      dz  }dd|iz  }t        t        j                  |            d x}x}x}}y )	N   i  rj   )z%(py0)s == (%(py3)s * %(py5)s)r   )r   rH   rI   r   rJ   r   )r+   r   rY   rZ   r,   r   r[   s          r    test_epoch_lock_ttl_is_two_hoursz2TestWB4_Constants.test_epoch_lock_ttl_is_two_hours  st    !")T)T)~))))~))))))~)))~))))))T)))))))r   N)r=   r>   r?   r@   r   r   r   rA   r   r   r   r     s    E6&*r   r   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)TestContextManagerz=Context manager: with lock.for_epoch("e1") as (lk, acquired).c                    t               }d|j                  _        t        |      }|j	                  d      5 \  }}d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }dd|iz  }t        t        j                  |            d x}}||u }|st        j                  d|fd	||f      d
t        j                         v st        j                  |      rt        j                  |      nd
dt        j                         v st        j                  |      rt        j                  |      nddz  }	dd|	iz  }
t        t        j                  |
            d }d d d        y # 1 sw Y   y xY w)NTr   r   z%(py0)s is %(py3)sacquiredr~   r   rI   )z%(py0)s is %(py2)slkr   rn   ro   r   r   r   r   r   	for_epochr"   r#   r$   r%   r&   r'   r(   r)   )r+   r   r   r   r   r   r,   r   rc   rq   rr   s              r   &test_context_manager_acquires_on_enterz9TestContextManager.test_context_manager_acquires_on_enter#  s   !a ^^D! 	^b(##8t####8t######8###8###t#######:222	 	 	s   E7F88Gc                 x   t               }d|j                  _        t        |      }dt	        j
                          dt        j                          j                         }||j                  _        |j                  d      5 \  }}	 d d d        |j                  j                  t               y # 1 sw Y   )xY w)NTri   rE   r   )r   r   r   r   rM   rN   rO   rP   rQ   r
   r   rS   rT   r   r+   r   r   rX   r   r   s         r   %test_context_manager_releases_on_exitz8TestContextManager.test_context_manager_releases_on_exit+  s    !a 6--/0"))+?FFH
'^^D! 	^b(		((8	 	s   B00B9c                    t               }d|j                  _        t        |      }dt	        j
                          dt        j                          j                         }||j                  _        t        j                  t              5  |j                  d      5 \  }}t        d      # 1 sw Y   nxY w	 d d d        n# 1 sw Y   nxY w|j                  j                  t                y )NTri   rE   r   z
test error)r   r   r   r   rM   rN   rO   rP   rQ   r
   pytestraises
ValueErrorr   rS   rT   r   r   s         r   *test_context_manager_releases_on_exceptionz=TestContextManager.test_context_manager_releases_on_exception5  s    !a 6--/0"))+?FFH
']]:& 	/% /"h ../ / /	/ 	/ 	/ 	
((8s   CB,,B5	1CCc                    t               }d |j                  _        t        |      }|j	                  d      5 \  }}d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }dd|iz  }t        t        j                  |            d x}}d d d        y # 1 sw Y   y xY w)	Nr5   Fr   r   r   r~   r   rI   r   )	r+   r   r   r   r   r   r,   r   rc   s	            r   2test_context_manager_acquired_false_when_lock_heldzETestContextManager.test_context_manager_acquired_false_when_lock_held@  s    !a ^^D! 	%^b($$8u$$$$8u$$$$$$8$$$8$$$u$$$$$$$	% 	% 	%s   B)C**C3c                    t        d       }|j                  d      5 \  }}d}||u }|st        j                  d|fd||f      dt	        j
                         v st        j                  |      rt        j                  |      ndt        j                  |      dz  }dd	|iz  }t        t        j                  |            d x}}d d d        y # 1 sw Y   y xY w)
Nr   r   Tr   r   r   r~   r   rI   )
r   r   r"   r#   r$   r%   r&   r'   r(   r)   )r+   r   r   r   r   r,   r   rc   s           r   test_context_manager_no_redisz0TestContextManager.test_context_manager_no_redisG  s    40^^D! 	$^b(##8t####8t######8###8###t#######	$ 	$ 	$s   B)CCN)	r=   r>   r?   r@   r   r   r   r   r   rA   r   r   r   r      s    G9	9%$r   r   c                       e Zd ZdZd Zd Zy)TestImportsz'Verify __init__.py exports are correct.c                    ddl m}m}m} d }||u}|st	        j
                  d|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      dz  }dd|iz  }t        t	        j                  |            d x}}d	}||k(  }|st	        j
                  d
|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      dz  }dd|iz  }t        t	        j                  |            d x}}d}||k(  }|st	        j
                  d
|fd||f      dt        j                         v st	        j                  |      rt	        j                  |      ndt	        j                  |      dz  }dd|iz  }t        t	        j                  |            d x}}y )Nr   r   r_   )z%(py0)s is not %(py3)sr   r~   r   rI   r   rj   r}   r   r   r   )
core.epochr   r   r   r"   r#   r$   r%   r&   r'   r(   r)   )r+   r   r   r   r   r,   r   rc   s           r   test_import_from_packagez$TestImports.test_import_from_packageT  s   MM%))~T))))~T))))))~)))~)))T)))))))!55~!55555~!5555555~555~555!55555555!%%~%%%%~%%%%%%~%%%~%%%%%%%%%%r   c                    ddl m}  |d       }|j                  }d} ||      }d}||u }|st        j                  d|fd||f      dt        j                         v st        j                  |      rt        j                  |      ndt        j                  |      t        j                  |      t        j                  |      t        j                  |      d	z  }d
d|iz  }	t        t        j                  |	            d x}x}x}x}}y )Nr   )r   r   testTr   r   r   r   r   r    )core.epoch.redis_epoch_lockr   r!   r"   r#   r$   r%   r&   r'   r(   r)   )
r+   r   r   r,   r-   r.   r/   r0   r1   r2   s
             r   test_direct_module_importz%TestImports.test_direct_module_importZ  s    >40||+F+|F#+t+#t++++#t++++++t+++t+++|+++F+++#+++t++++++++r   N)r=   r>   r?   r@   r   r   rA   r   r   r   r   Q  s    1&,r   r   )r@   builtinsr$   _pytest.assertion.rewrite	assertionrewriter"   rO   sysrM   unittest.mockr   r   r   pathinsertr   r   r   r   r   r   rC   rg   r   r   r   r   r   r   r   rA   r   r   <module>r      s     	 
  )  * + , ,4%( %(P* *@. .<8 8D) )D% %@
* 
*"*$ *$b, ,r   