RpcTest.java revision 420c9859
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 29420c9859SShuo Chenpublic class RpcTest { 30420c9859SShuo Chen 31420c9859SShuo Chen @Test 32420c9859SShuo Chen public void testEncoder() throws Exception { 33420c9859SShuo Chen RpcEncoder encoder = new RpcEncoder(); 34420c9859SShuo Chen RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(1).build(); 35420c9859SShuo Chen encoder.encode(null, null, message); 36420c9859SShuo Chen } 37420c9859SShuo Chen 38420c9859SShuo Chen @Test 39420c9859SShuo Chen public void testDecoder() throws Exception { 40420c9859SShuo Chen RpcEncoder encoder = new RpcEncoder(); 41420c9859SShuo Chen RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(1).build(); 42420c9859SShuo Chen ChannelBuffer buffer = (ChannelBuffer) encoder.encode(null, null, message); 43420c9859SShuo Chen 44420c9859SShuo Chen RpcDecoder decoder = new RpcDecoder(); 45420c9859SShuo Chen RpcMessage decodedMessage = (RpcMessage) decoder.decode(null, null, buffer); 46420c9859SShuo Chen assertEquals(1, decodedMessage.getId()); 47420c9859SShuo Chen } 48420c9859SShuo Chen 49420c9859SShuo Chen @Test 50420c9859SShuo Chen public void testDecoder2() throws Exception { 51420c9859SShuo Chen RpcEncoder encoder = new RpcEncoder(); 52420c9859SShuo Chen RpcMessage message = RpcMessage.newBuilder().setType(MessageType.REQUEST).setId(2).build(); 53420c9859SShuo Chen ChannelBuffer buffer = (ChannelBuffer) encoder.encode(null, null, message); 54420c9859SShuo Chen ChannelBuffer buf2 = new BigEndianHeapChannelBuffer(buffer.readableBytes() + 8); 55420c9859SShuo Chen buf2.writeInt(123); 56420c9859SShuo Chen buf2.writeBytes(buffer); 57420c9859SShuo Chen 58420c9859SShuo Chen buf2.readInt(); 59420c9859SShuo Chen RpcDecoder decoder = new RpcDecoder(); 60420c9859SShuo Chen RpcMessage decodedMessage = (RpcMessage) decoder.decode(null, null, buf2); 61420c9859SShuo Chen assertEquals(2, decodedMessage.getId()); 62420c9859SShuo Chen } 63420c9859SShuo Chen 64420c9859SShuo Chen SudokuResponse gotResponse; 65420c9859SShuo Chen 66420c9859SShuo Chen @Test 67420c9859SShuo Chen public void testClient() throws Exception { 68420c9859SShuo Chen MockChannel mockChannel = new MockChannel(); 69420c9859SShuo Chen RpcChannel channel = new RpcChannel(mockChannel); 70420c9859SShuo Chen SudokuService remoteService = Sudoku.SudokuService.newStub(channel); 71420c9859SShuo Chen SudokuRequest request = SudokuRequest.newBuilder().setCheckerboard("001010").build(); 72420c9859SShuo Chen remoteService.solve(null, request, new RpcCallback<Sudoku.SudokuResponse>() { 73420c9859SShuo Chen @Override 74420c9859SShuo Chen public void run(SudokuResponse response) { 75420c9859SShuo Chen // System.out.println(parameter); 76420c9859SShuo Chen gotResponse = response; 77420c9859SShuo Chen } 78420c9859SShuo Chen }); 79420c9859SShuo Chen RpcMessage message = (RpcMessage) mockChannel.message; 80420c9859SShuo Chen assertEquals(1, message.getId()); 81420c9859SShuo Chen assertEquals(MessageType.REQUEST, message.getType()); 82420c9859SShuo Chen assertEquals(remoteService.getDescriptorForType().getFullName(), message.getService()); 83420c9859SShuo Chen assertEquals("Solve", message.getMethod()); 84420c9859SShuo Chen 85420c9859SShuo Chen SudokuResponse sudokuResponse = SudokuResponse.newBuilder() 86420c9859SShuo Chen .setSolved(true) 87420c9859SShuo Chen .setCheckerboard("123456") 88420c9859SShuo Chen .build(); 89420c9859SShuo Chen RpcMessage response = RpcMessage.newBuilder() 90420c9859SShuo Chen .setType(MessageType.RESPONSE) 91420c9859SShuo Chen .setId(1) 92420c9859SShuo Chen .setResponse(sudokuResponse.toByteString()) 93420c9859SShuo Chen .build(); 94420c9859SShuo Chen channel.messageReceived(null, new UpstreamMessageEvent(mockChannel, response, null)); 95420c9859SShuo Chen assertEquals(sudokuResponse, gotResponse); 96420c9859SShuo Chen } 97420c9859SShuo Chen 98420c9859SShuo Chen SudokuRequest gotRequest; 99420c9859SShuo Chen 100420c9859SShuo Chen @Test 101420c9859SShuo Chen public void testServer() throws Exception { 102420c9859SShuo Chen 103420c9859SShuo Chen MockChannel mockChannel = new MockChannel(); 104420c9859SShuo Chen Map<String, Service> services = new HashMap<String, Service>(); 105420c9859SShuo Chen final SudokuResponse sudokuResponse = SudokuResponse.newBuilder() 106420c9859SShuo Chen .setSolved(true) 107420c9859SShuo Chen .setCheckerboard("98765") 108420c9859SShuo Chen .build(); 109420c9859SShuo Chen Interface mockImpl = new Interface() { 110420c9859SShuo Chen 111420c9859SShuo Chen @Override 112420c9859SShuo Chen public void solve(RpcController controller, SudokuRequest request, 113420c9859SShuo Chen RpcCallback<SudokuResponse> done) { 114420c9859SShuo Chen gotRequest = request; 115420c9859SShuo Chen 116420c9859SShuo Chen done.run(sudokuResponse); 117420c9859SShuo Chen } 118420c9859SShuo Chen }; 119420c9859SShuo Chen services.put(SudokuService.getDescriptor().getFullName(), 120420c9859SShuo Chen SudokuService.newReflectiveService(mockImpl)); 121420c9859SShuo Chen RpcChannel channel = new RpcChannel(mockChannel); 122420c9859SShuo Chen channel.setServiceMap(services); 123420c9859SShuo Chen 124420c9859SShuo Chen SudokuRequest sudokuRequest = SudokuRequest.newBuilder().setCheckerboard("001010").build(); 125420c9859SShuo Chen RpcMessage request = RpcMessage.newBuilder() 126420c9859SShuo Chen .setType(MessageType.REQUEST) 127420c9859SShuo Chen .setId(2) 128420c9859SShuo Chen .setService(SudokuService.getDescriptor().getFullName()) 129420c9859SShuo Chen .setMethod("Solve") 130420c9859SShuo Chen .setRequest(sudokuRequest.toByteString()) 131420c9859SShuo Chen .build(); 132420c9859SShuo Chen 133420c9859SShuo Chen channel.messageReceived(null, new UpstreamMessageEvent(mockChannel, request, null)); 134420c9859SShuo Chen assertEquals(sudokuRequest, gotRequest); 135420c9859SShuo Chen 136420c9859SShuo Chen RpcMessage response = RpcMessage.newBuilder() 137420c9859SShuo Chen .setType(MessageType.RESPONSE) 138420c9859SShuo Chen .setId(2) 139420c9859SShuo Chen .setResponse(sudokuResponse.toByteString()) 140420c9859SShuo Chen .build(); 141420c9859SShuo Chen assertEquals(response, mockChannel.message); 142420c9859SShuo Chen } 143420c9859SShuo Chen} 144