Exemplo n.º 1
0
def _connectivity_loop(channel, completion_queue):
    for _ in range(100):
        connectivity = channel.check_connectivity_state(True)
        channel.watch_connectivity_state(connectivity,
                                         cygrpc.Timespec(time.time() + 0.2),
                                         completion_queue, None)
        completion_queue.poll(deadline=cygrpc.Timespec(float('+inf')))
Exemplo n.º 2
0
 def next(self, deadline=float('+inf')):
     raw_event = self.completion_queue.poll(cygrpc.Timespec(deadline))
     if raw_event.type == cygrpc.CompletionType.queue_timeout:
         return None
     event_type = raw_event.type
     event_tag = raw_event.tag
     event_call = Call(raw_event.operation_call)
     if raw_event.request_call_details:
         event_call_details = _types.CallDetails(
             raw_event.request_call_details.method,
             raw_event.request_call_details.host,
             float(raw_event.request_call_details.deadline))
     else:
         event_call_details = None
     event_success = raw_event.success
     event_results = []
     if raw_event.is_new_request:
         event_results.append(
             _types.OpResult(_types.OpType.RECV_INITIAL_METADATA,
                             raw_event.request_metadata, None, None, None,
                             None))
     else:
         if raw_event.batch_operations:
             for operation in raw_event.batch_operations:
                 result_type = operation.type
                 result_initial_metadata = operation.received_metadata_or_none
                 result_trailing_metadata = operation.received_metadata_or_none
                 result_message = operation.received_message_or_none
                 if result_message is not None:
                     result_message = result_message.bytes()
                 result_cancelled = operation.received_cancelled_or_none
                 if operation.has_status:
                     result_status = _types.Status(
                         operation.received_status_code_or_none,
                         operation.received_status_details_or_none)
                 else:
                     result_status = None
                 event_results.append(
                     _types.OpResult(result_type, result_initial_metadata,
                                     result_trailing_metadata,
                                     result_message, result_status,
                                     result_cancelled))
     return _types.Event(event_type, event_tag, event_call,
                         event_call_details, event_results, event_success)
Exemplo n.º 3
0
def _poll_connectivity(state, channel, initial_try_to_connect):
    try_to_connect = initial_try_to_connect
    connectivity = channel.check_connectivity_state(try_to_connect)
    with state.lock:
        state.connectivity = (
            _common.
            CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY[connectivity])
        callbacks = tuple(
            callback for callback, unused_but_known_to_be_none_connectivity in
            state.callbacks_and_connectivities)
        for callback_and_connectivity in state.callbacks_and_connectivities:
            callback_and_connectivity[1] = state.connectivity
        if callbacks:
            _spawn_delivery(state, callbacks)
    completion_queue = cygrpc.CompletionQueue()
    while True:
        channel.watch_connectivity_state(connectivity,
                                         cygrpc.Timespec(time.time() + 0.2),
                                         completion_queue, None)
        event = completion_queue.poll()
        with state.lock:
            if not state.callbacks_and_connectivities and not state.try_to_connect:
                state.polling = False
                state.connectivity = None
                break
            try_to_connect = state.try_to_connect
            state.try_to_connect = False
        if event.success or try_to_connect:
            connectivity = channel.check_connectivity_state(try_to_connect)
            with state.lock:
                state.connectivity = (
                    _common.CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY[
                        connectivity])
                if not state.delivering:
                    # NOTE(nathaniel): The field is only ever used as a
                    # sequence so it's fine that both lists and tuples are
                    # assigned to it.
                    callbacks = _deliveries(state)  # pylint: disable=redefined-variable-type
                    if callbacks:
                        _spawn_delivery(state, callbacks)
Exemplo n.º 4
0
def _poll_connectivity(state, channel, initial_try_to_connect):
    try_to_connect = initial_try_to_connect
    connectivity = channel.check_connectivity_state(try_to_connect)
    with state.lock:
        state.connectivity = (
            _common.
            CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY[connectivity])
        callbacks = tuple(
            callback for callback, unused_but_known_to_be_none_connectivity in
            state.callbacks_and_connectivities)
        for callback_and_connectivity in state.callbacks_and_connectivities:
            callback_and_connectivity[1] = state.connectivity
        if callbacks:
            _spawn_delivery(state, callbacks)
    completion_queue = cygrpc.CompletionQueue()
    while True:
        channel.watch_connectivity_state(connectivity,
                                         cygrpc.Timespec(time.time() + 0.2),
                                         completion_queue, None)
        event = completion_queue.poll()
        with state.lock:
            if not state.callbacks_and_connectivities and not state.try_to_connect:
                state.polling = False
                state.connectivity = None
                break
            try_to_connect = state.try_to_connect
            state.try_to_connect = False
        if event.success or try_to_connect:
            connectivity = channel.check_connectivity_state(try_to_connect)
            with state.lock:
                state.connectivity = (
                    _common.CYGRPC_CONNECTIVITY_STATE_TO_CHANNEL_CONNECTIVITY[
                        connectivity])
                if not state.delivering:
                    callbacks = _deliveries(state)
                    if callbacks:
                        _spawn_delivery(state, callbacks)
Exemplo n.º 5
0
def _deadline(timeout):
    if timeout is None:
        return None, _INFINITE_FUTURE
    else:
        deadline = time.time() + timeout
        return deadline, cygrpc.Timespec(deadline)
Exemplo n.º 6
0
import sys
import threading
import time
import logging

import grpc
from grpc import _common
from grpc import _grpcio_metadata
from grpc._cython import cygrpc
from grpc.framework.foundation import callable_util

_USER_AGENT = 'grpc-python/{}'.format(_grpcio_metadata.__version__)

_EMPTY_FLAGS = 0
_INFINITE_FUTURE = cygrpc.Timespec(float('+inf'))
_EMPTY_METADATA = cygrpc.Metadata(())

_UNARY_UNARY_INITIAL_DUE = (
    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,
)
_UNARY_STREAM_INITIAL_DUE = (
    cygrpc.OperationType.send_initial_metadata,
    cygrpc.OperationType.send_message,
    cygrpc.OperationType.send_close_from_client,
    cygrpc.OperationType.receive_initial_metadata,
Exemplo n.º 7
0
 def testTimespec(self):
     now = time.time()
     now_timespec_a = cygrpc.Timespec(now)
     now_timespec_b = cygrpc.Timespec(now)
     self.assertAlmostEqual(now, float(now_timespec_a), places=8)
     self.assertEqual(now_timespec_a, now_timespec_b)
     self.assertLess(cygrpc.Timespec(now - 1), cygrpc.Timespec(now))
     self.assertGreater(cygrpc.Timespec(now + 1), cygrpc.Timespec(now))
     self.assertGreaterEqual(cygrpc.Timespec(now + 1), cygrpc.Timespec(now))
     self.assertGreaterEqual(cygrpc.Timespec(now), cygrpc.Timespec(now))
     self.assertLessEqual(cygrpc.Timespec(now - 1), cygrpc.Timespec(now))
     self.assertLessEqual(cygrpc.Timespec(now), cygrpc.Timespec(now))
     self.assertNotEqual(cygrpc.Timespec(now - 1), cygrpc.Timespec(now))
     self.assertNotEqual(cygrpc.Timespec(now + 1), cygrpc.Timespec(now))
Exemplo n.º 8
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()
Exemplo n.º 9
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
Exemplo n.º 10
0
 def testTimespec(self):
     now = time.time()
     timespec = cygrpc.Timespec(now)
     self.assertAlmostEqual(now, float(timespec), places=8)
Exemplo n.º 11
0
 def watch_connectivity_state(self, last_observed_state, deadline,
                              completion_queue, tag):
     self.channel.watch_connectivity_state(
         last_observed_state, cygrpc.Timespec(deadline),
         completion_queue.completion_queue, tag)
Exemplo n.º 12
0
 def create_call(self, completion_queue, method, host, deadline=None):
     internal_call = self.channel.create_call(
         None, 0, completion_queue.completion_queue, method, host,
         cygrpc.Timespec(deadline))
     return Call(internal_call)