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.testclassification.MediumTests;
034import org.apache.hadoop.hbase.testclassification.RPCTests;
035import org.apache.log4j.Level;
036import org.apache.log4j.Logger;
037import org.junit.After;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.runner.RunWith;
043import org.junit.runners.Parameterized;
044import org.junit.runners.Parameterized.Parameter;
045import org.junit.runners.Parameterized.Parameters;
046
047import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
048
049import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos;
050import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos.EchoRequestProto;
051import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestProtos.EchoResponseProto;
052import org.apache.hadoop.hbase.shaded.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface;
053
054/**
055 * Test for testing protocol buffer based RPC mechanism. This test depends on test.proto definition
056 * of types in <code>src/test/protobuf/test.proto</code> and protobuf service definition from
057 * <code>src/test/protobuf/test_rpc_service.proto</code>
058 */
059@RunWith(Parameterized.class)
060@Category({ RPCTests.class, MediumTests.class })
061public class TestProtoBufRpc {
062
063  @ClassRule
064  public static final HBaseClassTestRule CLASS_RULE =
065      HBaseClassTestRule.forClass(TestProtoBufRpc.class);
066
067  public final static String ADDRESS = "localhost";
068  public static int PORT = 0;
069  private InetSocketAddress isa;
070  private Configuration conf;
071  private RpcServerInterface server;
072
073  @Parameters(name = "{index}: rpcServerImpl={0}")
074  public static Collection<Object[]> parameters() {
075    return Arrays.asList(new Object[] { SimpleRpcServer.class.getName() },
076        new Object[] { NettyRpcServer.class.getName() });
077  }
078
079  @Parameter(0)
080  public String rpcServerImpl;
081
082  @Before
083  public void setUp() throws IOException { // Setup server for both protocols
084    this.conf = HBaseConfiguration.create();
085    this.conf.set(RpcServerFactory.CUSTOM_RPC_SERVER_IMPL_CONF_KEY,
086        rpcServerImpl);
087    Logger log = Logger.getLogger("org.apache.hadoop.ipc.HBaseServer");
088    log.setLevel(Level.DEBUG);
089    log = Logger.getLogger("org.apache.hadoop.ipc.HBaseServer.trace");
090    log.setLevel(Level.TRACE);
091    // Create server side implementation
092    // Get RPC server for server side implementation
093    this.server = RpcServerFactory.createRpcServer(null, "testrpc",
094        Lists.newArrayList(new RpcServer.BlockingServiceAndInterface(SERVICE, null)),
095        new InetSocketAddress(ADDRESS, PORT), conf, new FifoRpcScheduler(conf, 10));
096    InetSocketAddress address = server.getListenerAddress();
097    if (address == null) {
098      throw new IOException("Listener channel is closed");
099    }
100    this.isa = address;
101    this.server.start();
102  }
103
104  @After
105  public void tearDown() throws Exception {
106    server.stop();
107  }
108
109  @Test (expected=org.apache.hbase.thirdparty.com.google.protobuf.ServiceException.class
110      /*Thrown when we call stub.error*/)
111  public void testProtoBufRpc() throws Exception {
112    RpcClient rpcClient = RpcClientFactory.createClient(conf, HConstants.CLUSTER_ID_DEFAULT);
113    try {
114      BlockingInterface stub = newBlockingStub(rpcClient, this.isa);
115      // Test ping method
116      TestProtos.EmptyRequestProto emptyRequest = TestProtos.EmptyRequestProto.newBuilder().build();
117      stub.ping(null, emptyRequest);
118
119      // Test echo method
120      EchoRequestProto echoRequest = EchoRequestProto.newBuilder().setMessage("hello").build();
121      EchoResponseProto echoResponse = stub.echo(null, echoRequest);
122      assertEquals("hello", echoResponse.getMessage());
123
124      stub.error(null, emptyRequest);
125      fail("Expected exception is not thrown");
126    } finally {
127      rpcClient.close();
128    }
129  }
130}