View Javadoc

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  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   * Test for testing protocol buffer based RPC mechanism. This test depends on test.proto definition
49   * of types in <code>src/test/protobuf/test.proto</code> and protobuf service definition from
50   * <code>src/test/protobuf/test_rpc_service.proto</code>
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 { // Setup server for both protocols
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      // Create server side implementation
68      // Get RPC server for server side implementation
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    /* Thrown when we call stub.error */)
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        // Test ping method
92        TestProtos.EmptyRequestProto emptyRequest = TestProtos.EmptyRequestProto.newBuilder().build();
93        stub.ping(null, emptyRequest);
94  
95        // Test echo method
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 }