def testRequestLockSemantics(self):
     """
     To be used with threading.Condition, RequestLock objects MUST NOT have RLock semantics.
     It is important that the RequestLock is NOT re-entrant.
     """
     with RequestLock() as lock:
         assert not lock.acquire(0)
Exemple #2
0
    def testRequestLock(self):
        """
        Test the special Request-aware lock.
         
        Launch 99 requests and threads that all must fight over access to the same list.
        The list will eventually be 0,1,2...99, and each request will append a single number to the list.
        Each request must wait its turn before it can append it's number and finish.
        """
        # This test doesn't work if the request system is working in single-threaded 'debug' mode.
        # It depends on concurrent execution to make progress.  Otherwise it hangs.
        if Request.global_thread_pool.num_workers == 0:
            raise nose.SkipTest

        req_lock = RequestLock()
        l = [0]

        def append_n(n):
            #print "Starting append_{}\n".format(n)
            while True:
                with req_lock:
                    if l[-1] == n - 1:
                        #print "***** Appending {}".format(n)
                        l.append(n)
                        return

        # Create 50 requests
        N = 50
        reqs = []
        for i in range(1, 2 * N, 2):
            req = Request(partial(append_n, i))
            reqs.append(req)

        # Create 49 threads
        thrds = []
        for i in range(2, 2 * N, 2):
            thrd = threading.Thread(target=partial(append_n, i))
            thrds.append(thrd)

        # Submit in reverse order to ensure that no request finishes until they have all been started.
        # This proves that the requests really are being suspended.
        for req in reversed(reqs):
            req.submit()

        # Start all the threads
        for thrd in reversed(thrds):
            thrd.start()

        # All requests must finish
        for req in reqs:
            req.wait()

        # All threads should finish
        for thrd in thrds:
            thrd.join()

        assert l == list(
            range(100)), "Requests and/or threads finished in the wrong order!"
Exemple #3
0
    def testRequestLockSemantics(self):
        """
        To be used with threading.Condition, RequestLock objects MUST NOT have RLock semantics.
        It is important that the RequestLock is NOT re-entrant.
        """
        if Request.global_thread_pool.num_workers == 0:
            raise nose.SkipTest

        with RequestLock() as lock:
            assert not lock.acquire(0)
    def testRequestLock(self):
        """
        Test the special Request-aware lock.
        
        Launch 99 requests and threads that all must fight over access to the same list.
        The list will eventually be 0,1,2...99, and each request will append a single number to the list.
        Each request must wait its turn before it can append it's number and finish.
        """
        req_lock = RequestLock()
        l = [0]

        def append_n(n):
            #print "Starting append_{}\n".format(n)
            while True:
                with req_lock:
                    if l[-1] == n - 1:
                        #print "***** Appending {}".format(n)
                        l.append(n)
                        return

        # Create 50 requests
        reqs = []
        for i in range(1, 100, 2):
            req = Request(partial(append_n, i))
            reqs.append(req)

        # Create 49 threads
        thrds = []
        for i in range(2, 100, 2):
            thrd = threading.Thread(target=partial(append_n, i))
            thrds.append(thrd)

        # Submit in reverse order to ensure that no request finishes until they have all been started.
        # This proves that the requests really are being suspended.
        for req in reversed(reqs):
            req.submit()

        # Start all the threads
        for thrd in reversed(thrds):
            thrd.start()

        # All requests must finish
        for req in reqs:
            req.wait()

        # All threads should finish
        for thrd in thrds:
            thrd.join()

        assert l == list(
            range(100)), "Requests and/or threads finished in the wrong order!"