diff --git a/selftests/functional/test_utils.py b/selftests/functional/test_utils.py index c2fa1a6ffbd35beab66c698f876043836eb00817..3789af6d5fcd57d1434dd7ba591a3fe1e5f6b5a6 100644 --- a/selftests/functional/test_utils.py +++ b/selftests/functional/test_utils.py @@ -1,9 +1,15 @@ +import multiprocessing import os -import sys +import random +import shutil import stat -import time +import sys import tempfile -import shutil +import time + +from avocado.utils.filelock import FileLock +from avocado.utils.stacktrace import prepare_exc_info + if sys.version_info[:2] == (2, 6): import unittest2 as unittest @@ -155,5 +161,37 @@ class ProcessTest(unittest.TestCase): def tearDown(self): shutil.rmtree(self.base_logdir) + +def file_lock_action(args): + path, players = args + max_individual_timeout = 0.021 + max_timeout = max_individual_timeout * players + with FileLock(path, max_timeout): + sleeptime = random.random() / 100 + time.sleep(sleeptime) + + +class FileLockTest(unittest.TestCase): + + def setUp(self): + self.tmpdir = tempfile.mkdtemp(prefix='avocado_' + __name__) + + @unittest.skipIf(os.environ.get("AVOCADO_CHECK_LONG") != "1", + "Skipping test that takes a long time to run") + def test_filelock(self): + players = 1000 + pool = multiprocessing.Pool(players) + args = [(self.tmpdir, players)] * players + try: + pool.map(file_lock_action, args) + except: + msg = 'Failed to run FileLock with %s players:\n%s' + msg %= (players, prepare_exc_info(sys.exc_info())) + self.fail(msg) + + def tearDown(self): + shutil.rmtree(self.tmpdir) + + if __name__ == '__main__': unittest.main()