<commit_before>import pytest

from engine.lease import Lease


class TestLease:
    def test_initialization(self, mocker):
        lock = mocker.MagicMock(is_aquired=False)
        lease = Lease(lock, 9)

        assert lease.lock == lock
        assert not lease.is_held

    def test_acquire(self, mocker):
        lock = mocker.MagicMock()
        lock.acquire.side_effect = [True, RuntimeError]  # Exception here is to stop the infinite loop
        lock.release.side_effect = None

        # Mock the is_acquired property
        # as per https://docs.python.org/3/library/unittest.mock.html#unittest.mock.PropertyMock
        type(lock).is_acquired = mocker.PropertyMock(return_value=True)

        mocker.patch('engine.lease.time.sleep', return_value=None)

        lease = Lease(lock, 10)

        with pytest.raises(RuntimeError):
            lease.acquire()

        assert lease.is_held<commit_msg>Fixed lease unit tests.<commit_after>import pytest

from engine.lease import Lease


class TestLease:
    def test_initialization(self, mocker):
        lock = mocker.MagicMock(is_acquired=False)
        lease = Lease(lock, 9, 10)

        assert lease.lock == lock
        assert lease.lease_ttl == 10
        assert lease.refresh_ttl == 20 / 3
        assert not lease.is_held
        assert not lease.quit

    def test_acquire(self, mocker):
        lock = mocker.MagicMock()
        lock.acquire.side_effect = [True, RuntimeError]  # Exception here is to stop the infinite loop
        lock.release.side_effect = None

        # Mock the is_acquired property
        # as per https://docs.python.org/3/library/unittest.mock.html#unittest.mock.PropertyMock
        type(lock).is_acquired = mocker.PropertyMock(return_value=True)

        mocker.patch('engine.lease.time.sleep', return_value=None)

        lease = Lease(lock, 10, 6)

        with pytest.raises(RuntimeError):
            lease.acquire()

        assert lease.is_held

    def test_quit_acquire(self, mocker):
        lock = mocker.MagicMock(is_acquired=False)
        lease = Lease(lock, 10, 6)

        lease.quit = True
        lease.acquire()

        assert not lock.acquire.called
        assert not lease.is_held
{'fraction_non_alphanumeric': 0.09235668789808917, 'fraction_numerical': 0.008644222020018199, 'mean_word_length': 2.593137254901961, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3807652', 'n_tokens_mistral': 705, 'n_tokens_neox': 658, 'n_words': 150}