Exemplo n.º 1
0
 def testSharerBlocksDelete(self):
     self.sl.acquire(shared=1)
     self._addThread(target=self._doItDelete)
     self.assertRaises(Queue.Empty, self.done.get_nowait)
     self.sl.release()
     self._waitThreads()
     self.failUnlessEqual(self.done.get_nowait(), "DEL")
     self.sl = locking.SharedLock(self.sl.name)
Exemplo n.º 2
0
 def testExclusiveBlocksDelete(self):
     self.sl.acquire()
     self._addThread(target=self._doItDelete)
     self.assertRaises(queue.Empty, self.done.get_nowait)
     self.sl.release()
     self._waitThreads()
     self.assertEqual(self.done.get_nowait(), "DEL")
     self.sl = locking.SharedLock(self.sl.name)
Exemplo n.º 3
0
    def testAcquireTimeoutWithSpuriousNotifications(self):
        ready = threading.Event()
        locked = threading.Event()
        req = Queue.Queue(0)

        epoch = 4000.0
        timeout = 60.0

        def check_end(now):
            self.assertFalse(locked.isSet())

            # If we waited long enough (in virtual time), tell main thread to release
            # lock, otherwise tell it to notify once more
            req.put(now < (epoch + (timeout * 0.8)))

        time_fn = self._FakeTimeForSpuriousNotifications(epoch, check_end).time

        sl = locking.SharedLock("test", _time_fn=time_fn)

        # Acquire in exclusive mode
        sl.acquire(shared=0)

        def fn():
            self.assertTrue(
                sl.acquire(shared=0, timeout=timeout, test_notify=ready.set))
            locked.set()
            sl.release()
            self.done.put("success")

        # Start acquire with timeout and wait for it to be ready
        self._addThread(target=fn)
        ready.wait()

        # The separate thread is now waiting to acquire the lock, so start sending
        # spurious notifications.

        # Wait for separate thread to ask for another notification
        count = 0
        while req.get():
            # After sending the notification, the lock will take a short amount of
            # time to notice and to retrieve the current time
            sl._notify_topmost()
            count += 1

        self.assertTrue(count > 100, "Not enough notifications were sent")

        self.assertFalse(locked.isSet())

        # Some notifications have been sent, now actually release the lock
        sl.release()

        # Wait for lock to be acquired
        locked.wait()

        self._waitThreads()

        self.assertEqual(self.done.get_nowait(), "success")
        self.assertRaises(Queue.Empty, self.done.get_nowait)
Exemplo n.º 4
0
    def __init__(self, owner, pending_fn):
        """Initializes this class.

    """
        self._owner = owner
        self._pending_fn = pending_fn

        # The lock monitor runs in another thread, hence locking is necessary
        self._lock = locking.SharedLock("PendingHttpRequests")
        self.acquire = self._lock.acquire
        self.release = self._lock.release
Exemplo n.º 5
0
 def testDeletePendingSharersExclusiveDelete(self):
     self.sl.acquire()
     self._addThread(target=self._doItSharer)
     self._addThread(target=self._doItSharer)
     self._addThread(target=self._doItExclusive)
     self._addThread(target=self._doItDelete)
     self.sl.delete()
     self._waitThreads()
     # The threads who were pending return ERR
     for _ in range(4):
         self.assertEqual(self.done.get_nowait(), "ERR")
     self.sl = locking.SharedLock(self.sl.name)
Exemplo n.º 6
0
def main():
    (opts, _) = ParseOptions()

    lock = locking.SharedLock("TestLock")

    state = State(opts.thread_count)

    lock.acquire(shared=0)
    try:
        for i in range(opts.thread_count):
            t = threading.Thread(target=_Counter, args=(lock, state, i))
            t.setDaemon(True)
            t.start()

        start = time.clock()
    finally:
        lock.release()

    while True:
        if (time.clock() - start) > opts.duration:
            break
        time.sleep(0.1)

    # Make sure we get a consistent view
    lock.acquire(shared=0)

    lock_cputime = time.clock() - start

    res = resource.getrusage(resource.RUSAGE_SELF)

    print "Total number of acquisitions: %s" % state.total_count
    print "Per-thread acquisitions:"
    for (i, count) in enumerate(state.counts):
        print("  Thread %s: %d (%0.1f%%)" %
              (i, count, (100.0 * count / state.total_count)))

    print "Benchmark CPU time: %0.3fs" % lock_cputime
    print("Average time per lock acquisition: %0.5fms" %
          (1000.0 * lock_cputime / state.total_count))
    print "Process:"
    print "  User time: %0.3fs" % res.ru_utime
    print "  System time: %0.3fs" % res.ru_stime
    print "  Total time: %0.3fs" % (res.ru_utime + res.ru_stime)

    # Exit directly without attempting to clean up threads
    os._exit(0)  # pylint: disable=W0212
Exemplo n.º 7
0
    def setUp(self):
        _ThreadedTestCase.setUp(self)
        self.sl = locking.SharedLock("TestSharedLock")

        self.assertTrue(repr(self.sl).startswith("<"))
        self.assertTrue("name=TestSharedLock" in repr(self.sl))
Exemplo n.º 8
0
 def setUp(self):
     _ThreadedTestCase.setUp(self)
     self.sl = locking.SharedLock("TestSharedLockInCondition")
     self.setCondition()
Exemplo n.º 9
0
import gc
import itertools

from ganeti import constants
from ganeti import locking
from ganeti import errors
from ganeti import utils
from ganeti import compat
from ganeti import objects
from ganeti import query

import testutils

# This is used to test the ssynchronize decorator.
# Since it's passed as input to a decorator it must be declared as a global.
_decoratorlock = locking.SharedLock("decorator lock")

#: List for looping tests
ITERATIONS = range(8)


def _Repeat(fn):
    """Decorator for executing a function many times"""
    def wrapper(*args, **kwargs):
        for i in ITERATIONS:
            fn(*args, **kwargs)

    return wrapper


def SafeSleep(duration):