Esempio n. 1
0
def simple_recursive_locking_function():
    """try to trigger the same lock from the same process, an exception
    should be expected
    """
    with locking_function(simple_locked_function):
        with locking_function(simple_locked_function):
            pass
    return 'I should not be reached'
Esempio n. 2
0
def simple_recursive_locking_function():
    """try to trigger the same lock from the same process, an exception
    should be expected
    """
    with func_locker.locking_function(simple_locked_function):
        with func_locker.locking_function(simple_locked_function):
            pass
    return 'I should not be reached'
Esempio n. 3
0
    def test_simple_with(self):
        with locking_function(simple_function_to_lock):
            with open(_get_function_lock_path('simple_function_to_lock'),
                      'r') as rf:
                content = rf.read()

            self.assertEqual(str(os.getpid()), content)
Esempio n. 4
0
    def test_simple_with(self):
        with locking_function(simple_function_to_lock):
            with open(_get_function_lock_path('simple_function_to_lock'),
                      'r') as rf:
                content = rf.read()

            self.assertEqual(str(os.getpid()), content)
Esempio n. 5
0
    def test_simple_with_lock_function(self):
        """lock a function that is already decorated by lock_function"""
        with func_locker.locking_function(simple_locked_function):
            with open(_get_function_lock_path('simple_locked_function')) as rf:
                content = rf.read()

            assert str(os.getpid()) == content
Esempio n. 6
0
    def test_simple_with(self):
        with func_locker.locking_function(simple_function_to_lock):
            with open(
                    _get_function_lock_path('simple_function_to_lock')) as rf:
                content = rf.read()

            assert str(os.getpid()) == content
Esempio n. 7
0
    def test_negative_with_locking_not_locked(self):

        with self.assertRaises(FunctionLockerError) as context:
            with locking_function(simple_function_not_locked):
                pass

        self.assertIn('Cannot ensure locking', str(context.exception))
Esempio n. 8
0
    def test_negative_with_locking_not_locked(self):

        with self.assertRaises(FunctionLockerError) as context:
            with locking_function(simple_function_not_locked):
                pass

        self.assertIn('Cannot ensure locking', str(context.exception))
Esempio n. 9
0
    def test_simple_with_lock_function(self):
        """lock a function that is already decorated by lock_function"""
        with locking_function(simple_locked_function):
            with open(_get_function_lock_path('simple_locked_function'),
                      'r') as rf:
                content = rf.read()

            self.assertEqual(str(os.getpid()), content)
Esempio n. 10
0
    def test_simple_with_lock_function(self):
        """lock a function that is already decorated by lock_function"""
        with locking_function(simple_locked_function):
            with open(_get_function_lock_path('simple_locked_function'),
                      'r') as rf:
                content = rf.read()

            self.assertEqual(str(os.getpid()), content)
Esempio n. 11
0
def simple_scoped_locking_function():
    """This function do nothing, when called the locking function must create
    a lock file
    """
    with locking_function(simple_scoped_locking_function,
                          scope=NAMESPACE_SCOPE_TEST_2, scope_context=SCOPE_2):
        pass

    return None
Esempio n. 12
0
def simple_scoped_locking_function():
    """This function do nothing, when called the locking function must create
    a lock file
    """
    with func_locker.locking_function(
        simple_scoped_locking_function, scope=NAMESPACE_SCOPE_TEST_2, scope_context=SCOPE_2
    ):
        pass

    return None
Esempio n. 13
0
def simple_with_locking_function(index=None):
    global counter_file
    time.sleep(0.05)
    with func_locker.locking_function(simple_locked_function):
        with open(_get_function_lock_path('simple_locked_function')) as rf:
            content = rf.read()

    if index is not None:
        saved_counter = int(counter_file.read())
        counter_file.write(str(index + saved_counter))

    time.sleep(0.05)
    return os.getpid(), content
Esempio n. 14
0
def simple_with_locking_function(index=None):
    time.sleep(0.05)
    with locking_function(simple_locked_function):
        with open(_get_function_lock_path('simple_locked_function'),
                  'r') as rf:
            content = rf.read()

    if index is not None:
        saved_counter = int(_read_counter_file())
        _write_to_counter_file(str(index + saved_counter))

    time.sleep(0.05)
    return os.getpid(), content
Esempio n. 15
0
def simple_with_locking_function(index=None):
    time.sleep(0.05)
    with locking_function(simple_locked_function):
        with open(_get_function_lock_path('simple_locked_function'),
                  'r') as rf:
            content = rf.read()

    if index is not None:
        saved_counter = int(_read_counter_file())
        _write_to_counter_file(str(index + saved_counter))

    time.sleep(0.05)
    return os.getpid(), content
Esempio n. 16
0
    def test_negative_with_locking_not_locked(self):

        with pytest.raises(func_locker.FunctionLockerError, match=r'.*Cannot ensure locking.*'):
            with func_locker.locking_function(simple_function_not_locked):
                pass
Esempio n. 17
0
    def test_locker_file_location_when_in_class(self):
        """Check the lock file location when lock function in class"""

        file_path = _get_function_lock_path('simple_function_to_lock', class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path) as rf:
                content = rf.read()
        else:
            content = ''
        assert str(os.getpid()) != content

        with func_locker.locking_function(SimpleClass.simple_function_to_lock):
            with open(file_path) as rf:
                content = rf.read()

        assert str(os.getpid()) == content

        file_path = _get_function_lock_path('simple_function_to_lock_cls', class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path) as rf:
                content = rf.read()
        else:
            content = ''
        assert str(os.getpid()) != content

        with func_locker.locking_function(SimpleClass.simple_function_to_lock_cls):
            with open(file_path) as rf:
                content = rf.read()

        assert str(os.getpid()) == content

        file_path = _get_function_lock_path('simple_function_to_lock_cls', class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path) as rf:
                content = rf.read()
        else:
            content = ''
        assert str(os.getpid()) != content
        _, content = SimpleClass.simple_function_to_lock_cls(file_path=file_path)
        assert str(os.getpid()) == content

        simple = SimpleClass()
        file_path = _get_function_lock_path('simple_function_to_lock', class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path) as rf:
                content = rf.read()
        else:
            content = ''
        assert str(os.getpid()) != content
        _, content = simple.simple_function_to_lock(file_path=file_path)
        assert os.getpid() == int(content)
        # subCalss
        file_path = _get_function_lock_path(
            'simple_function_to_lock_cls', class_name='SimpleClass.SubClass'
        )
        if os.path.exists(file_path):
            with open(file_path) as rf:
                content = rf.read()
        else:
            content = ''
        assert str(os.getpid()) != content
        _, content = SimpleClass.SubClass.simple_function_to_lock_cls(file_path=file_path)

        assert str(os.getpid()) == content

        if os.path.exists(file_path):
            with open(file_path) as rf:
                content = rf.read()
        else:
            content = ''
        assert str(os.getpid()) != content
        with func_locker.locking_function(SimpleClass.SubClass.simple_function_to_lock_cls):
            with open(file_path) as rf:
                content = rf.read()

        assert str(os.getpid()) == content
Esempio n. 18
0
    def test_locker_file_location_when_in_class(self):
        """Check the lock file location when lock function in class"""

        file_path = _get_function_lock_path(
            'simple_function_to_lock', class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)

        with locking_function(SimpleClass.simple_function_to_lock):
            with open(file_path, 'r') as rf:
                content = rf.read()

        self.assertEqual(str(os.getpid()), content)

        file_path = _get_function_lock_path(
            'simple_function_to_lock_cls', class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)

        with locking_function(SimpleClass.simple_function_to_lock_cls):
            with open(file_path, 'r') as rf:
                content = rf.read()

        self.assertEqual(str(os.getpid()), content)

        file_path = _get_function_lock_path(
            'simple_function_to_lock_cls', class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)
        _, content = SimpleClass.simple_function_to_lock_cls(
            file_path=file_path)
        self.assertEqual(str(os.getpid()), content)

        simple = SimpleClass()
        file_path = _get_function_lock_path(
            'simple_function_to_lock', class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)
        _, content = simple.simple_function_to_lock(file_path=file_path)
        self.assertEqual(os.getpid(), int(content))
        # subCalss
        file_path = _get_function_lock_path(
            'simple_function_to_lock_cls', class_name='SimpleClass.SubClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)
        _, content = SimpleClass.SubClass.simple_function_to_lock_cls(
            file_path=file_path)

        self.assertEqual(str(os.getpid()), content)

        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)
        with locking_function(
                SimpleClass.SubClass.simple_function_to_lock_cls):
            with open(file_path, 'r') as rf:
                content = rf.read()

        self.assertEqual(str(os.getpid()), content)
Esempio n. 19
0
    def test_locker_file_location_when_in_class(self):
        """Check the lock file location when lock function in class"""

        file_path = _get_function_lock_path('simple_function_to_lock',
                                            class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)

        with locking_function(SimpleClass.simple_function_to_lock):
            with open(file_path, 'r') as rf:
                content = rf.read()

        self.assertEqual(str(os.getpid()), content)

        file_path = _get_function_lock_path('simple_function_to_lock_cls',
                                            class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)

        with locking_function(SimpleClass.simple_function_to_lock_cls):
            with open(file_path, 'r') as rf:
                content = rf.read()

        self.assertEqual(str(os.getpid()), content)

        file_path = _get_function_lock_path('simple_function_to_lock_cls',
                                            class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)
        _, content = SimpleClass.simple_function_to_lock_cls(
            file_path=file_path)
        self.assertEqual(str(os.getpid()), content)

        simple = SimpleClass()
        file_path = _get_function_lock_path('simple_function_to_lock',
                                            class_name='SimpleClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)
        _, content = simple.simple_function_to_lock(file_path=file_path)
        self.assertEqual(os.getpid(), int(content))
        # subCalss
        file_path = _get_function_lock_path('simple_function_to_lock_cls',
                                            class_name='SimpleClass.SubClass')
        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)
        _, content = SimpleClass.SubClass.simple_function_to_lock_cls(
            file_path=file_path)

        self.assertEqual(str(os.getpid()), content)

        if os.path.exists(file_path):
            with open(file_path, 'r') as rf:
                content = rf.read()
        else:
            content = ''
        self.assertNotEqual(str(os.getpid()), content)
        with locking_function(
                SimpleClass.SubClass.simple_function_to_lock_cls):
            with open(file_path, 'r') as rf:
                content = rf.read()

        self.assertEqual(str(os.getpid()), content)