001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.ipc;
019
020import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE;
021import static org.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.newBlockingStub;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.fail;
024
025import java.io.IOException;
026import java.net.InetSocketAddress;
027import java.util.Arrays;
028import java.util.Collection;
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseConfiguration;
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.hadoop.hbase.logging.Log4jUtils;
034import org.apache.hadoop.hbase.testclassification.RPCTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.junit.After;
037import org.junit.Before;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.junit.runner.RunWith;
042import org.junit.runners.Parameterized;
043import org.junit.runners.Parameterized.Parameter;
044import org.junit.runners.Parameterized.Parameters;
045
046import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
047
048import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos;
049import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos.EchoRequestProto;
050import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos.EchoResponseProto;
051import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface;
052
053/**
054 * Test for testing protocol buffer based RPC mechanism. This test depends on test.proto definition
055 * of types in <code>src/test/protobuf/test.proto</code> and protobuf service definition from
056 * <code>src/test/protobuf/test_rpc_service.proto</code>
057 */
058@RunWith(Parameterized.class)
059@Category({ RPCTests.class, SmallTests.class })
060public class TestProtoBufRpc {
061
062  @ClassRule
063  public static final HBaseClassTestRule CLASS_RULE =
064    HBaseClassTestRule.forClass(TestProtoBufRpc.class);
065
066  public final static String ADDRESS = "localhost";
067  private static int PORT = 0;
068  private InetSocketAddress isa;
069  private Configuration conf;
070  private RpcServerInterface server;
071
072  @Parameters(name = "{index}: rpcServerImpl={0}")
073  public static Collection<Object[]> parameters() {
074    return Arrays.asList(new Object[] { SimpleRpcServer.class.getName() },
075      new Object[] { NettyRpcServer.class.getName() });
076  }
077
078  @Parameter(0)
079  public String rpcServerImpl;
080
081  @Before
082  public void setUp() throws IOException { // Setup server for both protocols
083    this.conf = HBaseConfiguration.create();
084    this.conf.set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY, rpcServerImpl);
085    Log4jUtils.setLogLevel("org.apache.hadoop.ipc.HBaseServer", "ERROR");
086    Log4jUtils.setLogLevel("org.apache.hadoop.ipc.HBaseServer.trace", "TRACE");
087    // Create server side implementation
088    // Get RPC server for server side implementation
089    this.server = RpcServerFactory.createRpcServer(null, "testrpc",
090      Lists.newArrayList(new RpcServer.BlockingServiceAndInterface(SERVICE, null)),
091      new InetSocketAddress(ADDRESS, PORT), conf, new FifoRpcScheduler(conf, 10));
092    InetSocketAddress address = server.getListenerAddress();
093    if (address == null) {
094      throw new IOException("Listener channel is closed");
095    }
096    this.isa = address;
097    this.server.start();
098  }
099
100  @After
101  public void tearDown() throws Exception {
102    server.stop();
103  }
104
105  @Test(expected = org.apache.hbase.thirdparty.com.google.protobuf.ServiceException.class
106  /* Thrown when we call stub.error */)
107  public void testProtoBufRpc() throws Exception {
108    RpcClient rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
109    try {
110      BlockingInterface stub = newBlockingStub(rpcClient, this.isa);
111      // Test ping method
112      TestProtos.EmptyRequestProto emptyRequest = TestProtos.EmptyRequestProto.newBuilder().build();
113      stub.ping(null, emptyRequest);
114
115      // Test echo method
116      EchoRequestProto echoRequest = EchoRequestProto.newBuilder().setMessage("hello").build();
117      EchoResponseProto echoResponse = stub.echo(null, echoRequest);
118      assertEquals("hello", echoResponse.getMessage());
119
120      stub.error(null, emptyRequest);
121      fail("Expected exception is not thrown");
122    } finally {
123      rpcClient.close();
124    }
125  }
126}