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.ReceiveCloseOnServerOperation(_EMPTY_FLAGS), ),
                _RECEIVE_CLOSE_ON_SERVER_TAG)
            self._call.start_server_batch(
                (cygrpc.ReceiveMessageOperation(_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.SendInitialMetadataOperation(
                        _EMPTY_METADATA, _EMPTY_FLAGS),
                    cygrpc.SendMessageOperation(b'\x79\x57', _EMPTY_FLAGS),
                    cygrpc.SendStatusFromServerOperation(
                        _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()
Exemple #2
0
 def unary_request():
     with state.condition:
         if state.client is _CANCELLED or state.statused:
             return None
         else:
             rpc_event.call.start_server_batch(
                 (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS), ),
                 _receive_message(state, rpc_event.call,
                                  request_deserializer))
             state.due.add(_RECEIVE_MESSAGE_TOKEN)
             while True:
                 state.condition.wait()
                 if state.request is None:
                     if state.client is _CLOSED:
                         details = '"{}" requires exactly one request message.'.format(
                             rpc_event.call_details.method)
                         _abort(state, rpc_event.call,
                                cygrpc.StatusCode.unimplemented,
                                _common.encode(details))
                         return None
                     elif state.client is _CANCELLED:
                         return None
                 else:
                     request = state.request
                     state.request = None
                     return request
 def future(self,
            request_iterator,
            timeout=None,
            metadata=None,
            credentials=None):
     deadline = _deadline(timeout)
     state = _RPCState(_STREAM_UNARY_INITIAL_DUE, None, None, None, None)
     call, drive_call = self._managed_call(None, 0, self._method, None,
                                           deadline)
     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.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),),
             event_handler)
         operations = (
             cygrpc.SendInitialMetadataOperation(metadata, _EMPTY_FLAGS),
             cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
             cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
         )
         call_error = call.start_client_batch(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()
         _consume_request_iterator(request_iterator, state, call,
                                   self._request_serializer)
     return _Rendezvous(state, call, self._response_deserializer, deadline)
 def _blocking(self, request_iterator, timeout, metadata, credentials):
     deadline = _deadline(timeout)
     state = _RPCState(_STREAM_UNARY_INITIAL_DUE, None, None, None, None)
     completion_queue = cygrpc.CompletionQueue()
     call = self._channel.create_call(None, 0, completion_queue,
                                      self._method, None, deadline)
     if credentials is not None:
         call.set_credentials(credentials._credentials)
     with state.condition:
         call.start_client_batch(
             (cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),), None)
         operations = (
             cygrpc.SendInitialMetadataOperation(metadata, _EMPTY_FLAGS),
             cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
             cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
         )
         call_error = call.start_client_batch(operations, None)
         _check_call_error(call_error, metadata)
         _consume_request_iterator(request_iterator, state, call,
                                   self._request_serializer)
     while True:
         event = completion_queue.poll()
         with state.condition:
             _handle_event(event, state, self._response_deserializer)
             state.condition.notify_all()
             if not state.due:
                 break
     return state, call, deadline
Exemple #5
0
 def _next(self):
     with self._state.condition:
         if self._state.code is None:
             event_handler = _event_handler(self._state,
                                            self._response_deserializer)
             operating = self._call.operate(
                 (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS), ),
                 event_handler)
             if operating:
                 self._state.due.add(cygrpc.OperationType.receive_message)
         elif self._state.code is grpc.StatusCode.OK:
             raise StopIteration()
         else:
             raise self
         while True:
             self._state.condition.wait()
             if self._state.response is not None:
                 response = self._state.response
                 self._state.response = None
                 return response
             elif cygrpc.OperationType.receive_message not in self._state.due:
                 if self._state.code is grpc.StatusCode.OK:
                     raise StopIteration()
                 elif self._state.code is not None:
                     raise self
Exemple #6
0
    def _process_future(self, future):
        # The grpc callback triggered; time to figure out what happened,
        # and set the given future if necessary.
        if self.stream._state.response is not None:
            # event: we received something
            # Put it in the future and move on
            response = self.stream._state.response
            self.stream._state.response = None
            self.loop.call_soon_threadsafe(future.set_result, response)
            return
        elif cygrpc.OperationType.receive_message not in self.stream._state.due:
            if self.stream._state.code is grpc.StatusCode.OK:
                # event: stream closed
                # Tell the future we're done
                self.loop.call_soon_threadsafe(future.set_exception,
                                               StopAsyncIteration())
                return
            elif self.stream._state.code is not None:
                # event: some sort of stream error
                # self.stream is a grpc._Rendezvous object that normally
                # gives us stream results, but they double as grpc.RpcError
                # So we raise it
                self.loop.call_soon_threadsafe(future.set_exception,
                                               self.stream)
                return

        # An event we aren't interested in happened; we should wait for the next event.
        #########
        event_handler = lambda event: self.handle_event(event, future)
        self.stream._call.start_client_batch(
            (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS), ), event_handler)
        self.stream._state.due.add(cygrpc.OperationType.receive_message)
Exemple #7
0
    def _next(self):
        """
        Returns a future for the next value in an iterator
        """
        # ensure there is only one outstanding request at any given time, or segfault happens
        if cygrpc.OperationType.receive_message in self.stream._state.due:
            raise ValueError("Prior future was not resolved")

        future = Future()
        # this method is the same as the first part of _Rendevous._next
        with self.stream._state.condition:
            if self.stream._state.code is None:
                # Give grpc an event handler (self.handle_event) to call
                # whenever something changes with the stream
                # e.g. a batch is received or the channel is closed
                event_handler = lambda event: self.handle_event(event, future)
                self.stream._call.start_client_batch(
                    (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS), ),
                    event_handler)
                self.stream._state.due.add(
                    cygrpc.OperationType.receive_message)
            # if the stream is already finished (OK), just end now
            elif self.stream._state.code is grpc.StatusCode.OK:
                future.set_exception(StopAsyncIteration())
            # if there's already an error in the stream, set the error and return
            else:
                future.set_exception(self.stream)
        return future
Exemple #8
0
def _stream_unary_invocation_operationses(metadata):
    return (
        (
            cygrpc.SendInitialMetadataOperation(metadata, _EMPTY_FLAGS),
            cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
            cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
        ),
        (cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),),
    )
Exemple #9
0
 def _raise_or_start_receive_message(self):
     if self._state.client is _CANCELLED:
         _raise_rpc_error(self._state)
     elif not _is_rpc_state_active(self._state):
         raise StopIteration()
     else:
         self._call.start_server_batch(
             (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS), ),
             _receive_message(self._state, self._call,
                              self._request_deserializer))
         self._state.due.add(_RECEIVE_MESSAGE_TOKEN)
Exemple #10
0
 def _prepare(self, request, timeout, metadata):
     deadline, serialized_request, rendezvous = _start_unary_request(
         request, timeout, self._request_serializer)
     if serialized_request is None:
         return None, None, None, rendezvous
     else:
         state = _RPCState(_UNARY_UNARY_INITIAL_DUE, None, None, None, None)
         operations = (
             cygrpc.SendInitialMetadataOperation(metadata, _EMPTY_FLAGS),
             cygrpc.SendMessageOperation(serialized_request, _EMPTY_FLAGS),
             cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
             cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),
             cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
             cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
         )
         return state, operations, deadline, None
Exemple #11
0
def operate_async_stream_call(it, wrapper):
    future = futures.Future()
    callback = on_event_callback(future, it, wrapper)

    with it._state.condition:
        if it._state.code is None:
            it._state.on_event_handler_callback = callback
            operating = it._call.operate(
                (cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS), ),
                _event_handler(it._state, it._response_deserializer))
            if operating:
                it._state.due.add(cygrpc.OperationType.receive_message)
        elif it._state.code is grpc.StatusCode.OK:
            future.set_exception(StopIteration())
        else:
            future.set_exception(it)
    return future
Exemple #12
0
 def _prepare(self, request, timeout, metadata, wait_for_ready, compression):
     deadline, serialized_request, rendezvous = _start_unary_request(
         request, timeout, self._request_serializer)
     initial_metadata_flags = _InitialMetadataFlags().with_wait_for_ready(
         wait_for_ready)
     augmented_metadata = _compression.augment_metadata(
         metadata, compression)
     if serialized_request is None:
         return None, None, None, rendezvous
     else:
         state = _RPCState(_UNARY_UNARY_INITIAL_DUE, None, None, None, None)
         operations = (
             cygrpc.SendInitialMetadataOperation(augmented_metadata,
                                                 initial_metadata_flags),
             cygrpc.SendMessageOperation(serialized_request, _EMPTY_FLAGS),
             cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
             cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),
             cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
             cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
         )
         return state, operations, deadline, None
    def testReadSomeButNotAllResponses(self):
        server_completion_queue = cygrpc.CompletionQueue()
        server = cygrpc.Server([(
            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(), set(),
                                 None)

        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()

        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_receive_initial_metadata_tag = 'client_receive_initial_metadata_tag'
        client_complete_rpc_tag = 'client_complete_rpc_tag'
        client_call = channel.segregated_call(
            _EMPTY_FLAGS, b'/twinkies', None, None, _EMPTY_METADATA, None, (
                (
                    [
                        cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),
                    ],
                    client_receive_initial_metadata_tag,
                ),
                (
                    [
                        cygrpc.SendInitialMetadataOperation(
                            _EMPTY_METADATA, _EMPTY_FLAGS),
                        cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
                        cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
                    ],
                    client_complete_rpc_tag,
                ),
            ))
        client_receive_initial_metadata_event_future = test_utilities.SimpleFuture(
            client_call.next_event)

        server_rpc_event = server_driver.first_event()

        with server_call_condition:
            server_send_initial_metadata_start_batch_result = (
                server_rpc_event.call.start_server_batch([
                    cygrpc.SendInitialMetadataOperation(
                        _EMPTY_METADATA, _EMPTY_FLAGS),
                ], server_send_initial_metadata_tag))
            server_send_first_message_start_batch_result = (
                server_rpc_event.call.start_server_batch([
                    cygrpc.SendMessageOperation(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.call.start_server_batch([
                    cygrpc.SendMessageOperation(b'\x07', _EMPTY_FLAGS),
                ], server_send_second_message_tag))
            server_complete_rpc_start_batch_result = (
                server_rpc_event.call.start_server_batch([
                    cygrpc.ReceiveCloseOnServerOperation(_EMPTY_FLAGS),
                    cygrpc.SendStatusFromServerOperation(
                        (), 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()

        client_recieve_initial_metadata_event = client_receive_initial_metadata_event_future.result(
        )

        client_receive_first_message_tag = 'client_receive_first_message_tag'
        client_call.operate([
            cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
        ], client_receive_first_message_tag)
        client_receive_first_message_event = client_call.next_event()

        client_call_cancel_result = client_call.cancel(
            cygrpc.StatusCode.cancelled, 'Cancelled during test!')
        client_complete_rpc_event = client_call.next_event()

        channel.close(cygrpc.StatusCode.unknown, 'Channel closed!')
        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.assertIs(server_rpc_tag, server_rpc_event.tag)
        self.assertEqual(cygrpc.CompletionType.operation_complete,
                         server_rpc_event.completion_type)
        self.assertIsInstance(server_rpc_event.call, cygrpc.Call)
Exemple #14
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.SendInitialMetadataOperation(
                        _EMPTY_METADATA, _EMPTY_FLAGS),
                    cygrpc.SendMessageOperation(b'\x45\x56', _EMPTY_FLAGS),
                    cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
                    cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),
                    cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
                    cygrpc.ReceiveStatusOnClientOperation(_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)
Exemple #15
0
    def test_6522(self):
        DEADLINE = time.time() + 5
        DEADLINE_TOLERANCE = 0.25
        METHOD = b'twinkies'

        empty_metadata = ()

        # Prologue
        server_request_tag = object()
        self.server.request_call(self.server_completion_queue,
                                 self.server_completion_queue,
                                 server_request_tag)
        client_call = self.client_channel.segregated_call(
            0, METHOD, self.host_argument, DEADLINE, None, None,
            ([(
                [
                    cygrpc.SendInitialMetadataOperation(
                        empty_metadata, _EMPTY_FLAGS),
                    cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),
                ],
                object(),
            ),
              (
                  [
                      cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
                  ],
                  object(),
              )]))

        client_initial_metadata_event_future = test_utilities.SimpleFuture(
            client_call.next_event)

        request_event = self.server_completion_queue.poll(deadline=DEADLINE)
        server_call = request_event.call

        def perform_server_operations(operations, description):
            return self._perform_queue_operations(operations, server_call,
                                                  self.server_completion_queue,
                                                  DEADLINE, description)

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

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

        # Messaging
        for _ in range(10):
            client_call.operate([
                cygrpc.SendMessageOperation(b'', _EMPTY_FLAGS),
                cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
            ], "Client message")
            client_message_event_future = test_utilities.SimpleFuture(
                client_call.next_event)
            server_event_future = perform_server_operations([
                cygrpc.SendMessageOperation(b'', _EMPTY_FLAGS),
                cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
            ], "Server receive")

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

        # Epilogue
        client_call.operate([
            cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
        ], "Client epilogue")
        # One for ReceiveStatusOnClient, one for SendCloseFromClient.
        client_events_future = test_utilities.SimpleFuture(lambda: {
            client_call.next_event(),
            client_call.next_event(),
        })

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

        client_events_future.result()  # force completion
        server_event_future.result()
Exemple #16
0
    def test_echo(self):
        DEADLINE = time.time() + 5
        DEADLINE_TOLERANCE = 0.25
        CLIENT_METADATA_ASCII_KEY = 'key'
        CLIENT_METADATA_ASCII_VALUE = 'val'
        CLIENT_METADATA_BIN_KEY = 'key-bin'
        CLIENT_METADATA_BIN_VALUE = b'\0' * 1000
        SERVER_INITIAL_METADATA_KEY = 'init_me_me_me'
        SERVER_INITIAL_METADATA_VALUE = 'whodawha?'
        SERVER_TRAILING_METADATA_KEY = 'california_is_in_a_drought'
        SERVER_TRAILING_METADATA_VALUE = 'zomg it is'
        SERVER_STATUS_CODE = cygrpc.StatusCode.ok
        SERVER_STATUS_DETAILS = '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'

        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_initial_metadata = (
            (
                CLIENT_METADATA_ASCII_KEY,
                CLIENT_METADATA_ASCII_VALUE,
            ),
            (
                CLIENT_METADATA_BIN_KEY,
                CLIENT_METADATA_BIN_VALUE,
            ),
        )
        client_call = self.client_channel.integrated_call(
            0, METHOD, self.host_argument, DEADLINE, client_initial_metadata,
            None, [
                (
                    [
                        cygrpc.SendInitialMetadataOperation(
                            client_initial_metadata, _EMPTY_FLAGS),
                        cygrpc.SendMessageOperation(REQUEST, _EMPTY_FLAGS),
                        cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
                        cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),
                        cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
                        cygrpc.ReceiveStatusOnClientOperation(_EMPTY_FLAGS),
                    ],
                    client_call_tag,
                ),
            ])
        client_event_future = test_utilities.SimpleFuture(
            self.client_channel.next_call_event)

        request_event = self.server_completion_queue.poll(deadline=DEADLINE)
        self.assertEqual(cygrpc.CompletionType.operation_complete,
                         request_event.completion_type)
        self.assertIsInstance(request_event.call, cygrpc.Call)
        self.assertIs(server_request_tag, request_event.tag)
        self.assertTrue(
            test_common.metadata_transmitted(
                client_initial_metadata, request_event.invocation_metadata))
        self.assertEqual(METHOD, request_event.call_details.method)
        self.assertEqual(self.expected_host, request_event.call_details.host)
        self.assertLess(abs(DEADLINE - request_event.call_details.deadline),
                        DEADLINE_TOLERANCE)

        server_call_tag = object()
        server_call = request_event.call
        server_initial_metadata = ((
            SERVER_INITIAL_METADATA_KEY,
            SERVER_INITIAL_METADATA_VALUE,
        ), )
        server_trailing_metadata = ((
            SERVER_TRAILING_METADATA_KEY,
            SERVER_TRAILING_METADATA_VALUE,
        ), )
        server_start_batch_result = server_call.start_server_batch([
            cygrpc.SendInitialMetadataOperation(server_initial_metadata,
                                                _EMPTY_FLAGS),
            cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
            cygrpc.SendMessageOperation(RESPONSE, _EMPTY_FLAGS),
            cygrpc.ReceiveCloseOnServerOperation(_EMPTY_FLAGS),
            cygrpc.SendStatusFromServerOperation(
                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(deadline=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.initial_metadata()))
            elif client_result.type() == cygrpc.OperationType.receive_message:
                self.assertEqual(RESPONSE, client_result.message())
            elif client_result.type(
            ) == cygrpc.OperationType.receive_status_on_client:
                self.assertTrue(
                    test_common.metadata_transmitted(
                        server_trailing_metadata,
                        client_result.trailing_metadata()))
                self.assertEqual(SERVER_STATUS_DETAILS,
                                 client_result.details())
                self.assertEqual(SERVER_STATUS_CODE, client_result.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(server_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.message())
            elif server_result.type(
            ) == cygrpc.OperationType.receive_close_on_server:
                self.assertFalse(server_result.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
    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.ReceiveInitialMetadataOperation(_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.SendInitialMetadataOperation(_EMPTY_METADATA,
                                                        _EMPTY_FLAGS),
                    cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
                    cygrpc.ReceiveStatusOnClientOperation(_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.SendInitialMetadataOperation(_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.SendMessageOperation(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.SendMessageOperation(b'\x07', _EMPTY_FLAGS),
                ], server_send_second_message_tag))
            server_complete_rpc_start_batch_result = (
                server_rpc_event.operation_call.start_server_batch([
                    cygrpc.ReceiveCloseOnServerOperation(_EMPTY_FLAGS),
                    cygrpc.SendStatusFromServerOperation(
                        (), 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.ReceiveMessageOperation(_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))
    def testCancelManyCalls(self):
        server_thread_pool = logging_pool.pool(
            test_constants.THREAD_CONCURRENCY)

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

        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()

        with client_condition:
            client_calls = []
            for index in range(test_constants.RPC_CONCURRENCY):
                tag = 'client_complete_call_{0:04d}_tag'.format(index)
                client_call = channel.integrated_call(
                    _EMPTY_FLAGS, b'/twinkies', None, None, _EMPTY_METADATA,
                    None, ((
                        (
                            cygrpc.SendInitialMetadataOperation(
                                _EMPTY_METADATA, _EMPTY_FLAGS),
                            cygrpc.SendMessageOperation(
                                b'\x45\x56', _EMPTY_FLAGS),
                            cygrpc.SendCloseFromClientOperation(_EMPTY_FLAGS),
                            cygrpc.ReceiveInitialMetadataOperation(
                                _EMPTY_FLAGS),
                            cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
                            cygrpc.ReceiveStatusOnClientOperation(
                                _EMPTY_FLAGS),
                        ),
                        tag,
                    ), ))
                client_due.add(tag)
                client_calls.append(client_call)

        client_events_future = test_utilities.SimpleFuture(lambda: tuple(
            channel.next_call_event() for _ in range(_SUCCESSFUL_CALLS)))

        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_events_future.result()
        with client_condition:
            for client_call in client_calls:
                client_call.cancel(cygrpc.StatusCode.cancelled, 'Cancelled!')
        for _ in range(_UNSUCCESSFUL_CALLS):
            channel.next_call_event()

        channel.close(cygrpc.StatusCode.unknown, 'Cancelled on channel close!')
        with state.condition:
            server.shutdown(server_completion_queue, _SERVER_SHUTDOWN_TAG)
Exemple #19
0
    def test6522(self):
        DEADLINE = time.time() + 5
        DEADLINE_TOLERANCE = 0.25
        METHOD = b'twinkies'

        empty_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,
            DEADLINE)

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

        client_event_future = perform_client_operations([
            cygrpc.SendInitialMetadataOperation(empty_metadata, _EMPTY_FLAGS),
            cygrpc.ReceiveInitialMetadataOperation(_EMPTY_FLAGS),
        ], "Client prologue")

        request_event = self.server_completion_queue.poll(deadline=DEADLINE)
        server_call = request_event.call

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

        server_event_future = perform_server_operations([
            cygrpc.SendInitialMetadataOperation(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.SendMessageOperation(b'', _EMPTY_FLAGS),
                cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
            ], "Client message")
            server_event_future = perform_server_operations([
                cygrpc.SendMessageOperation(b'', _EMPTY_FLAGS),
                cygrpc.ReceiveMessageOperation(_EMPTY_FLAGS),
            ], "Server receive")

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

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

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

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