def testWaitAny_CancellerFinishesFirst(self): """Wait on a cancelled canceller and a blocked RPC returns canceller.""" blocking_event = threading.Event() class BlockingRPC(BarrierRPC): """RPC that blocks until blocking_event.""" def _SendRequest(self, *args, **kwargs): blocking_event.wait() super(BlockingRPC, self)._SendRequest(*args, **kwargs) blocking_rpc = BlockingRPC(self.stub) self.stub.SetRpcToReturn(blocking_rpc) rpc = apiproxy_stub_map.UserRPC('barrier', stubmap=self.stubmap) rpc.make_call('call', 0, None) wait_canceller = apiproxy_stub_map.WaitCanceller() wait_canceller.cancel() finished_rpc = apiproxy_stub_map.UserRPC.wait_any( [rpc, wait_canceller]) self.assertEqual(wait_canceller, finished_rpc) finished_rpc = apiproxy_stub_map.UserRPC.wait_any( [wait_canceller, rpc]) self.assertEqual(wait_canceller, finished_rpc) blocking_event.set()
def _create_rpc(deadline): """Create RPC object for file service. Args: deadling: Request deadline in seconds. """ return apiproxy_stub_map.UserRPC('file', deadline)
def testWaitAny_RpcFinishesFirst(self): """Wait on a cancelled canceller and finished RPC should return the RPC.""" rpc_finished = threading.Event() class TestRPC(BarrierRPC): """RPC that blocks until blocking_event.""" def _SendRequest(self, *args, **kwargs): super(TestRPC, self)._SendRequest(*args, **kwargs) rpc_finished.set() blocking_rpc = TestRPC(self.stub) self.stub.SetRpcToReturn(blocking_rpc) rpc = apiproxy_stub_map.UserRPC('barrier', stubmap=self.stubmap) rpc.make_call('call', 0, None) rpc_finished.wait() wait_canceller = apiproxy_stub_map.WaitCanceller() wait_canceller.cancel() finished_rpc = apiproxy_stub_map.UserRPC.wait_any( [rpc, wait_canceller]) self.assertEqual(rpc, finished_rpc) finished_rpc = apiproxy_stub_map.UserRPC.wait_any( [wait_canceller, rpc]) self.assertEqual(rpc, finished_rpc)
def testCheckSuccess_Exception(self): """Test that check_success() doesn't overwrite low-level exceptions.""" rpc1 = apiproxy_stub_map.UserRPC('barrier', stubmap=self.stubmap) rpc1.make_call('error', 42, None) rpc = apiproxy_stub_map.UserRPC.wait_any([rpc1]) self.assertIs(rpc1, rpc) self.assertRaises(ZeroDivisionError, rpc.check_success)
def testCheckSuccess(self): """Test that check_success() doesn't raise InterruptedError.""" rpc1 = apiproxy_stub_map.UserRPC('barrier', stubmap=self.stubmap) rpc1.make_call('call', 42, None) rpc = apiproxy_stub_map.UserRPC.wait_any([rpc1]) self.assertIs(rpc1, rpc) rpc.check_success()
def _Callback(arg=i): calls.append(arg + 100) other_rpc = apiproxy_stub_map.UserRPC('barrier', stubmap=self.stubmap) other_rpc.make_call('call', arg, None) other_rpc.wait() calls.append(arg + 200)
def testNoNestedCallbacks(self): """Test that callbacks will never be nested inside each other.""" rpcs = [] calls = [] for i in range(5): def _Callback(arg=i): calls.append(arg + 100) other_rpc = apiproxy_stub_map.UserRPC('barrier', stubmap=self.stubmap) other_rpc.make_call('call', arg, None) other_rpc.wait() calls.append(arg + 200) rpc = apiproxy_stub_map.UserRPC('barrier', callback=_Callback, stubmap=self.stubmap) rpc.make_call('call', i, None) rpcs.append(rpc) apiproxy_stub_map.UserRPC.wait_all([rpcs[1]]) self.assertCountEqual(calls, [101, 201]) calls = [] apiproxy_stub_map.UserRPC.wait_all(rpcs[:3]) self.assertCountEqual(calls, [100, 200, 102, 202]) calls = [] apiproxy_stub_map.UserRPC.wait_all(rpcs) self.assertCountEqual(calls, [103, 203, 104, 204])
def create_rpc(deadline=None, callback=None): """Creates an RPC object for use with the urlfetch API. Args: deadline: Optional deadline in seconds for the operation; the default is a system-specific deadline (typically 5 seconds). callback: Optional callable to invoke on completion. Returns: An apiproxy_stub_map.UserRPC object specialized for this service. """ return apiproxy_stub_map.UserRPC('urlfetch', deadline, callback)
def create_rpc(deadline=None, callback=None): """Creates an RPC object to use with the Blobstore API. Args: deadline: Optional deadline in seconds for the operation; the default value is a system-specific deadline, typically 5 seconds. callback: Optional callable to invoke on completion. Returns: An `apiproxy_stub_map.UserRPC` object that is specialized for this service. """ return apiproxy_stub_map.UserRPC('blobstore', deadline, callback)
def Call(request): try: barrier_rpc = TestRpc(self.stub) self.stub.SetRpcToReturn(barrier_rpc) rpc1 = apiproxy_stub_map.UserRPC('barrier', stubmap=self.stubmap) rpc1.make_call('call', request, None) rpc = apiproxy_stub_map.UserRPC.wait_any([rpc1]) self.assertIs(rpc1, rpc) rpc.check_success() except: exceptions.append(traceback.format_exc())
def create_rpc(deadline=None, callback=None): """Creates an RPC object for use with the App Identity API. Args: deadline: Optional deadline in seconds for the operation; the default value is a system-specific deadline, typically 5 seconds. callback: Optional callable to invoke on completion. Returns: An `apiproxy_stub_map.UserRPC` object specialized for this service. """ return apiproxy_stub_map.UserRPC(_APP_IDENTITY_SERVICE_NAME, deadline, callback)
def get_queue_names(app_id=None): """Returns a list with all non-special queue names for app_id.""" rpc = apiproxy_stub_map.UserRPC('taskqueue') request = taskqueue_service_pb.TaskQueueFetchQueuesRequest() response = taskqueue_service_pb.TaskQueueFetchQueuesResponse() if app_id: request.set_app_id(app_id) request.set_max_rows(100) queues = ['default'] try: rpc.make_call('FetchQueues', request, response) rpc.check_success() for queue in response.queue_list(): if (queue.mode() == taskqueue_service_pb.TaskQueueMode.PUSH and not queue.queue_name().startswith('__') and queue.queue_name() != 'default'): queues.append(queue.queue_name()) except Exception, e: logging.exception('Failed to get queue names: %s', str(e))
def testWaitAll(self): """Test UserRPC.wait_all().""" rpcs = [] calls = [] for i in range(5): def _Callback(arg=i): calls.append(arg) rpc = apiproxy_stub_map.UserRPC('barrier', callback=_Callback, stubmap=self.stubmap) rpc.make_call('call', i, None) rpcs.append(rpc) apiproxy_stub_map.UserRPC.wait_all([rpcs[3], rpcs[2], rpcs[1]]) self.assertCountEqual(calls, [1, 2, 3]) calls = [] apiproxy_stub_map.UserRPC.wait_all(rpcs) self.assertCountEqual(calls, [0, 4]) apiproxy_stub_map.UserRPC.wait_all([])
def testWaitAny(self): """Test UserRPC.wait_any().""" rpcs = [] calls = [] for i in range(5): def _Callback(arg=i): calls.append(arg) rpc = apiproxy_stub_map.UserRPC('barrier', callback=_Callback, stubmap=self.stubmap) rpc.make_call('call', i, None) rpcs.append(rpc) wait_for_rpcs = [rpcs[3], rpcs[2], rpcs[1]] rpc = apiproxy_stub_map.UserRPC.wait_any(wait_for_rpcs) self.assertIn(rpc, wait_for_rpcs) self.assertLen(calls, 1) first_call = calls[0] self.assertEqual(rpc, rpcs[first_call]) calls = [] rpc = apiproxy_stub_map.UserRPC.wait_any([rpcs[0]]) self.assertEqual(rpc, rpcs[0]) self.assertEqual(calls, [0]) calls = [] rpcs = set(rpcs) while rpcs: rpc = apiproxy_stub_map.UserRPC.wait_any(rpcs) rpcs.remove(rpc) expected_calls = [1, 2, 3, 4] expected_calls.remove(first_call) self.assertCountEqual(calls, expected_calls) rpc = apiproxy_stub_map.UserRPC.wait_any([]) self.assertIsNone(rpc)
def _GetRpc(): return apiproxy_stub_map.UserRPC('modules')
def create_rpc(deadline=None, callback=None): return apiproxy_stub_map.UserRPC('datastore_v3', deadline, callback)