def test_echo(self):
        TcpServer(self.ip, self.port, EchoService)
        client = TcpClient(self.ip, self.port, EchoClient, IEchoService_Stub)

        client.async_connect()

        echo_recorder = EchoRecorder()
        rpc_count = 0

        for i in xrange(10):
            asyncore.loop(0.1, count=1)
            if client.stub:
                if not client.service.streamout:
                    client.service.set_streamout(echo_recorder)
                request = EchoString()
                request.message = str(rpc_count)
                controller = RpcController(client.rpc_channel)
                client.stub.echo(controller, request, None)
                rpc_count += 1

        asyncore.loop(0.1, count=30)

        self.assertEqual(len(echo_recorder.record), rpc_count)

        echo_recorder.record.sort(cmp=lambda x, y: int(x) < int(y))
        for i in xrange(rpc_count):
            self.assertEqual(echo_recorder.record[i], str(i))
    def test_connection(self):
        server = TcpServer(self.ip, self.port, DummyService)
        client = TcpClient(self.ip, self.port, DummyService, DummyStub)
        client.async_connect()

        asyncore.loop(timeout=0.1, count=10)

        self.assertEqual(client.status, TcpConnection.ST_ESTABLISHED)

        server.close()
        client.close()
Ejemplo n.º 3
0
# coding:utf8
import sys
sys.path.append('../')

from proto.game_service_pb2 import IEchoService_Stub,IEchoClient,RequestMessage
from rpc.tcp_client import TcpClient
import asyncore

LISTEN_IP = "127.0.0.1"
LISTEN_PORT = 1888

# 被调用方,接收调用方(stub)的rpc请求
class MyEchoClientReply(IEchoClient):
	def echo_reply(self, rpc_controller, request, done):
		print "MyEchoClientReply:%s"%request.msg
		

if __name__ == "__main__":
	request = RequestMessage()
	request.msg = "just for test"
	
	client = TcpClient(LISTEN_IP, LISTEN_PORT, IEchoService_Stub, MyEchoClientReply)
	client.sync_connect()
	
	client.stub.echo(None, request, None)
	
	asyncore.loop()