From f2964e0f335fd962fcd61ffd774e3a4771fc03ad Mon Sep 17 00:00:00 2001 From: Cleber Rosa Date: Thu, 11 Aug 2016 17:01:05 -0300 Subject: [PATCH] avocado.utils.filelock: add a functional test We used a functional test during the development of the lockfile module, but didn't include because really useful tests would take long (>20s) to run. Now that the Makefile has a target for longer tests, let's add it with 1000 processes. This takes roughly 25s to run. Signed-off-by: Cleber Rosa --- selftests/functional/test_utils.py | 44 ++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/selftests/functional/test_utils.py b/selftests/functional/test_utils.py index c2fa1a6f..3789af6d 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() -- GitLab