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