1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2018 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
"""Unit test dnf.lock module.
Locking is very hard to cover reasonably with a unit test, this is more or less
just a sanity check.
"""
from __future__ import absolute_import
from __future__ import unicode_literals
import multiprocessing
import os
import re
import threading
import dnf.lock
import dnf.pycomp
import dnf.util
from dnf.exceptions import ProcessLockError, ThreadLockError
import tests.support
from tests.support import mock
# The tests here are not compatible with the forkserver method,
# which is the default on Python 3.14+.
# See https://github.com/python/cpython/issues/125714
if multiprocessing.get_start_method() == 'forkserver':
mp_context = multiprocessing.get_context(method='fork')
else:
mp_context = multiprocessing.get_context()
class ConcurrencyMixin(object):
def __init__(self, lock):
self.lock = lock
def run(self):
try:
with self.lock:
pass
except (ProcessLockError, ThreadLockError) as e:
self.queue.put(e)
class OtherThread(ConcurrencyMixin, threading.Thread):
def __init__(self, lock):
ConcurrencyMixin.__init__(self, lock)
threading.Thread.__init__(self)
self.queue = dnf.pycomp.Queue(1)
class OtherProcess(ConcurrencyMixin, mp_context.Process):
def __init__(self, lock):
ConcurrencyMixin.__init__(self, lock)
mp_context.Process.__init__(self)
self.queue = mp_context.Queue(1)
TARGET = os.path.join(tests.support.USER_RUNDIR, 'unit-test.pid')
def build_lock(blocking=False):
return dnf.lock.ProcessLock(TARGET, 'unit-tests', blocking)
class LockTest(tests.support.TestCase):
def test_fit_lock_dir(self):
orig = '/some'
with mock.patch('dnf.util.am_i_root', return_value=True):
self.assertEqual(dnf.lock._fit_lock_dir(orig), '/some')
with mock.patch('dnf.util.am_i_root', return_value=False):
dir_ = dnf.lock._fit_lock_dir(orig)
match = re.match(
r'/var/tmp/dnf-[^:]+-[^/]+/locks/2690814ff0269c86e5109d2915ea572092918cb3',
dir_)
self.assertTrue(match)
class ProcessLockTest(tests.support.TestCase):
@classmethod
def tearDownClass(cls):
dnf.util.rm_rf(tests.support.USER_RUNDIR)
def test_simple(self):
l1 = build_lock()
target = l1.target
with l1:
self.assertFile(target)
self.assertPathDoesNotExist(target)
def test_reentrance(self):
l1 = build_lock()
with l1:
with l1:
pass
def test_another_process(self):
l1 = build_lock()
process = OtherProcess(l1)
with l1:
process.start()
process.join()
self.assertIsInstance(process.queue.get(), ProcessLockError)
def test_another_process_blocking(self):
l1 = build_lock(blocking=True)
l2 = build_lock(blocking=True)
process = OtherProcess(l1)
target = l1.target
with l2:
process.start()
process.join()
self.assertEqual(process.queue.empty(), True)
self.assertPathDoesNotExist(target)
def test_another_thread(self):
l1 = build_lock()
thread = OtherThread(l1)
with l1:
thread.start()
thread.join()
self.assertIsInstance(thread.queue.get(), ThreadLockError)
|