1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.ipc;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotSame;
23
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26
27 import org.apache.hadoop.conf.Configuration;
28 import org.apache.hadoop.hbase.MediumTests;
29 import org.apache.hadoop.io.Text;
30 import org.apache.hadoop.io.Writable;
31 import org.junit.Test;
32 import org.junit.experimental.categories.Category;
33
34 import com.google.protobuf.DescriptorProtos;
35 import com.google.protobuf.DescriptorProtos.EnumDescriptorProto;
36
37
38 @Category(MediumTests.class)
39 public class TestPBOnWritableRpc {
40
41 private static Configuration conf = new Configuration();
42
43 public interface TestProtocol extends VersionedProtocol {
44 public static final long VERSION = 1L;
45
46 String echo(String value) throws IOException;
47 Writable echo(Writable value) throws IOException;
48
49 DescriptorProtos.EnumDescriptorProto exchangeProto(
50 DescriptorProtos.EnumDescriptorProto arg);
51 }
52
53 public static class TestImpl implements TestProtocol {
54 public long getProtocolVersion(String protocol, long clientVersion) {
55 return TestProtocol.VERSION;
56 }
57
58 public ProtocolSignature getProtocolSignature(String protocol, long clientVersion,
59 int hashcode) {
60 return new ProtocolSignature(TestProtocol.VERSION, null);
61 }
62
63 @Override
64 public String echo(String value) throws IOException { return value; }
65
66 @Override
67 public Writable echo(Writable writable) {
68 return writable;
69 }
70
71 @Override
72 public EnumDescriptorProto exchangeProto(EnumDescriptorProto arg) {
73 return arg;
74 }
75 }
76
77 @Test(timeout=60000)
78 public void testCalls() throws Exception {
79 testCallsInternal(conf);
80 }
81
82 private void testCallsInternal(Configuration conf) throws Exception {
83 RpcServer rpcServer = HBaseRPC.getServer(new TestImpl(),
84 new Class<?>[] {TestProtocol.class},
85 "localhost",
86 0,
87 2,
88 0,
89 conf.getBoolean("hbase.rpc.verbose", false), conf,
90 0);
91 RpcEngine rpcEngine = null;
92 try {
93 rpcServer.start();
94 rpcEngine = HBaseRPC.getProtocolEngine(conf);
95
96 InetSocketAddress isa = rpcServer.getListenerAddress();
97 TestProtocol proxy = HBaseRPC.waitForProxy(rpcEngine,
98 TestProtocol.class, TestProtocol.VERSION,
99 isa, conf, -1, 8000, 8000);
100
101 String stringResult = proxy.echo("foo");
102 assertEquals(stringResult, "foo");
103
104 stringResult = proxy.echo((String)null);
105 assertEquals(stringResult, null);
106
107 Text utf8Result = (Text)proxy.echo(new Text("hello world"));
108 assertEquals(utf8Result, new Text("hello world"));
109
110 utf8Result = (Text)proxy.echo((Text)null);
111 assertEquals(utf8Result, null);
112
113
114 EnumDescriptorProto sendProto =
115 EnumDescriptorProto.newBuilder().setName("test").build();
116 EnumDescriptorProto retProto = proxy.exchangeProto(sendProto);
117 assertEquals(sendProto, retProto);
118 assertNotSame(sendProto, retProto);
119 } finally {
120 rpcServer.stop();
121 if (rpcEngine != null) {
122 rpcEngine.close();
123 }
124 }
125 }
126
127 public static void main(String[] args) throws Exception {
128 new TestPBOnWritableRpc().testCallsInternal(conf);
129 }
130 }