def test_basic(self): """ Fire a couple requests and check the answer they give. """ def someWork(): time.sleep(0.001) return "Hello," callback_result = [''] def callback(result): callback_result[0] = result def test(s): req = Request(someWork) req.notify_finished(callback) s2 = req.wait() time.sleep(0.001) return s2 + s req = Request( partial(test, s = " World!") ) req.notify_finished(callback) # Wait for the result assert req.wait() == "Hello, World!" # Wait for it assert req.wait() == "Hello, World!" # It's already finished, should be same answer assert callback_result[0] == "Hello, World!" # From the callback requests = [] for i in range(10): req = Request( partial(test, s = "hallo %d" %i) ) requests.append(req) for r in requests: r.wait()
def test_callWaitDuringCallback(self): """ When using request.notify_finished(...) to handle request completions, the handler should be allowed to call request.wait() on the request that it's handling. """ def handler(req, result): req.wait() def workFn(): pass req = Request(workFn) req.notify_finished( partial(handler, req) ) #req.submit() req.wait()
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_block_during_calback(self): """ It is valid for request finish handlers to fire off and wait for requests. This tests that feature. """ def workload(): time.sleep(0.1) return 1 total_result = [0] def handler(result): req = Request(workload) total_result[0] = result + req.wait() # Waiting on some other request from WITHIN a request callback req = Request( workload ) req.notify_finished( handler ) assert req.wait() == 1 assert total_result[0] == 2
def someWork(depth, force=False, i=-1): #print 'depth=', depth, 'i=', i if depth > 0 and (force or random.random() > 0.5): requests = [] for i in range(10): req = Request( partial(someWork, depth=depth-1, i=i) ) req.notify_finished( partial(completionHandler, req=req) ) requests.append(req) allRequests.append(req) requestLock.acquire() requestCounter[0] += 1 requestLock.release() for r in requests: r.wait() return requestCounter[0]
def test_callbacks_before_wait_returns(self): """ If the user adds callbacks to the request via notify_finished() BEFORE the request is submitted, then wait() should block for the completion of all those callbacks before returning. Any callbacks added AFTER the request has already been submitted are NOT guaranteed to be executed before wait() returns, but they will still be executed. """ def someQuickWork(): return 42 callback_results = [] def slowCallback(n, result): time.sleep(0.1) callback_results.append(n) req = Request( someQuickWork ) req.notify_finished( partial(slowCallback, 1) ) req.notify_finished( partial(slowCallback, 2) ) req.notify_finished( partial(slowCallback, 3) ) result = req.wait() assert result == 42 assert callback_results == [1,2,3], "wait() returned before callbacks were complete! Got: {}".format( callback_results ) req.notify_finished( partial(slowCallback, 4) ) req.wait() assert callback_results == [1,2,3,4], "Callback on already-finished request wasn't executed."
def test(s): req = Request(someWork) req.notify_finished(callback) s2 = req.wait() time.sleep(0.001) return s2 + s