提交 f2964e0f 编写于 作者: C Cleber Rosa

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: NCleber Rosa <crosa@redhat.com>
上级 ec65bb32
import multiprocessing
import os import os
import sys import random
import shutil
import stat import stat
import time import sys
import tempfile 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): if sys.version_info[:2] == (2, 6):
import unittest2 as unittest import unittest2 as unittest
...@@ -155,5 +161,37 @@ class ProcessTest(unittest.TestCase): ...@@ -155,5 +161,37 @@ class ProcessTest(unittest.TestCase):
def tearDown(self): def tearDown(self):
shutil.rmtree(self.base_logdir) 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__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册