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."
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
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