1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  /** Unit tests to test PB-based types on WritableRpcEngine. */
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", // BindAddress is IP we got for this server.
86          0, // port number
87          2, // number of handlers
88          0, // we dont use high priority handlers in master
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       // Test protobufs
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 }