1420c9859SShuo Chenpackage muduo.rpc.test; 2420c9859SShuo Chen 3420c9859SShuo Chenimport static org.junit.Assert.assertEquals; 4420c9859SShuo Chen 5420c9859SShuo Chenimport java.util.HashMap; 6420c9859SShuo Chenimport java.util.Map; 7420c9859SShuo Chen 8420c9859SShuo Chenimport muduo.rpc.RpcChannel; 9420c9859SShuo Chenimport muduo.rpc.RpcDecoder; 10420c9859SShuo Chenimport muduo.rpc.RpcEncoder; 11420c9859SShuo Chenimport muduo.rpc.proto.RpcProto.MessageType; 12420c9859SShuo Chenimport muduo.rpc.proto.RpcProto.RpcMessage; 13420c9859SShuo Chen 14420c9859SShuo Chenimport org.jboss.netty.buffer.BigEndianHeapChannelBuffer; 15420c9859SShuo Chenimport org.jboss.netty.buffer.ChannelBuffer; 16420c9859SShuo Chenimport org.jboss.netty.channel.UpstreamMessageEvent; 17420c9859SShuo Chenimport org.junit.Test; 18420c9859SShuo Chen 19420c9859SShuo Chenimport sudoku.Sudoku; 20420c9859SShuo Chenimport sudoku.Sudoku.SudokuRequest; 21420c9859SShuo Chenimport sudoku.Sudoku.SudokuResponse; 22420c9859SShuo Chenimport sudoku.Sudoku.SudokuService; 23420c9859SShuo Chenimport sudoku.Sudoku.SudokuService.Interface; 24420c9859SShuo Chen 25420c9859SShuo Chenimport com.google.protobuf.RpcCallback; 26420c9859SShuo Chenimport com.google.protobuf.RpcController; 27420c9859SShuo Chenimport com.google.protobuf.Service; 28420c9859SShuo Chen 29a6e419c7SShuo Chenimport echo.EchoProto.EchoService; 30a6e419c7SShuo Chenimport echo.EchoServer; 31a6e419c7SShuo Chenimport echo.EchoProto.EchoRequest; 32a6e419c7SShuo Chen 33420c9859SShuo Chenpublic class RpcTest { 34420c9859SShuo Chen 35420c9859SShuo Chen @Test 36420c9859SShuo Chen public void testEncoder() throws Exception { 37420c9859SShuo Chen RpcEncoder encoder = new RpcEncoder(); 38420c9859SShuo Chen RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(1).build(); 39420c9859SShuo Chen encoder.encode(null, null, message); 40420c9859SShuo Chen } 41420c9859SShuo Chen 42420c9859SShuo Chen @Test 43a6e419c7SShuo Chen public void testEncoder2() throws Exception { 44a6e419c7SShuo Chen RpcEncoder encoder = new RpcEncoder(); 45a6e419c7SShuo Chen EchoRequest request = EchoRequest.newBuilder().setPayload("Hello").build(); 46a6e419c7SShuo Chen RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(1) 47a6e419c7SShuo Chen .setService(EchoService.getDescriptor().getFullName()) 48a6e419c7SShuo Chen .setMethod(EchoService.getDescriptor().getMethods().get(0).getName()) 49a6e419c7SShuo Chen .setRequest(request.toByteString()).build(); 50a6e419c7SShuo Chen encoder.encode(null, null, message); 51a6e419c7SShuo Chen } 52a6e419c7SShuo Chen 53a6e419c7SShuo Chen @Test 54420c9859SShuo Chen public void testDecoder() throws Exception { 55420c9859SShuo Chen RpcEncoder encoder = new RpcEncoder(); 56b5a588dfSShuo Chen SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build(); 57b5a588dfSShuo Chen RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(1) 58b5a588dfSShuo Chen .setService(SudokuService.getDescriptor().getFullName()) 59b5a588dfSShuo Chen .setMethod(SudokuService.getDescriptor().getMethods().get(0).getName()) 60b5a588dfSShuo Chen .setRequest(request.toByteString()).build(); 61420c9859SShuo Chen ChannelBuffer buffer = (ChannelBuffer) encoder.encode(null, null, message); 62420c9859SShuo Chen 63420c9859SShuo Chen RpcDecoder decoder = new RpcDecoder(); 64420c9859SShuo Chen RpcMessage decodedMessage = (RpcMessage) decoder.decode(null, null, buffer); 65420c9859SShuo Chen assertEquals(1, decodedMessage.getId()); 66420c9859SShuo Chen } 67420c9859SShuo Chen 68420c9859SShuo Chen @Test 69420c9859SShuo Chen public void testDecoder2() throws Exception { 70420c9859SShuo Chen RpcEncoder encoder = new RpcEncoder(); 71420c9859SShuo Chen RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(2).build(); 72420c9859SShuo Chen ChannelBuffer buffer = (ChannelBuffer) encoder.encode(null, null, message); 73420c9859SShuo Chen ChannelBuffer buf2 = new BigEndianHeapChannelBuffer(buffer.readableBytes() + 8); 74420c9859SShuo Chen buf2.writeInt(123); 75420c9859SShuo Chen buf2.writeBytes(buffer); 76420c9859SShuo Chen 77420c9859SShuo Chen buf2.readInt(); 78420c9859SShuo Chen RpcDecoder decoder = new RpcDecoder(); 79420c9859SShuo Chen RpcMessage decodedMessage = (RpcMessage) decoder.decode(null, null, buf2); 80420c9859SShuo Chen assertEquals(2, decodedMessage.getId()); 81420c9859SShuo Chen } 82420c9859SShuo Chen 83420c9859SShuo Chen SudokuResponse gotResponse; 84420c9859SShuo Chen 85420c9859SShuo Chen @Test 86420c9859SShuo Chen public void testClient() throws Exception { 87420c9859SShuo Chen MockChannel mockChannel = new MockChannel(); 88420c9859SShuo Chen RpcChannel channel = new RpcChannel(mockChannel); 89420c9859SShuo Chen SudokuService remoteService = Sudoku.SudokuService.newStub(channel); 90420c9859SShuo Chen SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build(); 91420c9859SShuo Chen remoteService.solve(null, request, new RpcCallback<Sudoku.SudokuResponse>() { 92420c9859SShuo Chen @Override 93420c9859SShuo Chen public void run(SudokuResponse response) { 94420c9859SShuo Chen // System.out.println(parameter); 95420c9859SShuo Chen gotResponse = response; 96420c9859SShuo Chen } 97420c9859SShuo Chen }); 98420c9859SShuo Chen RpcMessage message = (RpcMessage) mockChannel.message; 99420c9859SShuo Chen assertEquals(1, message.getId()); 100420c9859SShuo Chen assertEquals(MessageType.REQUEST, message.getType()); 101420c9859SShuo Chen assertEquals(remoteService.getDescriptorForType().getFullName(), message.getService()); 102420c9859SShuo Chen assertEquals("Solve", message.getMethod()); 103420c9859SShuo Chen 104420c9859SShuo Chen SudokuResponse sudokuResponse = SudokuResponse.newBuilder() 105420c9859SShuo Chen .setSolved(true) 106420c9859SShuo Chen .setCheckerboard("123456") 107420c9859SShuo Chen .build(); 108420c9859SShuo Chen RpcMessage response = RpcMessage.newBuilder() 109420c9859SShuo Chen .setType(MessageType.RESPONSE) 110420c9859SShuo Chen .setId(1) 111420c9859SShuo Chen .setResponse(sudokuResponse.toByteString()) 112420c9859SShuo Chen .build(); 113420c9859SShuo Chen channel.messageReceived(null, new UpstreamMessageEvent(mockChannel, response, null)); 114420c9859SShuo Chen assertEquals(sudokuResponse, gotResponse); 115420c9859SShuo Chen } 116420c9859SShuo Chen 117420c9859SShuo Chen SudokuRequest gotRequest; 118420c9859SShuo Chen 119420c9859SShuo Chen @Test 120420c9859SShuo Chen public void testServer() throws Exception { 121420c9859SShuo Chen 122420c9859SShuo Chen MockChannel mockChannel = new MockChannel(); 123420c9859SShuo Chen Map<String, Service> services = new HashMap<String, Service>(); 124420c9859SShuo Chen final SudokuResponse sudokuResponse = SudokuResponse.newBuilder() 125420c9859SShuo Chen .setSolved(true) 126420c9859SShuo Chen .setCheckerboard("98765") 127420c9859SShuo Chen .build(); 128420c9859SShuo Chen Interface mockImpl = new Interface() { 129420c9859SShuo Chen 130420c9859SShuo Chen @Override 131420c9859SShuo Chen public void solve(RpcController controller, SudokuRequest request, 132420c9859SShuo Chen RpcCallback<SudokuResponse> done) { 133420c9859SShuo Chen gotRequest = request; 134420c9859SShuo Chen 135420c9859SShuo Chen done.run(sudokuResponse); 136420c9859SShuo Chen } 137420c9859SShuo Chen }; 138420c9859SShuo Chen services.put(SudokuService.getDescriptor().getFullName(), 139420c9859SShuo Chen SudokuService.newReflectiveService(mockImpl)); 140420c9859SShuo Chen RpcChannel channel = new RpcChannel(mockChannel); 141420c9859SShuo Chen channel.setServiceMap(services); 142420c9859SShuo Chen 143420c9859SShuo Chen SudokuRequest sudokuRequest = SudokuRequest.newBuilder().setCheckerboard("001010").build(); 144420c9859SShuo Chen RpcMessage request = RpcMessage.newBuilder() 145420c9859SShuo Chen .setType(MessageType.REQUEST) 146420c9859SShuo Chen .setId(2) 147420c9859SShuo Chen .setService(SudokuService.getDescriptor().getFullName()) 148420c9859SShuo Chen .setMethod("Solve") 149420c9859SShuo Chen .setRequest(sudokuRequest.toByteString()) 150420c9859SShuo Chen .build(); 151420c9859SShuo Chen 152420c9859SShuo Chen channel.messageReceived(null, new UpstreamMessageEvent(mockChannel, request, null)); 153420c9859SShuo Chen assertEquals(sudokuRequest, gotRequest); 154420c9859SShuo Chen 155420c9859SShuo Chen RpcMessage response = RpcMessage.newBuilder() 156420c9859SShuo Chen .setType(MessageType.RESPONSE) 157420c9859SShuo Chen .setId(2) 158420c9859SShuo Chen .setResponse(sudokuResponse.toByteString()) 159420c9859SShuo Chen .build(); 160420c9859SShuo Chen assertEquals(response, mockChannel.message); 161420c9859SShuo Chen } 162420c9859SShuo Chen} 163