vendredi 1 décembre 2017

Testing python class using `fcntl.flock` method to lock file

I tried to find a way of testing my python class locking files by fcntl.flock method, but I couldn't find any tip nor example for this.

Here is my class FSLock:

import fcntl

class FSLock(object):
    def __init__(self, lock_name):
        self.__lock_file = open(lock_name, mode='wb')

    def __enter__(self):
        fcntl.flock(self.__lock_file, fcntl.LOCK_EX)

    def __exit__(self, exc_type, exc_val, exc_tb):
        fcntl.flock(self.__lock_file, fcntl.LOCK_UN)

And I wanna test kind like that:

class TestFSLock(unittest.TestCase):
    def test_file_locked_inside_with_statement(self):
        pass
    def test_access_denied_to_file_inside_with_statement(self):
        pass
    def test_file_not_locked_outside_with_statement(self):
        pass
    def test_write_acces_to_file_outside_with_statement(self):
        pass

Although I always passed lower 2 tests, I could never correctly test first two tests...

Could You give me a suggestion or solution how can I test it, please?

Aucun commentaire:

Enregistrer un commentaire