<s> from __future__ import absolute_import <EOL> import functools <EOL> import pytest <EOL> from sentry . testutils import TestCase <EOL> from sentry . utils . cache import ( <EOL> Lock , <EOL> LockAlreadyHeld , <EOL> UnableToGetLock , <EOL> ) <EOL> class LockTestCase ( TestCase ) : <EOL> def test_basic ( self ) : <EOL> timeout = <NUM_LIT:10> <EOL> lock = Lock ( '<STR_LIT>' , timeout = timeout ) <EOL> assert lock . held is False <EOL> assert lock . seconds_remaining is <NUM_LIT:0> <EOL> assert lock . acquire ( ) is True <EOL> assert timeout > lock . seconds_remaining > ( timeout - <NUM_LIT:0.1> ) <EOL> assert lock . held is True <EOL> with pytest . raises ( LockAlreadyHeld ) : <EOL> lock . acquire ( ) <EOL> assert lock . release ( ) is True <EOL> assert lock . seconds_remaining is <NUM_LIT:0> <EOL> assert lock . held is False <EOL> assert lock . release ( ) is False <EOL> def test_context ( self ) : <EOL> timeout = <NUM_LIT:10> <EOL> lock = Lock ( '<STR_LIT>' , timeout = timeout ) <EOL> with lock as result : <EOL> assert lock is result <EOL> assert lock . held is True <EOL> assert lock . held is False <EOL> def test_concurrent ( self ) : <EOL> make_lock = functools . partial ( Lock , '<STR_LIT>' ) <EOL> first = make_lock ( ) <EOL> second = make_lock ( nowait = True ) <EOL> assert first . acquire ( ) is True <EOL> assert second . acquire ( ) is False <EOL> with pytest . raises ( UnableToGetLock ) : <EOL> with second : <EOL> pass </s>
