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