Beispiel #1
0
def run():
    channel = grpc.insecure_channel('localhost:50051')
    stub = test_pb2_grpc.GreeterStub(channel)
    duration = 10
    end = time.time() + duration
    msgs = 0
    data = b'b' * (1 << 20)
    while time.time() < end:
        response = stub.SendData(test_pb2.Request(name=data))
        msgs += 1

    print('Received {} messages in {} second(s).'.format(msgs, duration))
 def setUp(self):
     self.channel = SocketRpcChannel(host=host, port=success_port)
     self.controller = self.channel.newController()
     self.service = test_pb2.TestService_Stub(self.channel)
     self.request = test_pb2.Request()
     self.request.str_data = 'I like cheese'
     self.callback = lambda request, response: response
     self.thread = service.RpcThread(
         test_pb2.TestService_Stub.TestMethod,
         self.service, self.controller, self.request, self.callback)
     ServerThread.start_server(
         success_port, fail_port, Exception('YOU FAIL!'))
    def setUp(self):
        self.service = service.RpcService(test_pb2.TestService_Stub,
                                          success_port,
                                          host)
        self.fail_service = service.RpcService(test_pb2.TestService_Stub,
                                          fail_port,
                                          host)
        self.request = test_pb2.Request()
        self.request.str_data = 'I like cheese'

        # Start a server thread
        ServerThread.start_server(
            success_port, fail_port, Exception('YOU FAIL!'))
    def setUp(self):
        # Define some arbitrary values to satisfy the interface
        self.client_addr = 'here'
        self.server_addr = 'there'

        # Define a simple service request
        self.service_request = test_pb2.Request()
        self.service_request.str_data = 'The lord giveth'
        self.serialized_request = self.service_request.SerializeToString()

        # Define an RPC request with the service request as payload
        self.rpc_request = rpc_pb2.Request()
        self.rpc_request.request_proto = self.serialized_request
        self.testserver = server.SocketRpcServer(8090)
    def setUp(self):
        # Create a channel connected to a fake socket
        self.factory = FakeSocketFactory()
        self.socket = FakeSocket()
        self.channel = ch.SocketRpcChannel(socketFactory=self.factory)
        self.controller = self.channel.newController()

        self.lc = ch._LifeCycle(self.controller, self.channel)

        self.factory.setSocket(self.socket)

        # Define a simple service request
        self.service_request = test_pb2.Request()
        self.service_request.str_data = 'The lord giveth'
        self.serialized_request = self.service_request.SerializeToString()

        # Define an RPC request with the service request as payload
        self.rpc_request = rpc_pb2.Request()
        self.rpc_request.request_proto = self.serialized_request
    def setUp(self):

        # Create a channel connected to a fake socket
        self.factory = FakeSocketFactory()
        self.channel = ch.SocketRpcChannel(socketFactory=self.factory)

        # Define a simple service request
        self.service_request = test_pb2.Request()
        self.service_request.str_data = 'The lord giveth'
        self.serialized_request = self.service_request.SerializeToString()

        # Define a service response
        self.service_response = test_pb2.Response()
        self.service_response.str_data = 'And the lord taketh away'
        self.serialized_response = \
            self.service_response.SerializePartialToString()

        # Define an RPC request with the service request as payload
        self.rpc_request = rpc_pb2.Request()
        self.rpc_request.request_proto = self.serialized_request
    def test_createRpcRequest(self):
        '''Test createRpcRequest - normal usage.'''

        # Instantiate the test service, and get a reference to the method
        method_name = 'TestMethod'
        service = TestServiceImpl()
        method = service.DESCRIPTOR.FindMethodByName(method_name)

        # Define a simple service request
        service_request = test_pb2.Request()
        service_request.str_data = 'The lord giveth'
        serialized_request = service_request.SerializeToString()

        # Define an RPC request with the service request as payload
        expected_rpc = rpc_pb2.Request()
        expected_rpc.request_proto = serialized_request
        expected_rpc.service_name = service.DESCRIPTOR.full_name
        expected_rpc.method_name = method_name

        self.assertEqual(
            self.channel.createRpcRequest(method, service_request),
            expected_rpc, 'createRpcRequest - normal usage')
    def testIncompleteRequest(self):
        '''Test calling RPC with incomplete request.'''

        # Create data
        service_request = test_pb2.Request()

        # Fake socket with prepared response
        socket = FakeSocket()
        socket.withResponseProto(self.service_response)
        socketFactory = FakeSocketFactory()
        socketFactory.setSocket(socket)

        # Create channel
        channel = ch.SocketRpcChannel("host", -1, socketFactory)

        # Create the service
        service = test_pb2.TestService_Stub(channel)

        # Call RPC method
        controller = channel.newController()
        callback = FakeCallback()
        service.TestMethod(controller, service_request, callback)

        self.assertTrue(callback.invoked, 'Callback invoked')
        self.assertTrue(callback.response is None, 'Response')
        self.assertEquals(rpc_pb2.BAD_REQUEST_PROTO, controller.reason)
        self.assertTrue(controller.failed())

        # Call Blocking RPC
        controller = channel.newController()
        try:
            service.TestMethod(controller, service_request, None)
            self.fail('Should have thrown error')
        except RpcError:
            self.assertTrue(controller.failed())
            self.assertEquals(rpc_pb2.BAD_REQUEST_PROTO, controller.reason, 
                              'Error reason')
Beispiel #9
0
def test(ip, img):
    with grpc.insecure_channel(ip) as channel:
        req = test_pb2.Request(image=img)
        stub = test_pb2_grpc.SpeedTestStub(channel)
        ret = stub.work(req)
    return ret