Пример #1
0
    def test_cancel_basic(self):
        """
        Start a workload and cancel it.  Verify that it was actually cancelled before all the work was finished.
        """
        if Request.global_thread_pool.num_workers == 0:
            raise nose.SkipTest

        def workload():
            time.sleep(0.1)
            return 1

        got_cancel = [False]
        workcounter = [0]

        def big_workload():
            try:
                requests = []
                for i in range(100):
                    requests.append(Request(workload))

                for r in requests:
                    workcounter[0] += r.wait()

                assert False, "Shouldn't get to this line.  This test is designed so that big_workload should be cancelled before it finishes all its work"
                for r in requests:
                    assert not r.cancelled
            except Request.CancellationException:
                got_cancel[0] = True
            except Exception as ex:
                import traceback
                traceback.print_exc()
                raise

        completed = [False]

        def handle_complete(result):
            completed[0] = True

        req = Request(big_workload)
        req.notify_finished(handle_complete)
        req.submit()

        while workcounter[0] == 0:
            time.sleep(0.001)

        req.cancel()
        time.sleep(1)

        assert req.cancelled

        assert not completed[0]
        assert got_cancel[0]

        # Make sure this test is functioning properly:
        # The cancellation should have occurred in the middle (not before the request even got started)
        # If not, then adjust the timing of the cancellation, above.
        assert workcounter[
            0] != 0, "This timing-sensitive test needs to be tweaked."
        assert workcounter[
            0] != 100, "This timing-sensitive test needs to be tweaked."
Пример #2
0
    def test_cancel_basic(self):
        """
        Start a workload and cancel it.  Verify that it was actually cancelled before all the work was finished.
        """
        if Request.global_thread_pool.num_workers == 0:
            raise nose.SkipTest
        
        def workload():
            time.sleep(0.1)
            return 1
         
        got_cancel = [False]
        workcounter = [0]
        def big_workload():
            try:
                requests = []
                for i in range(100):
                    requests.append( Request(workload) )
                 
                for r in requests:
                    workcounter[0] += r.wait()
                 
                assert False, "Shouldn't get to this line.  This test is designed so that big_workload should be cancelled before it finishes all its work"
                for r in requests:
                    assert not r.cancelled
            except Request.CancellationException:
                got_cancel[0] = True
            except Exception as ex:
                import traceback
                traceback.print_exc()
                raise
         
        completed = [False]
        def handle_complete( result ):
            completed[0] = True
         
        req = Request( big_workload )
        req.notify_finished( handle_complete )
        req.submit()
 
        while workcounter[0] == 0:
            time.sleep(0.001)
             
        req.cancel()
        time.sleep(1)
         
        assert req.cancelled
         
        assert not completed[0]
        assert got_cancel[0]
         
        # Make sure this test is functioning properly:
        # The cancellation should have occurred in the middle (not before the request even got started)
        # If not, then adjust the timing of the cancellation, above.
        assert workcounter[0] != 0, "This timing-sensitive test needs to be tweaked."
        assert workcounter[0] != 100, "This timing-sensitive test needs to be tweaked."
Пример #3
0
    def test_if_request_has_been_cancelled_callback_should_still_be_called(self):
        cb = mock.Mock()

        req = Request(lambda: 42)
        req.cancel()
        req.add_done_callback(cb)
        req.submit()

        with pytest.raises(Request.InvalidRequestException):
            req.wait()

        cb.assert_called_once_with(req)
Пример #4
0
 def test_early_cancel(self):
     """
     If you try to wait for a request after it's already been cancelled, you get a InvalidRequestException.
     """
     def f():
         pass
     req = Request(f)
     req.cancel()
     try:
         req.wait()
     except Request.InvalidRequestException:
         pass
     else:
         assert False, "Expected a Request.InvalidRequestException because we're waiting for a request that's already been cancelled."
Пример #5
0
 def test_early_cancel(self):
     """
     If you try to wait for a request after it's already been cancelled, you get a InvalidRequestException.
     """
     def f():
         pass
     req = Request(f)
     req.cancel()
     try:
         req.wait()
     except Request.InvalidRequestException:
         pass
     else:
         assert False, "Expected a Request.InvalidRequestException because we're waiting for a request that's already been cancelled."
Пример #6
0
    def test_cancel_basic(self):
        """
        Start a workload and cancel it.  Verify that it was actually cancelled before all the work was finished.
        """
        counter_lock = threading.RLock()

        def workload():
            time.sleep(0.1)
            return 1
        
        got_cancel = [False]
        workcounter = [0]
        def big_workload():
            try:
                requests = []
                for i in range(100):
                    requests.append( Request(workload) )
                
                for r in requests:
                    workcounter[0] += r.wait()
                
                assert False, "Shouldn't get to this line.  This test is designed so that big_workload should be cancelled before it finishes all its work"
                for r in requests:
                    assert not r.cancelled
            except Request.CancellationException:
                got_cancel[0] = True
        
        completed = [False]
        def handle_complete( result ):
            completed[0] = True
        
        req = Request( big_workload )
        req.notify_finished( handle_complete )
        req.submit()
        time.sleep(.5)
        req.cancel()
        
        assert req.cancelled
        
        time.sleep(2)
        assert not completed[0]
        assert got_cancel[0]
        
        # Make sure this test is functioning properly:
        # The cancellation should have occurred in the middle (not before the request even got started)
        # If not, then adjust the timing of the cancellation, above.
        assert workcounter[0] != 0
        assert workcounter[0] != 100
Пример #7
0
    def test_dont_cancel_shared_request(self):
        """
        Test that a request isn't cancelled if it has requests pending for it.
        """
        if Request.global_thread_pool.num_workers == 0:
            raise nose.SkipTest

        cancelled_requests = []

        def f1():
            time.sleep(1)
            return "RESULT"

        r1 = Request(f1)
        r1.notify_cancelled(partial(cancelled_requests.append, 1))

        def f2():
            try:
                return r1.wait()
            except:
                cancelled_requests.append(2)

        r2 = Request(f2)

        def f3():
            try:
                return r1.wait()
            except:
                cancelled_requests.append(3)

        r3 = Request(f3)

        def otherThread():
            r2.wait()

        t = threading.Thread(target=otherThread)
        t.start()
        r3.submit()

        time.sleep(0.5)

        # By now both r2 and r3 are waiting for the result of r1
        # Cancelling r3 should not cancel r1.
        r3.cancel()

        t.join()  # Wait for r2 to finish

        time.sleep(0.5)

        assert r1.started
        assert r1.finished
        assert not r1.cancelled  # Not cancelled, even though we cancelled a request that was waiting for it.
        assert 1 not in cancelled_requests

        assert r2.started
        assert r2.finished
        assert not r2.cancelled  # Not cancelled.
        assert 1 not in cancelled_requests
        assert r2.wait() == "RESULT"

        assert r3.started
        assert r3.finished
        assert r3.cancelled  # Successfully cancelled.
        assert 3 in cancelled_requests
Пример #8
0
    def test_dont_cancel_shared_request(self):
        """
        Test that a request isn't cancelled if it has requests pending for it.
        """

        cancelled_requests = []
        
        def f1():
            time.sleep(1)
            return "RESULT"
        
        r1 = Request(f1)
        r1.notify_cancelled( partial(cancelled_requests.append, 1) )
        
        def f2():
            try:
                return r1.wait()
            except:
                cancelled_requests.append(2)

        r2 = Request(f2)
        
        def f3():
            try:
                return r1.wait()
            except:
                cancelled_requests.append(3)
        
        r3 = Request(f3)
        
        def otherThread():
            r2.wait()

        t = threading.Thread(target=otherThread)
        t.start()
        r3.submit()
        
        time.sleep(0.5)
        
        # By now both r2 and r3 are waiting for the result of r1
        # Cancelling r3 should not cancel r1.
        r3.cancel()

        t.join() # Wait for r2 to finish

        time.sleep(0.5)

        assert r1.started
        assert r1.finished        
        assert not r1.cancelled # Not cancelled, even though we cancelled a request that was waiting for it.
        assert 1 not in cancelled_requests 

        assert r2.started
        assert r2.finished
        assert not r2.cancelled # Not cancelled.
        assert 1 not in cancelled_requests
        assert r2.wait() == "RESULT" 

        assert r3.started
        assert r3.finished
        assert r3.cancelled # Successfully cancelled.
        assert 3 in cancelled_requests