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 java.io.IOException;
22  import java.net.InetSocketAddress;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.SmallTests;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /** Unit test for Protocol extending common interface. */
32  @Category(SmallTests.class)
33  public class TestProtocolExtension {
34    private static final String ADDRESS = "0.0.0.0";
35  
36    public static final Log LOG =
37      LogFactory.getLog(TestProtocolExtension.class);
38    
39    private static Configuration conf = new Configuration();
40  
41    public interface ProtocolExtention {
42      void logClassName();
43    }
44  
45    public interface TestProtocol extends VersionedProtocol, ProtocolExtention {
46      public static final long VERSION = 7L;
47  
48      void ping() throws IOException;
49  
50      // @Override  // Uncomment to make the test pass
51      // public void logClassName();
52  }
53  
54    public static class TestImpl implements TestProtocol {
55      public long getProtocolVersion(String protocol, long clientVersion) {
56        return TestProtocol.VERSION;
57      }
58  
59      @Override
60      public void ping() {}
61  
62      @Override
63      public void logClassName() {
64        LOG.info(this.getClass().getName());
65      }
66  
67      @Override
68      public ProtocolSignature getProtocolSignature(String protocol,
69          long clientVersion, int clientMethodsHash) throws IOException {
70        return new ProtocolSignature(VERSION, null);
71      }
72    }
73  
74    @Test
75    public void testCalls() throws Exception {
76      RpcServer server = HBaseRPC.getServer(TestProtocol.class,
77                                    new TestImpl(),
78                                    new Class<?>[]{ProtocolExtention.class},
79                                    ADDRESS,
80                                    6016,
81                                    10, 10, false,
82                                    conf, 10);
83      RpcEngine rpcEngine = null;
84      try {
85        server.start();
86        rpcEngine = HBaseRPC.getProtocolEngine(conf);
87  
88        InetSocketAddress addr = server.getListenerAddress();
89        TestProtocol proxy = rpcEngine.getProxy(
90            TestProtocol.class, TestProtocol.VERSION, addr, conf, 10000);
91  
92        proxy.ping();
93  
94        proxy.logClassName();
95      } finally {
96        server.stop();
97        if (rpcEngine != null) {
98          rpcEngine.close();
99        }
100     }
101   }
102   
103   @org.junit.Rule
104   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
105     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
106 }