HBASE-17532 Replaced explicit type with diamond operator
[hbase.git] / hbase-thrift / src / test / java / org / apache / hadoop / hbase / thrift / TestThriftServerCmdLine.java
blob87998dacc6821448acd50e1b61997c006c7db496
1 /*
2 * Copyright The Apache Software Foundation
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with this
6 * work for additional information regarding copyright ownership. The ASF
7 * licenses this file to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 * License for the specific language governing permissions and limitations
17 * under the License.
19 package org.apache.hadoop.hbase.thrift;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
24 import java.net.InetAddress;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.testclassification.ClientTests;
33 import org.apache.hadoop.hbase.testclassification.LargeTests;
34 import org.apache.hadoop.hbase.thrift.ThriftServerRunner.ImplType;
35 import org.apache.hadoop.hbase.thrift.generated.Hbase;
36 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
37 import org.apache.hadoop.hbase.util.EnvironmentEdgeManagerTestHelper;
38 import org.apache.hadoop.hbase.util.IncrementingEnvironmentEdge;
39 import org.apache.thrift.protocol.TBinaryProtocol;
40 import org.apache.thrift.protocol.TCompactProtocol;
41 import org.apache.thrift.protocol.TProtocol;
42 import org.apache.thrift.server.TServer;
43 import org.apache.thrift.transport.TFramedTransport;
44 import org.apache.thrift.transport.TSocket;
45 import org.apache.thrift.transport.TTransport;
46 import org.junit.AfterClass;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.junit.experimental.categories.Category;
50 import org.junit.runner.RunWith;
51 import org.junit.runners.Parameterized;
52 import org.junit.runners.Parameterized.Parameters;
54 import com.google.common.base.Joiner;
56 /**
57 * Start the HBase Thrift server on a random port through the command-line
58 * interface and talk to it from client side.
60 @Category({ClientTests.class, LargeTests.class})
61 @RunWith(Parameterized.class)
62 public class TestThriftServerCmdLine {
64 private static final Log LOG =
65 LogFactory.getLog(TestThriftServerCmdLine.class);
67 private final ImplType implType;
68 private boolean specifyFramed;
69 private boolean specifyBindIP;
70 private boolean specifyCompact;
72 private static final HBaseTestingUtility TEST_UTIL =
73 new HBaseTestingUtility();
75 private Thread cmdLineThread;
76 private volatile Exception cmdLineException;
78 private Exception clientSideException;
80 private ThriftServer thriftServer;
81 private int port;
83 @Parameters
84 public static Collection<Object[]> getParameters() {
85 Collection<Object[]> parameters = new ArrayList<>();
86 for (ImplType implType : ImplType.values()) {
87 for (boolean specifyFramed : new boolean[] {false, true}) {
88 for (boolean specifyBindIP : new boolean[] {false, true}) {
89 if (specifyBindIP && !implType.canSpecifyBindIP) {
90 continue;
92 for (boolean specifyCompact : new boolean[] {false, true}) {
93 parameters.add(new Object[]{implType, specifyFramed,
94 specifyBindIP, specifyCompact});
99 return parameters;
102 public TestThriftServerCmdLine(ImplType implType, boolean specifyFramed,
103 boolean specifyBindIP, boolean specifyCompact) {
104 this.implType = implType;
105 this.specifyFramed = specifyFramed;
106 this.specifyBindIP = specifyBindIP;
107 this.specifyCompact = specifyCompact;
108 LOG.debug(getParametersString());
111 private String getParametersString() {
112 return "implType=" + implType + ", " +
113 "specifyFramed=" + specifyFramed + ", " +
114 "specifyBindIP=" + specifyBindIP + ", " +
115 "specifyCompact=" + specifyCompact;
118 @BeforeClass
119 public static void setUpBeforeClass() throws Exception {
120 TEST_UTIL.getConfiguration().setBoolean("hbase.table.sanity.checks", false);
121 TEST_UTIL.startMiniCluster();
122 //ensure that server time increments every time we do an operation, otherwise
123 //successive puts having the same timestamp will override each other
124 EnvironmentEdgeManagerTestHelper.injectEdge(new IncrementingEnvironmentEdge());
127 @AfterClass
128 public static void tearDownAfterClass() throws Exception {
129 TEST_UTIL.shutdownMiniCluster();
130 EnvironmentEdgeManager.reset();
133 private void startCmdLineThread(final String[] args) {
134 LOG.info("Starting HBase Thrift server with command line: " + Joiner.on(" ").join(args));
136 cmdLineException = null;
137 cmdLineThread = new Thread(new Runnable() {
138 @Override
139 public void run() {
140 try {
141 thriftServer.doMain(args);
142 } catch (Exception e) {
143 cmdLineException = e;
147 cmdLineThread.setName(ThriftServer.class.getSimpleName() +
148 "-cmdline");
149 cmdLineThread.start();
152 @Test(timeout=600000)
153 public void testRunThriftServer() throws Exception {
154 List<String> args = new ArrayList<>();
155 if (implType != null) {
156 String serverTypeOption = implType.toString();
157 assertTrue(serverTypeOption.startsWith("-"));
158 args.add(serverTypeOption);
160 port = HBaseTestingUtility.randomFreePort();
161 args.add("-" + ThriftServer.PORT_OPTION);
162 args.add(String.valueOf(port));
163 args.add("-infoport");
164 int infoPort = HBaseTestingUtility.randomFreePort();
165 args.add(String.valueOf(infoPort));
167 if (specifyFramed) {
168 args.add("-" + ThriftServer.FRAMED_OPTION);
170 if (specifyBindIP) {
171 args.add("-" + ThriftServer.BIND_OPTION);
172 args.add(InetAddress.getLocalHost().getHostName());
174 if (specifyCompact) {
175 args.add("-" + ThriftServer.COMPACT_OPTION);
177 args.add("start");
179 thriftServer = new ThriftServer(TEST_UTIL.getConfiguration());
180 startCmdLineThread(args.toArray(new String[args.size()]));
182 // wait up to 10s for the server to start
183 for (int i = 0; i < 100
184 && (thriftServer.serverRunner == null || thriftServer.serverRunner.tserver == null); i++) {
185 Thread.sleep(100);
188 Class<? extends TServer> expectedClass = implType != null ?
189 implType.serverClass : TBoundedThreadPoolServer.class;
190 assertEquals(expectedClass,
191 thriftServer.serverRunner.tserver.getClass());
193 try {
194 talkToThriftServer();
195 } catch (Exception ex) {
196 clientSideException = ex;
197 } finally {
198 stopCmdLineThread();
201 if (clientSideException != null) {
202 LOG.error("Thrift client threw an exception. Parameters:" +
203 getParametersString(), clientSideException);
204 throw new Exception(clientSideException);
208 private static volatile boolean tableCreated = false;
210 private void talkToThriftServer() throws Exception {
211 TSocket sock = new TSocket(InetAddress.getLocalHost().getHostName(),
212 port);
213 TTransport transport = sock;
214 if (specifyFramed || implType.isAlwaysFramed) {
215 transport = new TFramedTransport(transport);
218 sock.open();
219 try {
220 TProtocol prot;
221 if (specifyCompact) {
222 prot = new TCompactProtocol(transport);
223 } else {
224 prot = new TBinaryProtocol(transport);
226 Hbase.Client client = new Hbase.Client(prot);
227 if (!tableCreated){
228 TestThriftServer.createTestTables(client);
229 tableCreated = true;
231 TestThriftServer.checkTableList(client);
233 } finally {
234 sock.close();
238 private void stopCmdLineThread() throws Exception {
239 LOG.debug("Stopping " + implType.simpleClassName() + " Thrift server");
240 thriftServer.stop();
241 cmdLineThread.join();
242 if (cmdLineException != null) {
243 LOG.error("Command-line invocation of HBase Thrift server threw an " +
244 "exception", cmdLineException);
245 throw new Exception(cmdLineException);