コード例 #1
0
def _send_response(rpc_event, state, serialized_response):
    with state.condition:
        if state.client is _CANCELLED or state.statused:
            return False
        else:
            if state.initial_metadata_allowed:
                operations = (
                    cygrpc.operation_send_initial_metadata(
                        _common.EMPTY_METADATA, _EMPTY_FLAGS),
                    cygrpc.operation_send_message(serialized_response,
                                                  _EMPTY_FLAGS),
                )
                state.initial_metadata_allowed = False
                token = _SEND_INITIAL_METADATA_AND_SEND_MESSAGE_TOKEN
            else:
                operations = (cygrpc.operation_send_message(
                    serialized_response, _EMPTY_FLAGS), )
                token = _SEND_MESSAGE_TOKEN
            rpc_event.operation_call.start_server_batch(
                cygrpc.Operations(operations), _send_message(state, token))
            state.due.add(token)
            while True:
                state.condition.wait()
                if token not in state.due:
                    return state.client is not _CANCELLED and not state.statused
コード例 #2
0
ファイル: _server.py プロジェクト: wuyunhao/grpc
def _send_response(rpc_event, state, serialized_response):
  with state.condition:
    if state.client is _CANCELLED or state.statused:
      return False
    else:
      if state.initial_metadata_allowed:
        operations = (
            cygrpc.operation_send_initial_metadata(
                _EMPTY_METADATA, _EMPTY_FLAGS),
            cygrpc.operation_send_message(serialized_response, _EMPTY_FLAGS),
        )
        state.initial_metadata_allowed = False
        token = _SEND_INITIAL_METADATA_AND_SEND_MESSAGE_TOKEN
      else:
        operations = (
            cygrpc.operation_send_message(serialized_response, _EMPTY_FLAGS),
        )
        token = _SEND_MESSAGE_TOKEN
      rpc_event.operation_call.start_server_batch(
          cygrpc.Operations(operations), _send_message(state, token))
      state.due.add(token)
      while True:
        state.condition.wait()
        if token not in state.due:
          return state.client is not _CANCELLED and not state.statused
コード例 #3
0
    def __call__(self):
        with self._state.condition:
            self._state.parked_handlers += 1
            if self._state.parked_handlers == test_constants.THREAD_CONCURRENCY:
                self._state.condition.notify_all()
            while not self._state.handlers_released:
                self._state.condition.wait()

        with self._lock:
            self._call.start_server_batch(
                cygrpc.Operations(
                    (cygrpc.operation_receive_close_on_server(_EMPTY_FLAGS),)),
                _RECEIVE_CLOSE_ON_SERVER_TAG)
            self._call.start_server_batch(
                cygrpc.Operations(
                    (cygrpc.operation_receive_message(_EMPTY_FLAGS),)),
                _RECEIVE_MESSAGE_TAG)
        first_event = self._completion_queue.poll()
        if _is_cancellation_event(first_event):
            self._completion_queue.poll()
        else:
            with self._lock:
                operations = (
                    cygrpc.operation_send_initial_metadata(_EMPTY_METADATA,
                                                           _EMPTY_FLAGS),
                    cygrpc.operation_send_message(b'\x79\x57', _EMPTY_FLAGS),
                    cygrpc.operation_send_status_from_server(
                        _EMPTY_METADATA, cygrpc.StatusCode.ok, b'test details!',
                        _EMPTY_FLAGS),)
                self._call.start_server_batch(
                    cygrpc.Operations(operations), _SERVER_COMPLETE_CALL_TAG)
            self._completion_queue.poll()
            self._completion_queue.poll()
コード例 #4
0
    def __call__(self):
        with self._state.condition:
            self._state.parked_handlers += 1
            if self._state.parked_handlers == test_constants.THREAD_CONCURRENCY:
                self._state.condition.notify_all()
            while not self._state.handlers_released:
                self._state.condition.wait()

        with self._lock:
            self._call.start_server_batch(
                (cygrpc.operation_receive_close_on_server(_EMPTY_FLAGS), ),
                _RECEIVE_CLOSE_ON_SERVER_TAG)
            self._call.start_server_batch(
                (cygrpc.operation_receive_message(_EMPTY_FLAGS), ),
                _RECEIVE_MESSAGE_TAG)
        first_event = self._completion_queue.poll()
        if _is_cancellation_event(first_event):
            self._completion_queue.poll()
        else:
            with self._lock:
                operations = (
                    cygrpc.operation_send_initial_metadata(
                        _EMPTY_METADATA, _EMPTY_FLAGS),
                    cygrpc.operation_send_message(b'\x79\x57', _EMPTY_FLAGS),
                    cygrpc.operation_send_status_from_server(
                        _EMPTY_METADATA, cygrpc.StatusCode.ok,
                        b'test details!', _EMPTY_FLAGS),
                )
                self._call.start_server_batch(operations,
                                              _SERVER_COMPLETE_CALL_TAG)
            self._completion_queue.poll()
            self._completion_queue.poll()
コード例 #5
0
ファイル: _low.py プロジェクト: zhouruiapple/grpc
 def start_batch(self, ops, tag):
     translated_ops = []
     for op in ops:
         if op.type == _types.OpType.SEND_INITIAL_METADATA:
             translated_op = cygrpc.operation_send_initial_metadata(
                 cygrpc.Metadata(
                     cygrpc.Metadatum(key, value)
                     for key, value in op.initial_metadata))
         elif op.type == _types.OpType.SEND_MESSAGE:
             translated_op = cygrpc.operation_send_message(op.message)
         elif op.type == _types.OpType.SEND_CLOSE_FROM_CLIENT:
             translated_op = cygrpc.operation_send_close_from_client()
         elif op.type == _types.OpType.SEND_STATUS_FROM_SERVER:
             translated_op = cygrpc.operation_send_status_from_server(
                 cygrpc.Metadata(
                     cygrpc.Metadatum(key, value)
                     for key, value in op.trailing_metadata),
                 op.status.code, op.status.details)
         elif op.type == _types.OpType.RECV_INITIAL_METADATA:
             translated_op = cygrpc.operation_receive_initial_metadata()
         elif op.type == _types.OpType.RECV_MESSAGE:
             translated_op = cygrpc.operation_receive_message()
         elif op.type == _types.OpType.RECV_STATUS_ON_CLIENT:
             translated_op = cygrpc.operation_receive_status_on_client()
         elif op.type == _types.OpType.RECV_CLOSE_ON_SERVER:
             translated_op = cygrpc.operation_receive_close_on_server()
         else:
             raise ValueError('unexpected operation type {}'.format(
                 op.type))
         translated_ops.append(translated_op)
     return self.call.start_batch(cygrpc.Operations(translated_ops), tag)
コード例 #6
0
ファイル: _channel.py プロジェクト: gnirodi/grpc
 def consume_request_iterator():
   for request in request_iterator:
     serialized_request = _common.serialize(request, request_serializer)
     with state.condition:
       if state.code is None and not state.cancelled:
         if serialized_request is None:
           call.cancel()
           details = 'Exception serializing request!'
           _abort(state, grpc.StatusCode.INTERNAL, details)
           return
         else:
           operations = (
               cygrpc.operation_send_message(
                   serialized_request, _EMPTY_FLAGS),
           )
           call.start_client_batch(cygrpc.Operations(operations),
                                   event_handler)
           state.due.add(cygrpc.OperationType.send_message)
           while True:
             state.condition.wait()
             if state.code is None:
               if cygrpc.OperationType.send_message not in state.due:
                 break
             else:
               return
       else:
         return
   with state.condition:
     if state.code is None:
       operations = (
           cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
       )
       call.start_client_batch(cygrpc.Operations(operations), event_handler)
       state.due.add(cygrpc.OperationType.send_close_from_client)
コード例 #7
0
ファイル: _channel.py プロジェクト: gnirodi/grpc
 def __call__(self, request, timeout=None, metadata=None, credentials=None):
   deadline, deadline_timespec, serialized_request, rendezvous = (
       _start_unary_request(request, timeout, self._request_serializer))
   if serialized_request is None:
     raise rendezvous
   else:
     state = _RPCState(_UNARY_STREAM_INITIAL_DUE, None, None, None, None)
     call = self._create_managed_call(
         None, 0, self._method, None, deadline_timespec)
     if credentials is not None:
       call.set_credentials(credentials._credentials)
     event_handler = _event_handler(state, call, self._response_deserializer)
     with state.condition:
       call.start_client_batch(
           cygrpc.Operations(
               (cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),)),
           event_handler)
       operations = (
           cygrpc.operation_send_initial_metadata(
               _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
           cygrpc.operation_send_message(serialized_request, _EMPTY_FLAGS),
           cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
           cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
       )
       call.start_client_batch(cygrpc.Operations(operations), event_handler)
     return _Rendezvous(state, call, self._response_deserializer, deadline)
コード例 #8
0
ファイル: _low.py プロジェクト: 201528013359030/grpc
 def start_batch(self, ops, tag):
   translated_ops = []
   for op in ops:
     if op.type == _types.OpType.SEND_INITIAL_METADATA:
       translated_op = cygrpc.operation_send_initial_metadata(
           cygrpc.Metadata(
               cygrpc.Metadatum(key, value)
               for key, value in op.initial_metadata),
           op.flags)
     elif op.type == _types.OpType.SEND_MESSAGE:
       translated_op = cygrpc.operation_send_message(op.message, op.flags)
     elif op.type == _types.OpType.SEND_CLOSE_FROM_CLIENT:
       translated_op = cygrpc.operation_send_close_from_client(op.flags)
     elif op.type == _types.OpType.SEND_STATUS_FROM_SERVER:
       translated_op = cygrpc.operation_send_status_from_server(
           cygrpc.Metadata(
               cygrpc.Metadatum(key, value)
               for key, value in op.trailing_metadata),
           op.status.code,
           op.status.details,
           op.flags)
     elif op.type == _types.OpType.RECV_INITIAL_METADATA:
       translated_op = cygrpc.operation_receive_initial_metadata(
           op.flags)
     elif op.type == _types.OpType.RECV_MESSAGE:
       translated_op = cygrpc.operation_receive_message(op.flags)
     elif op.type == _types.OpType.RECV_STATUS_ON_CLIENT:
       translated_op = cygrpc.operation_receive_status_on_client(
           op.flags)
     elif op.type == _types.OpType.RECV_CLOSE_ON_SERVER:
       translated_op = cygrpc.operation_receive_close_on_server(op.flags)
     else:
       raise ValueError('unexpected operation type {}'.format(op.type))
     translated_ops.append(translated_op)
   return self.call.start_batch(cygrpc.Operations(translated_ops), tag)
コード例 #9
0
 def __call__(self, request, timeout=None, metadata=None, credentials=None):
     deadline, deadline_timespec, serialized_request, rendezvous = (
         _start_unary_request(request, timeout, self._request_serializer))
     if serialized_request is None:
         raise rendezvous
     else:
         state = _RPCState(_UNARY_STREAM_INITIAL_DUE, None, None, None,
                           None)
         call, drive_call = self._managed_call(None, 0, self._method, None,
                                               deadline_timespec)
         if credentials is not None:
             call.set_credentials(credentials._credentials)
         event_handler = _event_handler(state, call,
                                        self._response_deserializer)
         with state.condition:
             call.start_client_batch(
                 cygrpc.Operations(
                     (cygrpc.operation_receive_initial_metadata(
                         _EMPTY_FLAGS), )), event_handler)
             operations = (
                 cygrpc.operation_send_initial_metadata(
                     _common.cygrpc_metadata(metadata), _EMPTY_FLAGS),
                 cygrpc.operation_send_message(serialized_request,
                                               _EMPTY_FLAGS),
                 cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
                 cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
             )
             call_error = call.start_client_batch(
                 cygrpc.Operations(operations), event_handler)
             if call_error != cygrpc.CallError.ok:
                 _call_error_set_RPCstate(state, call_error, metadata)
                 return _Rendezvous(state, None, None, deadline)
             drive_call()
         return _Rendezvous(state, call, self._response_deserializer,
                            deadline)
コード例 #10
0
def _status(rpc_event, state, serialized_response):
    with state.condition:
        if state.client is not _CANCELLED:
            trailing_metadata = _common.cygrpc_metadata(
                state.trailing_metadata)
            code = _completion_code(state)
            details = _details(state)
            operations = [
                cygrpc.operation_send_status_from_server(
                    trailing_metadata, code, details, _EMPTY_FLAGS),
            ]
            if state.initial_metadata_allowed:
                operations.append(
                    cygrpc.operation_send_initial_metadata(
                        _common.EMPTY_METADATA, _EMPTY_FLAGS))
            if serialized_response is not None:
                operations.append(
                    cygrpc.operation_send_message(serialized_response,
                                                  _EMPTY_FLAGS))
            rpc_event.operation_call.start_server_batch(
                cygrpc.Operations(operations),
                _send_status_from_server(state,
                                         _SEND_STATUS_FROM_SERVER_TOKEN))
            state.statused = True
            state.due.add(_SEND_STATUS_FROM_SERVER_TOKEN)
コード例 #11
0
ファイル: _channel.py プロジェクト: zzzhr1990/grpc
 def consume_request_iterator():
     for request in request_iterator:
         serialized_request = _common.serialize(request, request_serializer)
         with state.condition:
             if state.code is None and not state.cancelled:
                 if serialized_request is None:
                     call.cancel()
                     details = 'Exception serializing request!'
                     _abort(state, grpc.StatusCode.INTERNAL, details)
                     return
                 else:
                     operations = (cygrpc.operation_send_message(
                         serialized_request, _EMPTY_FLAGS), )
                     call.start_client_batch(cygrpc.Operations(operations),
                                             event_handler)
                     state.due.add(cygrpc.OperationType.send_message)
                     while True:
                         state.condition.wait()
                         if state.code is None:
                             if cygrpc.OperationType.send_message not in state.due:
                                 break
                         else:
                             return
             else:
                 return
     with state.condition:
         if state.code is None:
             operations = (
                 cygrpc.operation_send_close_from_client(_EMPTY_FLAGS), )
             call.start_client_batch(cygrpc.Operations(operations),
                                     event_handler)
             state.due.add(cygrpc.OperationType.send_close_from_client)
コード例 #12
0
    def testCancelManyCalls(self):
        server_thread_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)

        server_completion_queue = cygrpc.CompletionQueue()
        server = cygrpc.Server(cygrpc.ChannelArgs([]))
        server.register_completion_queue(server_completion_queue)
        port = server.add_http2_port(b"[::]:0")
        server.start()
        channel = cygrpc.Channel("localhost:{}".format(port).encode(), cygrpc.ChannelArgs([]))

        state = _State()

        server_thread_args = (state, server, server_completion_queue, server_thread_pool)
        server_thread = threading.Thread(target=_serve, args=server_thread_args)
        server_thread.start()

        client_condition = threading.Condition()
        client_due = set()
        client_completion_queue = cygrpc.CompletionQueue()
        client_driver = _QueueDriver(client_condition, client_completion_queue, client_due)
        client_driver.start()

        with client_condition:
            client_calls = []
            for index in range(test_constants.RPC_CONCURRENCY):
                client_call = channel.create_call(
                    None, _EMPTY_FLAGS, client_completion_queue, b"/twinkies", None, _INFINITE_FUTURE
                )
                operations = (
                    cygrpc.operation_send_initial_metadata(_EMPTY_METADATA, _EMPTY_FLAGS),
                    cygrpc.operation_send_message(b"\x45\x56", _EMPTY_FLAGS),
                    cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
                    cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
                    cygrpc.operation_receive_message(_EMPTY_FLAGS),
                    cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
                )
                tag = "client_complete_call_{0:04d}_tag".format(index)
                client_call.start_client_batch(cygrpc.Operations(operations), tag)
                client_due.add(tag)
                client_calls.append(client_call)

        with state.condition:
            while True:
                if state.parked_handlers < test_constants.THREAD_CONCURRENCY:
                    state.condition.wait()
                elif state.handled_rpcs < test_constants.RPC_CONCURRENCY:
                    state.condition.wait()
                else:
                    state.handlers_released = True
                    state.condition.notify_all()
                    break

        client_driver.events(test_constants.RPC_CONCURRENCY * _SUCCESS_CALL_FRACTION)
        with client_condition:
            for client_call in client_calls:
                client_call.cancel()

        with state.condition:
            server.shutdown(server_completion_queue, _SERVER_SHUTDOWN_TAG)
コード例 #13
0
ファイル: cygrpc_test.py プロジェクト: hmings888/grpc
 def testOperationsIteration(self):
     operations = cygrpc.Operations([cygrpc.operation_send_message('asdf')])
     iterator = iter(operations)
     operation = next(iterator)
     self.assertIsInstance(operation, cygrpc.Operation)
     # `Operation`s are write-only structures; can't directly debug anything out
     # of them. Just check that we stop iterating.
     with self.assertRaises(StopIteration):
         next(iterator)
コード例 #14
0
 def testOperationsIteration(self):
     operations = cygrpc.Operations([cygrpc.operation_send_message(b"asdf", _EMPTY_FLAGS)])
     iterator = iter(operations)
     operation = next(iterator)
     self.assertIsInstance(operation, cygrpc.Operation)
     # `Operation`s are write-only structures; can't directly debug anything out
     # of them. Just check that we stop iterating.
     with self.assertRaises(StopIteration):
         next(iterator)
コード例 #15
0
ファイル: _channel.py プロジェクト: deepaklukose/grpc
 def _prepare(self, request, timeout, metadata):
     deadline, deadline_timespec, serialized_request, rendezvous = (
         _start_unary_request(request, timeout, self._request_serializer))
     if serialized_request is None:
         return None, None, None, None, rendezvous
     else:
         state = _RPCState(_UNARY_UNARY_INITIAL_DUE, None, None, None, None)
         operations = (
             cygrpc.operation_send_initial_metadata(metadata, _EMPTY_FLAGS),
             cygrpc.operation_send_message(serialized_request, _EMPTY_FLAGS),
             cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
             cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
             cygrpc.operation_receive_message(_EMPTY_FLAGS),
             cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),)
         return state, operations, deadline, deadline_timespec, None
コード例 #16
0
 def _prepare(self, request, timeout, metadata):
     deadline, deadline_timespec, serialized_request, rendezvous = (
         _start_unary_request(request, timeout, self._request_serializer))
     if serialized_request is None:
         return None, None, None, None, rendezvous
     else:
         state = _RPCState(_UNARY_UNARY_INITIAL_DUE, None, None, None, None)
         operations = (
             cygrpc.operation_send_initial_metadata(
                 _common.to_cygrpc_metadata(metadata), _EMPTY_FLAGS),
             cygrpc.operation_send_message(serialized_request, _EMPTY_FLAGS),
             cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
             cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
             cygrpc.operation_receive_message(_EMPTY_FLAGS),
             cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),)
         return state, operations, deadline, deadline_timespec, None
コード例 #17
0
ファイル: _channel.py プロジェクト: aaronjheng/grpc
 def consume_request_iterator():
     while True:
         try:
             request = next(request_iterator)
         except StopIteration:
             break
         except Exception:  # pylint: disable=broad-except
             logging.exception("Exception iterating requests!")
             call.cancel()
             _abort(state, grpc.StatusCode.UNKNOWN,
                    "Exception iterating requests!")
             return
         serialized_request = _common.serialize(request, request_serializer)
         with state.condition:
             if state.code is None and not state.cancelled:
                 if serialized_request is None:
                     call.cancel()
                     details = 'Exception serializing request!'
                     _abort(state, grpc.StatusCode.INTERNAL, details)
                     return
                 else:
                     operations = (cygrpc.operation_send_message(
                         serialized_request, _EMPTY_FLAGS),)
                     call.start_client_batch(
                         cygrpc.Operations(operations), event_handler)
                     state.due.add(cygrpc.OperationType.send_message)
                     while True:
                         state.condition.wait()
                         if state.code is None:
                             if cygrpc.OperationType.send_message not in state.due:
                                 break
                         else:
                             return
             else:
                 return
     with state.condition:
         if state.code is None:
             operations = (
                 cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),)
             call.start_client_batch(
                 cygrpc.Operations(operations), event_handler)
             state.due.add(cygrpc.OperationType.send_close_from_client)
コード例 #18
0
 def consume_request_iterator():
     while True:
         try:
             request = next(request_iterator)
         except StopIteration:
             break
         except Exception:  # pylint: disable=broad-except
             logging.exception("Exception iterating requests!")
             call.cancel()
             _abort(state, grpc.StatusCode.UNKNOWN,
                    "Exception iterating requests!")
             return
         serialized_request = _common.serialize(request, request_serializer)
         with state.condition:
             if state.code is None and not state.cancelled:
                 if serialized_request is None:
                     call.cancel()
                     details = 'Exception serializing request!'
                     _abort(state, grpc.StatusCode.INTERNAL, details)
                     return
                 else:
                     operations = (cygrpc.operation_send_message(
                         serialized_request, _EMPTY_FLAGS),)
                     call.start_client_batch(
                         cygrpc.Operations(operations), event_handler)
                     state.due.add(cygrpc.OperationType.send_message)
                     while True:
                         state.condition.wait()
                         if state.code is None:
                             if cygrpc.OperationType.send_message not in state.due:
                                 break
                         else:
                             return
             else:
                 return
     with state.condition:
         if state.code is None:
             operations = (
                 cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),)
             call.start_client_batch(
                 cygrpc.Operations(operations), event_handler)
             state.due.add(cygrpc.OperationType.send_close_from_client)
コード例 #19
0
ファイル: _server.py プロジェクト: deepaklukose/grpc
def _status(rpc_event, state, serialized_response):
    with state.condition:
        if state.client is not _CANCELLED:
            code = _completion_code(state)
            details = _details(state)
            operations = [
                cygrpc.operation_send_status_from_server(
                    state.trailing_metadata, code, details, _EMPTY_FLAGS),
            ]
            if state.initial_metadata_allowed:
                operations.append(
                    cygrpc.operation_send_initial_metadata((), _EMPTY_FLAGS))
            if serialized_response is not None:
                operations.append(
                    cygrpc.operation_send_message(serialized_response,
                                                  _EMPTY_FLAGS))
            rpc_event.operation_call.start_server_batch(
                operations,
                _send_status_from_server(state, _SEND_STATUS_FROM_SERVER_TOKEN))
            state.statused = True
            state.due.add(_SEND_STATUS_FROM_SERVER_TOKEN)
コード例 #20
0
 def consume_request_iterator():
     while True:
         try:
             request = next(request_iterator)
         except StopIteration:
             LOGGER.debug(
                 'consume_request_iterator() encountered StopIteration')
             break
         except Exception:
             LOGGER.exception('Exception iterating requests!')
             call.cancel()
             grpc._channel._abort(state, grpc.StatusCode.UNKNOWN,
                                  'Exception iterating requests!')
             return
         serialized_request = grpc._common.serialize(
             request, request_serializer)
         with state.condition:
             if state.code is None and not state.cancelled:
                 if serialized_request is None:
                     call.cancel()
                     details = 'Exception serializing request!'
                     grpc._channel._abort(state, grpc.StatusCode.INTERNAL,
                                          details)
                     return
                 else:
                     operations = (cygrpc.operation_send_message(
                         serialized_request,
                         grpc._channel._EMPTY_FLAGS,
                     ), )
                     call.start_client_batch(
                         make_ops(operations),
                         event_handler,
                     )
                     state.due.add(cygrpc.OperationType.send_message)
                     while True:
                         state.condition.wait()
                         if state.code is None:
                             if cygrpc.OperationType.send_message not in state.due:
                                 LOGGER.debug(
                                     'consume_request_iterator() sent:\n%r',
                                     request)
                                 break
                         else:
                             LOGGER.debug(
                                 'consume_request_iterator() exiting in '
                                 'error (%r) after waiting for '
                                 'send_message()', state.code)
                             return
             else:
                 LOGGER.debug(
                     'consume_request_iterator() exiting in error before '
                     'sending newly consumed request:\n%r', request)
                 return
     with state.condition:
         if state.code is None:
             operations = (cygrpc.operation_send_close_from_client(
                 grpc._channel._EMPTY_FLAGS, ), )
             call.start_client_batch(make_ops(operations), event_handler)
             state.due.add(cygrpc.OperationType.send_close_from_client)
         else:
             LOGGER.debug(
                 'consume_request_iterator() exiting in error (%r) after a '
                 'request iterator is exhausted', state.code)
コード例 #21
0
 def testOperationFlags(self):
     operation = cygrpc.operation_send_message(b'asdf',
                                               cygrpc.WriteFlag.no_compress)
     self.assertEqual(cygrpc.WriteFlag.no_compress, operation.flags)
コード例 #22
0
    def test6522(self):
        DEADLINE = time.time() + 5
        DEADLINE_TOLERANCE = 0.25
        METHOD = b'twinkies'

        cygrpc_deadline = cygrpc.Timespec(DEADLINE)
        empty_metadata = cygrpc.Metadata([])

        server_request_tag = object()
        self.server.request_call(self.server_completion_queue,
                                 self.server_completion_queue,
                                 server_request_tag)
        client_call = self.client_channel.create_call(
            None, 0, self.client_completion_queue, METHOD, self.host_argument,
            cygrpc_deadline)

        # Prologue
        def perform_client_operations(operations, description):
            return self._perform_operations(operations, client_call,
                                            self.client_completion_queue,
                                            cygrpc_deadline, description)

        client_event_future = perform_client_operations([
            cygrpc.operation_send_initial_metadata(empty_metadata,
                                                   _EMPTY_FLAGS),
            cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
        ], "Client prologue")

        request_event = self.server_completion_queue.poll(cygrpc_deadline)
        server_call = request_event.operation_call

        def perform_server_operations(operations, description):
            return self._perform_operations(operations, server_call,
                                            self.server_completion_queue,
                                            cygrpc_deadline, description)

        server_event_future = perform_server_operations([
            cygrpc.operation_send_initial_metadata(empty_metadata,
                                                   _EMPTY_FLAGS),
        ], "Server prologue")

        client_event_future.result()  # force completion
        server_event_future.result()

        # Messaging
        for _ in range(10):
            client_event_future = perform_client_operations([
                cygrpc.operation_send_message(b'', _EMPTY_FLAGS),
                cygrpc.operation_receive_message(_EMPTY_FLAGS),
            ], "Client message")
            server_event_future = perform_server_operations([
                cygrpc.operation_send_message(b'', _EMPTY_FLAGS),
                cygrpc.operation_receive_message(_EMPTY_FLAGS),
            ], "Server receive")

            client_event_future.result()  # force completion
            server_event_future.result()

        # Epilogue
        client_event_future = perform_client_operations([
            cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
            cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS)
        ], "Client epilogue")

        server_event_future = perform_server_operations([
            cygrpc.operation_receive_close_on_server(_EMPTY_FLAGS),
            cygrpc.operation_send_status_from_server(
                empty_metadata, cygrpc.StatusCode.ok, b'', _EMPTY_FLAGS)
        ], "Server epilogue")

        client_event_future.result()  # force completion
        server_event_future.result()
コード例 #23
0
    def testEcho(self):
        DEADLINE = time.time() + 5
        DEADLINE_TOLERANCE = 0.25
        CLIENT_METADATA_ASCII_KEY = b'key'
        CLIENT_METADATA_ASCII_VALUE = b'val'
        CLIENT_METADATA_BIN_KEY = b'key-bin'
        CLIENT_METADATA_BIN_VALUE = b'\0' * 1000
        SERVER_INITIAL_METADATA_KEY = b'init_me_me_me'
        SERVER_INITIAL_METADATA_VALUE = b'whodawha?'
        SERVER_TRAILING_METADATA_KEY = b'california_is_in_a_drought'
        SERVER_TRAILING_METADATA_VALUE = b'zomg it is'
        SERVER_STATUS_CODE = cygrpc.StatusCode.ok
        SERVER_STATUS_DETAILS = b'our work is never over'
        REQUEST = b'in death a member of project mayhem has a name'
        RESPONSE = b'his name is robert paulson'
        METHOD = b'twinkies'

        cygrpc_deadline = cygrpc.Timespec(DEADLINE)

        server_request_tag = object()
        request_call_result = self.server.request_call(
            self.server_completion_queue, self.server_completion_queue,
            server_request_tag)

        self.assertEqual(cygrpc.CallError.ok, request_call_result)

        client_call_tag = object()
        client_call = self.client_channel.create_call(
            None, 0, self.client_completion_queue, METHOD, self.host_argument,
            cygrpc_deadline)
        client_initial_metadata = cygrpc.Metadata([
            cygrpc.Metadatum(CLIENT_METADATA_ASCII_KEY,
                             CLIENT_METADATA_ASCII_VALUE),
            cygrpc.Metadatum(CLIENT_METADATA_BIN_KEY, CLIENT_METADATA_BIN_VALUE)
        ])
        client_start_batch_result = client_call.start_client_batch([
            cygrpc.operation_send_initial_metadata(client_initial_metadata,
                                                   _EMPTY_FLAGS),
            cygrpc.operation_send_message(REQUEST, _EMPTY_FLAGS),
            cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
            cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
            cygrpc.operation_receive_message(_EMPTY_FLAGS),
            cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS)
        ], client_call_tag)
        self.assertEqual(cygrpc.CallError.ok, client_start_batch_result)
        client_event_future = test_utilities.CompletionQueuePollFuture(
            self.client_completion_queue, cygrpc_deadline)

        request_event = self.server_completion_queue.poll(cygrpc_deadline)
        self.assertEqual(cygrpc.CompletionType.operation_complete,
                         request_event.type)
        self.assertIsInstance(request_event.operation_call, cygrpc.Call)
        self.assertIs(server_request_tag, request_event.tag)
        self.assertEqual(0, len(request_event.batch_operations))
        self.assertTrue(
            test_common.metadata_transmitted(client_initial_metadata,
                                             request_event.request_metadata))
        self.assertEqual(METHOD, request_event.request_call_details.method)
        self.assertEqual(self.expected_host,
                         request_event.request_call_details.host)
        self.assertLess(
            abs(DEADLINE - float(request_event.request_call_details.deadline)),
            DEADLINE_TOLERANCE)

        server_call_tag = object()
        server_call = request_event.operation_call
        server_initial_metadata = cygrpc.Metadata([
            cygrpc.Metadatum(SERVER_INITIAL_METADATA_KEY,
                             SERVER_INITIAL_METADATA_VALUE)
        ])
        server_trailing_metadata = cygrpc.Metadata([
            cygrpc.Metadatum(SERVER_TRAILING_METADATA_KEY,
                             SERVER_TRAILING_METADATA_VALUE)
        ])
        server_start_batch_result = server_call.start_server_batch([
            cygrpc.operation_send_initial_metadata(
                server_initial_metadata,
                _EMPTY_FLAGS), cygrpc.operation_receive_message(_EMPTY_FLAGS),
            cygrpc.operation_send_message(RESPONSE, _EMPTY_FLAGS),
            cygrpc.operation_receive_close_on_server(_EMPTY_FLAGS),
            cygrpc.operation_send_status_from_server(
                server_trailing_metadata, SERVER_STATUS_CODE,
                SERVER_STATUS_DETAILS, _EMPTY_FLAGS)
        ], server_call_tag)
        self.assertEqual(cygrpc.CallError.ok, server_start_batch_result)

        server_event = self.server_completion_queue.poll(cygrpc_deadline)
        client_event = client_event_future.result()

        self.assertEqual(6, len(client_event.batch_operations))
        found_client_op_types = set()
        for client_result in client_event.batch_operations:
            # we expect each op type to be unique
            self.assertNotIn(client_result.type, found_client_op_types)
            found_client_op_types.add(client_result.type)
            if client_result.type == cygrpc.OperationType.receive_initial_metadata:
                self.assertTrue(
                    test_common.metadata_transmitted(
                        server_initial_metadata,
                        client_result.received_metadata))
            elif client_result.type == cygrpc.OperationType.receive_message:
                self.assertEqual(RESPONSE,
                                 client_result.received_message.bytes())
            elif client_result.type == cygrpc.OperationType.receive_status_on_client:
                self.assertTrue(
                    test_common.metadata_transmitted(
                        server_trailing_metadata,
                        client_result.received_metadata))
                self.assertEqual(SERVER_STATUS_DETAILS,
                                 client_result.received_status_details)
                self.assertEqual(SERVER_STATUS_CODE,
                                 client_result.received_status_code)
        self.assertEqual(
            set([
                cygrpc.OperationType.send_initial_metadata,
                cygrpc.OperationType.send_message,
                cygrpc.OperationType.send_close_from_client,
                cygrpc.OperationType.receive_initial_metadata,
                cygrpc.OperationType.receive_message,
                cygrpc.OperationType.receive_status_on_client
            ]), found_client_op_types)

        self.assertEqual(5, len(server_event.batch_operations))
        found_server_op_types = set()
        for server_result in server_event.batch_operations:
            self.assertNotIn(client_result.type, found_server_op_types)
            found_server_op_types.add(server_result.type)
            if server_result.type == cygrpc.OperationType.receive_message:
                self.assertEqual(REQUEST,
                                 server_result.received_message.bytes())
            elif server_result.type == cygrpc.OperationType.receive_close_on_server:
                self.assertFalse(server_result.received_cancelled)
        self.assertEqual(
            set([
                cygrpc.OperationType.send_initial_metadata,
                cygrpc.OperationType.receive_message,
                cygrpc.OperationType.send_message,
                cygrpc.OperationType.receive_close_on_server,
                cygrpc.OperationType.send_status_from_server
            ]), found_server_op_types)

        del client_call
        del server_call
コード例 #24
0
    def testReadSomeButNotAllResponses(self):
        server_completion_queue = cygrpc.CompletionQueue()
        server = cygrpc.Server()
        server.register_completion_queue(server_completion_queue)
        port = server.add_http2_port(b"[::]:0")
        server.start()
        channel = cygrpc.Channel("localhost:{}".format(port).encode())

        server_shutdown_tag = "server_shutdown_tag"
        server_driver = _ServerDriver(server_completion_queue, server_shutdown_tag)
        server_driver.start()

        client_condition = threading.Condition()
        client_due = set()
        client_completion_queue = cygrpc.CompletionQueue()
        client_driver = _QueueDriver(client_condition, client_completion_queue, client_due)
        client_driver.start()

        server_call_condition = threading.Condition()
        server_send_initial_metadata_tag = "server_send_initial_metadata_tag"
        server_send_first_message_tag = "server_send_first_message_tag"
        server_send_second_message_tag = "server_send_second_message_tag"
        server_complete_rpc_tag = "server_complete_rpc_tag"
        server_call_due = set(
            (
                server_send_initial_metadata_tag,
                server_send_first_message_tag,
                server_send_second_message_tag,
                server_complete_rpc_tag,
            )
        )
        server_call_completion_queue = cygrpc.CompletionQueue()
        server_call_driver = _QueueDriver(server_call_condition, server_call_completion_queue, server_call_due)
        server_call_driver.start()

        server_rpc_tag = "server_rpc_tag"
        request_call_result = server.request_call(server_call_completion_queue, server_completion_queue, server_rpc_tag)

        client_call = channel.create_call(
            None, _EMPTY_FLAGS, client_completion_queue, b"/twinkies", None, _INFINITE_FUTURE
        )
        client_receive_initial_metadata_tag = "client_receive_initial_metadata_tag"
        client_complete_rpc_tag = "client_complete_rpc_tag"
        with client_condition:
            client_receive_initial_metadata_start_batch_result = client_call.start_batch(
                cygrpc.Operations([cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS)]),
                client_receive_initial_metadata_tag,
            )
            client_due.add(client_receive_initial_metadata_tag)
            client_complete_rpc_start_batch_result = client_call.start_batch(
                cygrpc.Operations(
                    [
                        cygrpc.operation_send_initial_metadata(_EMPTY_METADATA, _EMPTY_FLAGS),
                        cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
                        cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
                    ]
                ),
                client_complete_rpc_tag,
            )
            client_due.add(client_complete_rpc_tag)

        server_rpc_event = server_driver.first_event()

        with server_call_condition:
            server_send_initial_metadata_start_batch_result = server_rpc_event.operation_call.start_batch(
                cygrpc.Operations([cygrpc.operation_send_initial_metadata(_EMPTY_METADATA, _EMPTY_FLAGS)]),
                server_send_initial_metadata_tag,
            )
            server_send_first_message_start_batch_result = server_rpc_event.operation_call.start_batch(
                cygrpc.Operations([cygrpc.operation_send_message(b"\x07", _EMPTY_FLAGS)]), server_send_first_message_tag
            )
        server_send_initial_metadata_event = server_call_driver.event_with_tag(server_send_initial_metadata_tag)
        server_send_first_message_event = server_call_driver.event_with_tag(server_send_first_message_tag)
        with server_call_condition:
            server_send_second_message_start_batch_result = server_rpc_event.operation_call.start_batch(
                cygrpc.Operations([cygrpc.operation_send_message(b"\x07", _EMPTY_FLAGS)]),
                server_send_second_message_tag,
            )
            server_complete_rpc_start_batch_result = server_rpc_event.operation_call.start_batch(
                cygrpc.Operations(
                    [
                        cygrpc.operation_receive_close_on_server(_EMPTY_FLAGS),
                        cygrpc.operation_send_status_from_server(
                            cygrpc.Metadata(()), cygrpc.StatusCode.ok, b"test details", _EMPTY_FLAGS
                        ),
                    ]
                ),
                server_complete_rpc_tag,
            )
        server_send_second_message_event = server_call_driver.event_with_tag(server_send_second_message_tag)
        server_complete_rpc_event = server_call_driver.event_with_tag(server_complete_rpc_tag)
        server_call_driver.events()

        with client_condition:
            client_receive_first_message_tag = "client_receive_first_message_tag"
            client_receive_first_message_start_batch_result = client_call.start_batch(
                cygrpc.Operations([cygrpc.operation_receive_message(_EMPTY_FLAGS)]), client_receive_first_message_tag
            )
            client_due.add(client_receive_first_message_tag)
        client_receive_first_message_event = client_driver.event_with_tag(client_receive_first_message_tag)

        client_call_cancel_result = client_call.cancel()
        client_driver.events()

        server.shutdown(server_completion_queue, server_shutdown_tag)
        server.cancel_all_calls()
        server_driver.events()

        self.assertEqual(cygrpc.CallError.ok, request_call_result)
        self.assertEqual(cygrpc.CallError.ok, server_send_initial_metadata_start_batch_result)
        self.assertEqual(cygrpc.CallError.ok, client_receive_initial_metadata_start_batch_result)
        self.assertEqual(cygrpc.CallError.ok, client_complete_rpc_start_batch_result)
        self.assertEqual(cygrpc.CallError.ok, client_call_cancel_result)
        self.assertIs(server_rpc_tag, server_rpc_event.tag)
        self.assertEqual(cygrpc.CompletionType.operation_complete, server_rpc_event.type)
        self.assertIsInstance(server_rpc_event.operation_call, cygrpc.Call)
        self.assertEqual(0, len(server_rpc_event.batch_operations))
コード例 #25
0
    def testReadSomeButNotAllResponses(self):
        server_completion_queue = cygrpc.CompletionQueue()
        server = cygrpc.Server(cygrpc.ChannelArgs([]))
        server.register_completion_queue(server_completion_queue)
        port = server.add_http2_port(b'[::]:0')
        server.start()
        channel = cygrpc.Channel('localhost:{}'.format(port).encode(),
                                 cygrpc.ChannelArgs([]))

        server_shutdown_tag = 'server_shutdown_tag'
        server_driver = _ServerDriver(server_completion_queue,
                                      server_shutdown_tag)
        server_driver.start()

        client_condition = threading.Condition()
        client_due = set()
        client_completion_queue = cygrpc.CompletionQueue()
        client_driver = _QueueDriver(client_condition, client_completion_queue,
                                     client_due)
        client_driver.start()

        server_call_condition = threading.Condition()
        server_send_initial_metadata_tag = 'server_send_initial_metadata_tag'
        server_send_first_message_tag = 'server_send_first_message_tag'
        server_send_second_message_tag = 'server_send_second_message_tag'
        server_complete_rpc_tag = 'server_complete_rpc_tag'
        server_call_due = set((
            server_send_initial_metadata_tag,
            server_send_first_message_tag,
            server_send_second_message_tag,
            server_complete_rpc_tag,))
        server_call_completion_queue = cygrpc.CompletionQueue()
        server_call_driver = _QueueDriver(server_call_condition,
                                          server_call_completion_queue,
                                          server_call_due)
        server_call_driver.start()

        server_rpc_tag = 'server_rpc_tag'
        request_call_result = server.request_call(server_call_completion_queue,
                                                  server_completion_queue,
                                                  server_rpc_tag)

        client_call = channel.create_call(None, _EMPTY_FLAGS,
                                          client_completion_queue, b'/twinkies',
                                          None, _INFINITE_FUTURE)
        client_receive_initial_metadata_tag = 'client_receive_initial_metadata_tag'
        client_complete_rpc_tag = 'client_complete_rpc_tag'
        with client_condition:
            client_receive_initial_metadata_start_batch_result = (
                client_call.start_client_batch(
                    cygrpc.Operations([
                        cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
                    ]), client_receive_initial_metadata_tag))
            client_due.add(client_receive_initial_metadata_tag)
            client_complete_rpc_start_batch_result = (
                client_call.start_client_batch(
                    cygrpc.Operations([
                        cygrpc.operation_send_initial_metadata(_EMPTY_METADATA,
                                                               _EMPTY_FLAGS),
                        cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
                        cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
                    ]), client_complete_rpc_tag))
            client_due.add(client_complete_rpc_tag)

        server_rpc_event = server_driver.first_event()

        with server_call_condition:
            server_send_initial_metadata_start_batch_result = (
                server_rpc_event.operation_call.start_server_batch([
                    cygrpc.operation_send_initial_metadata(_EMPTY_METADATA,
                                                           _EMPTY_FLAGS),
                ], server_send_initial_metadata_tag))
            server_send_first_message_start_batch_result = (
                server_rpc_event.operation_call.start_server_batch([
                    cygrpc.operation_send_message(b'\x07', _EMPTY_FLAGS),
                ], server_send_first_message_tag))
        server_send_initial_metadata_event = server_call_driver.event_with_tag(
            server_send_initial_metadata_tag)
        server_send_first_message_event = server_call_driver.event_with_tag(
            server_send_first_message_tag)
        with server_call_condition:
            server_send_second_message_start_batch_result = (
                server_rpc_event.operation_call.start_server_batch([
                    cygrpc.operation_send_message(b'\x07', _EMPTY_FLAGS),
                ], server_send_second_message_tag))
            server_complete_rpc_start_batch_result = (
                server_rpc_event.operation_call.start_server_batch([
                    cygrpc.operation_receive_close_on_server(_EMPTY_FLAGS),
                    cygrpc.operation_send_status_from_server(
                        cygrpc.Metadata(()), cygrpc.StatusCode.ok,
                        b'test details', _EMPTY_FLAGS),
                ], server_complete_rpc_tag))
        server_send_second_message_event = server_call_driver.event_with_tag(
            server_send_second_message_tag)
        server_complete_rpc_event = server_call_driver.event_with_tag(
            server_complete_rpc_tag)
        server_call_driver.events()

        with client_condition:
            client_receive_first_message_tag = 'client_receive_first_message_tag'
            client_receive_first_message_start_batch_result = (
                client_call.start_client_batch(
                    cygrpc.Operations([
                        cygrpc.operation_receive_message(_EMPTY_FLAGS),
                    ]), client_receive_first_message_tag))
            client_due.add(client_receive_first_message_tag)
        client_receive_first_message_event = client_driver.event_with_tag(
            client_receive_first_message_tag)

        client_call_cancel_result = client_call.cancel()
        client_driver.events()

        server.shutdown(server_completion_queue, server_shutdown_tag)
        server.cancel_all_calls()
        server_driver.events()

        self.assertEqual(cygrpc.CallError.ok, request_call_result)
        self.assertEqual(cygrpc.CallError.ok,
                         server_send_initial_metadata_start_batch_result)
        self.assertEqual(cygrpc.CallError.ok,
                         client_receive_initial_metadata_start_batch_result)
        self.assertEqual(cygrpc.CallError.ok,
                         client_complete_rpc_start_batch_result)
        self.assertEqual(cygrpc.CallError.ok, client_call_cancel_result)
        self.assertIs(server_rpc_tag, server_rpc_event.tag)
        self.assertEqual(cygrpc.CompletionType.operation_complete,
                         server_rpc_event.type)
        self.assertIsInstance(server_rpc_event.operation_call, cygrpc.Call)
        self.assertEqual(0, len(server_rpc_event.batch_operations))
コード例 #26
0
    def testCancelManyCalls(self):
        server_thread_pool = logging_pool.pool(
            test_constants.THREAD_CONCURRENCY)

        server_completion_queue = cygrpc.CompletionQueue()
        server = cygrpc.Server(
            cygrpc.ChannelArgs([cygrpc.ChannelArg(b'grpc.so_reuseport', 0)]))
        server.register_completion_queue(server_completion_queue)
        port = server.add_http2_port(b'[::]:0')
        server.start()
        channel = cygrpc.Channel('localhost:{}'.format(port).encode(),
                                 cygrpc.ChannelArgs([]))

        state = _State()

        server_thread_args = (
            state,
            server,
            server_completion_queue,
            server_thread_pool,
        )
        server_thread = threading.Thread(target=_serve,
                                         args=server_thread_args)
        server_thread.start()

        client_condition = threading.Condition()
        client_due = set()
        client_completion_queue = cygrpc.CompletionQueue()
        client_driver = _QueueDriver(client_condition, client_completion_queue,
                                     client_due)
        client_driver.start()

        with client_condition:
            client_calls = []
            for index in range(test_constants.RPC_CONCURRENCY):
                client_call = channel.create_call(None, _EMPTY_FLAGS,
                                                  client_completion_queue,
                                                  b'/twinkies', None,
                                                  _INFINITE_FUTURE)
                operations = (
                    cygrpc.operation_send_initial_metadata(
                        _EMPTY_METADATA, _EMPTY_FLAGS),
                    cygrpc.operation_send_message(b'\x45\x56', _EMPTY_FLAGS),
                    cygrpc.operation_send_close_from_client(_EMPTY_FLAGS),
                    cygrpc.operation_receive_initial_metadata(_EMPTY_FLAGS),
                    cygrpc.operation_receive_message(_EMPTY_FLAGS),
                    cygrpc.operation_receive_status_on_client(_EMPTY_FLAGS),
                )
                tag = 'client_complete_call_{0:04d}_tag'.format(index)
                client_call.start_client_batch(operations, tag)
                client_due.add(tag)
                client_calls.append(client_call)

        with state.condition:
            while True:
                if state.parked_handlers < test_constants.THREAD_CONCURRENCY:
                    state.condition.wait()
                elif state.handled_rpcs < test_constants.RPC_CONCURRENCY:
                    state.condition.wait()
                else:
                    state.handlers_released = True
                    state.condition.notify_all()
                    break

        client_driver.events(test_constants.RPC_CONCURRENCY *
                             _SUCCESS_CALL_FRACTION)
        with client_condition:
            for client_call in client_calls:
                client_call.cancel()

        with state.condition:
            server.shutdown(server_completion_queue, _SERVER_SHUTDOWN_TAG)