Example #1
0
    def test_stub_context(self):
        server = implementations.server(
            self._method_implementations, options=self._server_options)
        port = server.add_secure_port('[::]:0', self._server_credentials)
        server.start()

        channel = test_utilities.not_really_secure_channel(
            'localhost', port, self._channel_credentials, _SERVER_HOST_OVERRIDE)
        dynamic_stub = implementations.dynamic_stub(
            channel, _GROUP, self._cardinalities, options=self._stub_options)
        for _ in range(100):
            with dynamic_stub:
                pass
        for _ in range(10):
            with dynamic_stub:
                call_options = interfaces.grpc_call_options(
                    disable_compression=True)
                response = getattr(dynamic_stub, _UNARY_UNARY)(
                    _REQUEST,
                    test_constants.LONG_TIMEOUT,
                    protocol_options=call_options)
                self.assertEqual(_RESPONSE, response)
                self.assertIsNotNone(self._servicer.peer())

        server.stop(test_constants.SHORT_TIMEOUT).wait()
Example #2
0
    def test_stub_context(self):
        server = implementations.server(self._method_implementations,
                                        options=self._server_options)
        port = server.add_secure_port('[::]:0', self._server_credentials)
        server.start()

        channel = test_utilities.not_really_secure_channel(
            'localhost', port, self._channel_credentials,
            _SERVER_HOST_OVERRIDE)
        dynamic_stub = implementations.dynamic_stub(channel,
                                                    _GROUP,
                                                    self._cardinalities,
                                                    options=self._stub_options)
        for _ in range(100):
            with dynamic_stub:
                pass
        for _ in range(10):
            with dynamic_stub:
                call_options = interfaces.grpc_call_options(
                    disable_compression=True)
                response = getattr(dynamic_stub,
                                   _UNARY_UNARY)(_REQUEST,
                                                 test_constants.LONG_TIMEOUT,
                                                 protocol_options=call_options)
                self.assertEqual(_RESPONSE, response)
                self.assertIsNotNone(self._servicer.peer())

        server.stop(test_constants.SHORT_TIMEOUT).wait()
Example #3
0
 def test_unary_stream(self):
     call_options = interfaces.grpc_call_options(disable_compression=True)
     response_iterator = getattr(self._dynamic_stub, _UNARY_STREAM)(
         _REQUEST,
         test_constants.LONG_TIMEOUT,
         protocol_options=call_options)
     self._servicer.block_until_serviced()
     self.assertIsNotNone(self._servicer.peer())
Example #4
0
 def test_unary_unary(self):
     call_options = interfaces.grpc_call_options(disable_compression=True)
     response = getattr(self._dynamic_stub,
                        _UNARY_UNARY)(_REQUEST,
                                      test_constants.LONG_TIMEOUT,
                                      protocol_options=call_options)
     self.assertEqual(_RESPONSE, response)
     self.assertIsNotNone(self._servicer.peer())
Example #5
0
 def test_stream_unary(self):
   call_options = interfaces.grpc_call_options()
   request_iterator = _BlockingIterator(iter((_REQUEST,)))
   response_future = getattr(self._dynamic_stub, _STREAM_UNARY).future(
       request_iterator, test_constants.LONG_TIMEOUT,
       protocol_options=call_options)
   response_future.protocol_context().disable_next_request_compression()
   request_iterator.allow()
   response_future.protocol_context().disable_next_request_compression()
   request_iterator.allow()
   self._servicer.block_until_serviced()
   self.assertIsNotNone(self._servicer.peer())
   self.assertEqual(_RESPONSE, response_future.result())
Example #6
0
 def test_unary_stream(self):
   call_options = interfaces.grpc_call_options(
       disable_compression=True, credentials=self._call_credentials)
   response_iterator = getattr(self._dynamic_stub, _UNARY_STREAM)(
       _REQUEST, test_constants.LONG_TIMEOUT, protocol_options=call_options)
   self._servicer.block_until_serviced()
   self.assertIsNotNone(self._servicer.peer())
   invocation_metadata = [(metadatum.key, metadatum.value) for metadatum in
                          self._servicer._invocation_metadata]
   self.assertIn(
       (_PER_RPC_CREDENTIALS_METADATA_KEY,
        _PER_RPC_CREDENTIALS_METADATA_VALUE),
       invocation_metadata)
Example #7
0
def _per_rpc_creds(stub, args):
  json_key_filename = os.environ[
      oauth2client_client.GOOGLE_APPLICATION_CREDENTIALS]
  wanted_email = json.load(open(json_key_filename, 'rb'))['client_email']
  credentials = oauth2client_client.GoogleCredentials.get_application_default()
  scoped_credentials = credentials.create_scoped([args.oauth_scope])
  call_creds = implementations.google_call_credentials(scoped_credentials)
  options = interfaces.grpc_call_options(disable_compression=False,
                                         credentials=call_creds)
  response = _large_unary_common_behavior(stub, True, False,
                                          protocol_options=options)
  if wanted_email != response.username:
    raise ValueError(
        'expected username %s, got %s' % (wanted_email, response.username))
Example #8
0
 def test_stream_unary(self):
     call_options = interfaces.grpc_call_options()
     request_iterator = _BlockingIterator(iter((_REQUEST, )))
     response_future = getattr(self._dynamic_stub, _STREAM_UNARY).future(
         request_iterator,
         test_constants.LONG_TIMEOUT,
         protocol_options=call_options)
     response_future.protocol_context().disable_next_request_compression()
     request_iterator.allow()
     response_future.protocol_context().disable_next_request_compression()
     request_iterator.allow()
     self._servicer.block_until_serviced()
     self.assertIsNotNone(self._servicer.peer())
     self.assertEqual(_RESPONSE, response_future.result())
Example #9
0
 def test_unary_stream(self):
     call_options = interfaces.grpc_call_options(
         disable_compression=True, credentials=self._call_credentials)
     response_iterator = getattr(self._dynamic_stub, _UNARY_STREAM)(
         _REQUEST,
         test_constants.LONG_TIMEOUT,
         protocol_options=call_options)
     self._servicer.block_until_serviced()
     self.assertIsNotNone(self._servicer.peer())
     invocation_metadata = [
         (metadatum.key, metadatum.value)
         for metadatum in self._servicer._invocation_metadata
     ]
     self.assertIn((_PER_RPC_CREDENTIALS_METADATA_KEY,
                    _PER_RPC_CREDENTIALS_METADATA_VALUE),
                   invocation_metadata)
Example #10
0
 def test_stream_stream(self):
     call_options = interfaces.grpc_call_options(
         credentials=self._client_credentials)
     request_iterator = _BlockingIterator(iter((_REQUEST, )))
     response_iterator = getattr(self._dynamic_stub, _STREAM_STREAM)(
         request_iterator,
         test_constants.SHORT_TIMEOUT,
         protocol_options=call_options)
     response_iterator.protocol_context().disable_next_request_compression()
     request_iterator.allow()
     response = next(response_iterator)
     response_iterator.protocol_context().disable_next_request_compression()
     request_iterator.allow()
     self._servicer.block_until_serviced()
     self.assertIsNotNone(self._servicer.peer())
     self.assertEqual(_RESPONSE, response)
Example #11
0
def _per_rpc_creds(stub, args):
    json_key_filename = os.environ[
        oauth2client_client.GOOGLE_APPLICATION_CREDENTIALS]
    wanted_email = json.load(open(json_key_filename, 'rb'))['client_email']
    credentials = oauth2client_client.GoogleCredentials.get_application_default(
    )
    scoped_credentials = credentials.create_scoped([args.oauth_scope])
    call_creds = implementations.google_call_credentials(scoped_credentials)
    options = interfaces.grpc_call_options(disable_compression=False,
                                           credentials=call_creds)
    response = _large_unary_common_behavior(stub,
                                            True,
                                            False,
                                            protocol_options=options)
    if wanted_email != response.username:
        raise ValueError('expected username %s, got %s' %
                         (wanted_email, response.username))
Example #12
0
 def test_stream_unary(self):
   call_options = interfaces.grpc_call_options(
       credentials=self._call_credentials)
   request_iterator = _BlockingIterator(iter((_REQUEST,)))
   response_future = getattr(self._dynamic_stub, _STREAM_UNARY).future(
       request_iterator, test_constants.LONG_TIMEOUT,
       protocol_options=call_options)
   response_future.protocol_context().disable_next_request_compression()
   request_iterator.allow()
   response_future.protocol_context().disable_next_request_compression()
   request_iterator.allow()
   self._servicer.block_until_serviced()
   self.assertIsNotNone(self._servicer.peer())
   self.assertEqual(_RESPONSE, response_future.result())
   invocation_metadata = [(metadatum.key, metadatum.value) for metadatum in
                          self._servicer._invocation_metadata]
   self.assertIn(
       (_PER_RPC_CREDENTIALS_METADATA_KEY,
        _PER_RPC_CREDENTIALS_METADATA_VALUE),
       invocation_metadata)
Example #13
0
 def test_stream_unary(self):
     call_options = interfaces.grpc_call_options(
         credentials=self._call_credentials)
     request_iterator = _BlockingIterator(iter((_REQUEST, )))
     response_future = getattr(self._dynamic_stub, _STREAM_UNARY).future(
         request_iterator,
         test_constants.LONG_TIMEOUT,
         protocol_options=call_options)
     response_future.protocol_context().disable_next_request_compression()
     request_iterator.allow()
     response_future.protocol_context().disable_next_request_compression()
     request_iterator.allow()
     self._servicer.block_until_serviced()
     self.assertIsNotNone(self._servicer.peer())
     self.assertEqual(_RESPONSE, response_future.result())
     invocation_metadata = [
         (metadatum.key, metadatum.value)
         for metadatum in self._servicer._invocation_metadata
     ]
     self.assertIn((_PER_RPC_CREDENTIALS_METADATA_KEY,
                    _PER_RPC_CREDENTIALS_METADATA_VALUE),
                   invocation_metadata)
Example #14
0
 def test_unary_stream(self):
   call_options = interfaces.grpc_call_options(disable_compression=True)
   response_iterator = getattr(self._dynamic_stub, _UNARY_STREAM)(
       _REQUEST, test_constants.LONG_TIMEOUT, protocol_options=call_options)
   self._servicer.block_until_serviced()
   self.assertIsNotNone(self._servicer.peer())
Example #15
0
 def test_unary_unary(self):
   call_options = interfaces.grpc_call_options(disable_compression=True)
   response = getattr(self._dynamic_stub, _UNARY_UNARY)(
       _REQUEST, test_constants.LONG_TIMEOUT, protocol_options=call_options)
   self.assertEqual(_RESPONSE, response)
   self.assertIsNotNone(self._servicer.peer())