1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.ipc;
19
20 import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE;
21 import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.newBlockingStub;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.fail;
24
25 import com.google.common.collect.Lists;
26 import com.google.protobuf.ServiceException;
27
28 import java.io.IOException;
29 import java.net.InetSocketAddress;
30
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.HBaseConfiguration;
33 import org.apache.hadoop.hbase.HConstants;
34 import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos;
35 import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EchoRequestProto;
36 import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EchoResponseProto;
37 import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface;
38 import org.apache.hadoop.hbase.testclassification.MediumTests;
39 import org.apache.hadoop.hbase.testclassification.RPCTests;
40 import org.apache.log4j.Level;
41 import org.apache.log4j.Logger;
42 import org.junit.After;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46
47
48
49
50
51
52 @Category({ RPCTests.class, MediumTests.class })
53 public class TestProtoBufRpc {
54 public final static String ADDRESS = "localhost";
55 public static int PORT = 0;
56 private InetSocketAddress isa;
57 private Configuration conf;
58 private RpcServerInterface server;
59
60 @Before
61 public void setUp() throws IOException {
62 this.conf = HBaseConfiguration.create();
63 Logger log = Logger.getLogger("org.apache.hadoop.ipc.HBaseServer");
64 log.setLevel(Level.DEBUG);
65 log = Logger.getLogger("org.apache.hadoop.ipc.HBaseServer.trace");
66 log.setLevel(Level.TRACE);
67
68
69 this.server = new RpcServer(null, "testrpc",
70 Lists.newArrayList(new RpcServer.BlockingServiceAndInterface(SERVICE, null)),
71 new InetSocketAddress(ADDRESS, PORT), conf, new FifoRpcScheduler(conf, 10));
72 InetSocketAddress address = server.getListenerAddress();
73 if (address == null) {
74 throw new IOException("Listener channel is closed");
75 }
76 this.isa = address;
77 this.server.start();
78 }
79
80 @After
81 public void tearDown() throws Exception {
82 server.stop();
83 }
84
85 @Test(expected = ServiceException.class
86
87 public void testProtoBufRpc() throws Exception {
88 RpcClient rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
89 try {
90 BlockingInterface stub = newBlockingStub(rpcClient, this.isa);
91
92 TestProtos.EmptyRequestProto emptyRequest = TestProtos.EmptyRequestProto.newBuilder().build();
93 stub.ping(null, emptyRequest);
94
95
96 EchoRequestProto echoRequest = EchoRequestProto.newBuilder().setMessage("hello").build();
97 EchoResponseProto echoResponse = stub.echo(null, echoRequest);
98 assertEquals(echoResponse.getMessage(), "hello");
99
100 stub.error(null, emptyRequest);
101 fail("Expected exception is not thrown");
102 } finally {
103 rpcClient.close();
104 }
105 }
106 }