This is an automated email from the ASF dual-hosted git repository.

dkuzmenko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/master by this push:
     new de40069420b HIVE-28194: Regenerate HS2 thrift port in 
TestRetryingThriftCLIServiceClient (Araika Singh, reviewed by Denys Kuzmenko)
de40069420b is described below

commit de40069420b0dac729fbf45746654f2085c4a0d3
Author: NZEC <[email protected]>
AuthorDate: Wed Nov 13 14:26:44 2024 +0530

    HIVE-28194: Regenerate HS2 thrift port in 
TestRetryingThriftCLIServiceClient (Araika Singh, reviewed by Denys Kuzmenko)
    
    Closes #5205
---
 .../TestThriftCLIServiceWithAllAndBinary.java      |  12 +--
 .../thrift/TestThriftCLIServiceWithAllAndHttp.java |  10 +-
 .../cli/thrift/TestThriftCLIServiceWithBinary.java |  14 +--
 .../cli/thrift/TestThriftCLIServiceWithHttp.java   |  15 +--
 .../thrift/TestThriftCliServiceMessageSize.java    |  47 ++-------
 .../thrift/TestThriftHttpCLIServiceFeatures.java   |  44 +++------
 .../cli/TestRetryingThriftCLIServiceClient.java    |  54 ++++------
 .../service/cli/thrift/AbstractThriftCLITest.java  | 110 +++++++++++++++++++++
 .../service/cli/thrift/ThriftCLIServiceTest.java   |  58 +----------
 .../hive/service/server/TestHS2HttpServer.java     |  44 ++-------
 10 files changed, 164 insertions(+), 244 deletions(-)

diff --git 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithAllAndBinary.java
 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithAllAndBinary.java
index 66445ceb943..660b686bb1b 100644
--- 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithAllAndBinary.java
+++ 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithAllAndBinary.java
@@ -22,7 +22,6 @@ import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hive.service.Service;
 import org.apache.hive.service.auth.HiveAuthConstants;
 
-import org.junit.AfterClass;
 import org.junit.BeforeClass;
 
 import static org.junit.Assert.assertNotNull;
@@ -41,9 +40,7 @@ public class TestThriftCLIServiceWithAllAndBinary extends 
ThriftCLIServiceTest {
    */
   @BeforeClass
   public static void setUpBeforeClass() throws Exception {
-    // Set up the base class
-    ThriftCLIServiceTest.setUpBeforeClass();
-
+    initConf(TestThriftCLIServiceWithAllAndBinary.class);
     assertNotNull(port);
     assertNotNull(hiveServer2);
     assertNotNull(hiveConf);
@@ -59,13 +56,6 @@ public class TestThriftCLIServiceWithAllAndBinary extends 
ThriftCLIServiceTest {
     client = getHttpServiceClientInternal();
   }
 
-  /**
-   * @throws Exception
-   */
-  @AfterClass
-  public static void tearDownAfterClass() throws Exception {
-    ThriftCLIServiceTest.tearDownAfterClass();
-  }
   static ThriftCLIServiceClient getHttpServiceClientInternal() {
     for (Service service : hiveServer2.getServices()) {
       if (service instanceof ThriftBinaryCLIService) {
diff --git 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithAllAndHttp.java
 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithAllAndHttp.java
index 715be745fb4..5ad7f659e34 100644
--- 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithAllAndHttp.java
+++ 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithAllAndHttp.java
@@ -22,7 +22,6 @@ import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hive.service.Service;
 import org.apache.hive.service.auth.HiveAuthConstants;
 
-import org.junit.AfterClass;
 import org.junit.BeforeClass;
 
 import static org.junit.Assert.assertNotNull;
@@ -43,7 +42,7 @@ public class TestThriftCLIServiceWithAllAndHttp extends 
ThriftCLIServiceTest {
   @BeforeClass
   public static void setUpBeforeClass() throws Exception {
     // Set up the base class
-    ThriftCLIServiceTest.setUpBeforeClass();
+    initConf(TestThriftCLIServiceWithAllAndHttp.class);
 
     assertNotNull(port);
     assertNotNull(hiveServer2);
@@ -61,13 +60,6 @@ public class TestThriftCLIServiceWithAllAndHttp extends 
ThriftCLIServiceTest {
     client = getHttpServiceClientInternal();
   }
 
-  /**
-   * @throws Exception
-   */
-  @AfterClass
-  public static void tearDownAfterClass() throws Exception {
-    ThriftCLIServiceTest.tearDownAfterClass();
-  }
   static ThriftCLIServiceClient getHttpServiceClientInternal() {
     for (Service service : hiveServer2.getServices()) {
       if (service instanceof ThriftBinaryCLIService) {
diff --git 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithBinary.java
 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithBinary.java
index ee24b673bdd..3cfb3044680 100644
--- 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithBinary.java
+++ 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithBinary.java
@@ -23,9 +23,6 @@ import static org.junit.Assert.assertNotNull;
 import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
 import org.apache.hive.service.auth.HiveAuthConstants;
 import org.apache.hive.service.auth.HiveAuthConstants.AuthTypes;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
 import org.junit.BeforeClass;
 
 
@@ -46,7 +43,7 @@ public class TestThriftCLIServiceWithBinary extends 
ThriftCLIServiceTest {
   @BeforeClass
   public static void setUpBeforeClass() throws Exception {
     // Set up the base class
-    ThriftCLIServiceTest.setUpBeforeClass();
+    initConf(TestThriftCLIServiceWithBinary.class);
 
     assertNotNull(port);
     assertNotNull(hiveServer2);
@@ -62,13 +59,4 @@ public class TestThriftCLIServiceWithBinary extends 
ThriftCLIServiceTest {
 
     client = getServiceClientInternal();
   }
-
-  /**
-   * @throws java.lang.Exception
-   */
-  @AfterClass
-  public static void tearDownAfterClass() throws Exception {
-    ThriftCLIServiceTest.tearDownAfterClass();
-  }
-
 }
\ No newline at end of file
diff --git 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithHttp.java
 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithHttp.java
index dd337af9594..f255bd8e925 100644
--- 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithHttp.java
+++ 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCLIServiceWithHttp.java
@@ -47,9 +47,6 @@ import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.protocol.TProtocol;
 import org.apache.thrift.transport.THttpClient;
 import org.apache.thrift.transport.TTransport;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
@@ -71,7 +68,7 @@ public class TestThriftCLIServiceWithHttp extends 
ThriftCLIServiceTest {
   @BeforeClass
   public static void setUpBeforeClass() throws Exception {
     // Set up the base class
-    ThriftCLIServiceTest.setUpBeforeClass();
+    initConf(TestThriftCLIServiceWithHttp.class);
 
     assertNotNull(port);
     assertNotNull(hiveServer2);
@@ -88,14 +85,4 @@ public class TestThriftCLIServiceWithHttp extends 
ThriftCLIServiceTest {
 
     client = getServiceClientInternal();
   }
-
-  /**
-   * @throws java.lang.Exception
-   */
-  @AfterClass
-  public static void tearDownAfterClass() throws Exception {
-    ThriftCLIServiceTest.tearDownAfterClass();
-  }
-
-  
 }
diff --git 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCliServiceMessageSize.java
 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCliServiceMessageSize.java
index d59e4f1655f..e0458b0a4f1 100644
--- 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCliServiceMessageSize.java
+++ 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftCliServiceMessageSize.java
@@ -26,7 +26,6 @@ import java.sql.Statement;
 
 import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
-import org.apache.hadoop.hive.metastore.MetaStoreTestUtils;
 import org.apache.hive.service.auth.HiveAuthConstants;
 import org.apache.hive.service.server.HiveServer2;
 import org.junit.After;
@@ -35,24 +34,14 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-public class TestThriftCliServiceMessageSize {
-  protected static int port;
-  protected static String host = "localhost";
-  protected static HiveServer2 hiveServer2;
-  protected static ThriftCLIServiceClient client;
-  protected static HiveConf hiveConf;
-  protected static String USERNAME = "anonymous";
-  protected static String PASSWORD = "anonymous";
+public class TestThriftCliServiceMessageSize extends AbstractThriftCLITest {
 
   /**
    * @throws java.lang.Exception
    */
   @BeforeClass
   public static void setUpBeforeClass() throws Exception {
-    // Find a free port
-    port = MetaStoreTestUtils.findFreePort();
-    hiveServer2 = new HiveServer2();
-    hiveConf = new HiveConf();
+    initConf(TestThriftCliServiceMessageSize.class);
   }
 
   /**
@@ -62,28 +51,6 @@ public class TestThriftCliServiceMessageSize {
   public static void tearDownAfterClass() throws Exception {
   }
 
-  protected static void startHiveServer2WithConf(HiveServer2 hiveServer2, 
HiveConf hiveConf)
-      throws Exception {
-    hiveServer2.init(hiveConf);
-    // Start HiveServer2 with given config
-    // Fail if server doesn't start
-    try {
-      hiveServer2.start();
-    } catch (Throwable t) {
-      t.printStackTrace();
-      fail();
-    }
-    // Wait for startup to complete
-    Thread.sleep(2000);
-    System.out.println("HiveServer2 started on port " + port);
-  }
-
-  protected static void stopHiveServer2(HiveServer2 hiveServer2) throws 
Exception {
-    if (hiveServer2 != null) {
-      hiveServer2.stop();
-    }
-  }
-
   /**
    * @throws java.lang.Exception
    */
@@ -109,14 +76,14 @@ public class TestThriftCliServiceMessageSize {
     hiveConf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION, 
HiveAuthConstants.AuthTypes.NONE.toString());
     hiveConf.setVar(ConfVars.HIVE_SERVER2_TRANSPORT_MODE, transportMode);
 
-    HiveServer2 hiveServer2 = new HiveServer2();
+    hiveServer2 = new HiveServer2();
     String url = "jdbc:hive2://localhost:" + port + "/default";
     Class.forName("org.apache.hive.jdbc.HiveDriver");
 
     try {
       // First start HS2 with high message size limit. This should allow 
connections
       hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE, 
100*1024*1024);
-      startHiveServer2WithConf(hiveServer2, hiveConf);
+      startHiveServer2WithConf(hiveConf);
 
       System.out.println("Started Thrift CLI service with message size limit "
           + hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE));
@@ -127,12 +94,12 @@ public class TestThriftCliServiceMessageSize {
       assertNotNull("Statement is null", stmt);
       stmt.execute("set hive.support.concurrency = false");
       connection.close();
-      stopHiveServer2(hiveServer2);
+      stopHiveServer2();
 
       // Now start HS2 with low message size limit. This should prevent any 
connections
       hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE, 1);
       hiveServer2 = new HiveServer2();
-      startHiveServer2WithConf(hiveServer2, hiveConf);
+      startHiveServer2WithConf(hiveConf);
       System.out.println("Started Thrift CLI service with message size limit "
           + hiveConf.getIntVar(ConfVars.HIVE_SERVER2_THRIFT_MAX_MESSAGE_SIZE));
 
@@ -146,7 +113,7 @@ public class TestThriftCliServiceMessageSize {
       // Verify we hit an error while connecting
       assertNotNull(caughtException);
     } finally {
-      stopHiveServer2(hiveServer2);
+      stopHiveServer2();
       hiveServer2 = null;
     }
   }
diff --git 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftHttpCLIServiceFeatures.java
 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftHttpCLIServiceFeatures.java
index f04db3635f3..a3ca23404cd 100644
--- 
a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftHttpCLIServiceFeatures.java
+++ 
b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftHttpCLIServiceFeatures.java
@@ -62,7 +62,6 @@ import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.protocol.TProtocol;
 import org.apache.thrift.transport.THttpClient;
 import org.apache.thrift.transport.TTransport;
-import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -78,7 +77,7 @@ import com.google.common.base.Joiner;
  * classes instead of jdbc.
  */
 
-public class TestThriftHttpCLIServiceFeatures  {
+public class TestThriftHttpCLIServiceFeatures extends AbstractThriftCLITest {
 
   private static String transportMode = "http";
   private static String thriftHttpPath = "cliservice";
@@ -137,17 +136,15 @@ public class TestThriftHttpCLIServiceFeatures  {
    */
   @BeforeClass
   public static void setUpBeforeClass() throws Exception {
-    // Set up the base class
-    ThriftCLIServiceTest.setUpBeforeClass();
+    initConf(TestThriftHttpCLIServiceFeatures.class);
 
-    assertNotNull(ThriftCLIServiceTest.port);
-    assertNotNull(ThriftCLIServiceTest.hiveServer2);
-    assertNotNull(ThriftCLIServiceTest.hiveConf);
-    HiveConf hiveConf = ThriftCLIServiceTest.hiveConf;
+    assertNotNull(port);
+    assertNotNull(hiveServer2);
+    assertNotNull(hiveConf);
 
     hiveConf.setBoolVar(ConfVars.HIVE_SERVER2_ENABLE_DOAS, false);
-    hiveConf.setVar(ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, 
ThriftCLIServiceTest.host);
-    hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_PORT, 
ThriftCLIServiceTest.port);
+    hiveConf.setVar(ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, host);
+    hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_PORT, port);
     hiveConf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION, 
HiveAuthConstants.AuthTypes.NOSASL.toString());
     hiveConf.setVar(ConfVars.HIVE_SERVER2_TRANSPORT_MODE, transportMode);
     hiveConf.setVar(ConfVars.HIVE_SERVER2_THRIFT_HTTP_PATH, thriftHttpPath);
@@ -157,20 +154,11 @@ public class TestThriftHttpCLIServiceFeatures  {
     hiveConf.setVar(ConfVars.HIVE_AUTHENTICATOR_MANAGER, 
SessionStateUserAuthenticator.class.getName());
     hiveConf.setBoolVar(ConfVars.HIVE_AUTHORIZATION_ENABLED, true);
 
-    ThriftCLIServiceTest.startHiveServer2WithConf(hiveConf);
+    startHiveServer2WithConf(hiveConf);
 
-    ThriftCLIServiceTest.client = 
ThriftCLIServiceTest.getServiceClientInternal();
+    client = ThriftCLIServiceTest.getServiceClientInternal();
   }
 
-  /**
-   * @throws java.lang.Exception
-   */
-  @AfterClass
-  public static void tearDownAfterClass() throws Exception {
-    ThriftCLIServiceTest.tearDownAfterClass();
-  }
-
-
   @Test
   /**
    * Tests calls from a raw (NOSASL) binary client,
@@ -225,21 +213,21 @@ public class TestThriftHttpCLIServiceFeatures  {
   }
 
   private TTransport getRawBinaryTransport() throws Exception {
-    return HiveAuthUtils.getSocketTransport(ThriftCLIServiceTest.host, 
ThriftCLIServiceTest.port, 0);
+    return HiveAuthUtils.getSocketTransport(host, port, 0);
   }
 
   private static TTransport getHttpTransport() throws Exception {
     DefaultHttpClient httpClient = new DefaultHttpClient();
     String httpUrl = getHttpUrl();
     httpClient.addRequestInterceptor(
-        new HttpBasicAuthInterceptor(ThriftCLIServiceTest.USERNAME, 
ThriftCLIServiceTest.PASSWORD,
+        new HttpBasicAuthInterceptor(USERNAME, PASSWORD,
             null, null, false, null, null));
     return new THttpClient(httpUrl, httpClient);
   }
 
   private static String getHttpUrl() {
-    return transportMode + "://" + ThriftCLIServiceTest.host + ":"
-        + ThriftCLIServiceTest.port +
+    return transportMode + "://" + host + ":"
+        + port +
         "/" + thriftHttpPath + "/";
   }
 
@@ -292,7 +280,7 @@ public class TestThriftHttpCLIServiceFeatures  {
     DefaultHttpClient hClient = new DefaultHttpClient();
     String httpUrl = getHttpUrl();
     HttpBasicAuthInterceptorWithLogging authInt =
-      new HttpBasicAuthInterceptorWithLogging(ThriftCLIServiceTest.USERNAME, 
ThriftCLIServiceTest.PASSWORD, null, null,
+      new HttpBasicAuthInterceptorWithLogging(USERNAME, PASSWORD, null, null,
       false, additionalHeaders, cookieHeaders);
     hClient.addRequestInterceptor(authInt);
     transport = new THttpClient(httpUrl, hClient);
@@ -342,8 +330,8 @@ public class TestThriftHttpCLIServiceFeatures  {
     }
 
     // interceptor for adding username, pwd
-    HttpBasicAuthInterceptor authInt = new 
HttpBasicAuthInterceptor(ThriftCLIServiceTest.USERNAME,
-        ThriftCLIServiceTest.PASSWORD, null, null,
+    HttpBasicAuthInterceptor authInt = new HttpBasicAuthInterceptor(USERNAME,
+            PASSWORD, null, null,
         false, null, null);
     hClient.addRequestInterceptor(authInt);
 
diff --git 
a/service/src/test/org/apache/hive/service/cli/TestRetryingThriftCLIServiceClient.java
 
b/service/src/test/org/apache/hive/service/cli/TestRetryingThriftCLIServiceClient.java
index 364124c7fdb..7037d48f90d 100644
--- 
a/service/src/test/org/apache/hive/service/cli/TestRetryingThriftCLIServiceClient.java
+++ 
b/service/src/test/org/apache/hive/service/cli/TestRetryingThriftCLIServiceClient.java
@@ -23,14 +23,14 @@ import org.apache.hadoop.hive.conf.HiveServer2TransportMode;
 import org.apache.hive.service.Service;
 import org.apache.hive.service.auth.HiveAuthConstants;
 import org.apache.hive.service.cli.session.HiveSession;
+import org.apache.hive.service.cli.thrift.AbstractThriftCLITest;
 import org.apache.hive.service.cli.thrift.RetryingThriftCLIServiceClient;
 import org.apache.hive.service.cli.thrift.ThriftCLIService;
-import org.apache.hive.service.server.HiveServer2;
 import org.apache.thrift.TException;
 import org.apache.thrift.transport.TTransport;
 import org.apache.thrift.transport.TTransportException;
 
-import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 
 import java.lang.reflect.Method;
@@ -44,14 +44,12 @@ import static org.junit.Assert.*;
  * Test CLI service with a retrying client. All tests should pass. This is to 
validate that calls
  * are transferred successfully.
  */
-public class TestRetryingThriftCLIServiceClient {
+public class TestRetryingThriftCLIServiceClient extends AbstractThriftCLITest {
   protected static ThriftCLIService service;
-  private HiveConf hiveConf;
-  private HiveServer2 server;
 
-  @Before
-  public void init() {
-    hiveConf = new HiveConf();
+  @BeforeClass
+  public static void init() throws Exception {
+    initConf(TestRetryingThriftCLIServiceClient.class);
     hiveConf.setVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, 
"localhost");
     hiveConf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT, 15000);
     hiveConf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS, false);
@@ -66,22 +64,6 @@ public class TestRetryingThriftCLIServiceClient {
         
"org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory");
   }
 
-  private void startHiveServer() throws InterruptedException {
-    // Start hive server2
-    server = new HiveServer2();
-    server.init(hiveConf);
-    server.start();
-    Thread.sleep(5000);
-    System.out.println("## HiveServer started");
-  }
-
-  private void stopHiveServer() {
-    if (server != null) {
-      // kill server
-      server.stop();
-    }
-  }
-
   static class RetryingThriftCLIServiceClientTest extends 
RetryingThriftCLIServiceClient {
     int callCount = 0;
     int connectCount = 0;
@@ -117,7 +99,9 @@ public class TestRetryingThriftCLIServiceClient {
 
   @Test
   public void testRetryBehaviour() throws Exception {
-    startHiveServer();
+    startHiveServer2WithConf(hiveConf);
+    // Get the port number HS2 thrift port is started on
+    int thriftPort = 
hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT);
     // Check if giving invalid address causes retry in connection attempt
     hiveConf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT, 17000);
     try {
@@ -128,7 +112,7 @@ public class TestRetryingThriftCLIServiceClient {
       assertTrue(sqlExc.getMessage().contains("3"));
     }
     // Reset port setting
-    hiveConf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT, 15000);
+    hiveConf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT, thriftPort);
 
     hiveConf.setVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, 
"10.17.207.11");
     try {
@@ -146,7 +130,7 @@ public class TestRetryingThriftCLIServiceClient {
       = 
RetryingThriftCLIServiceClientTest.newRetryingCLIServiceClient(hiveConf);
     System.out.println("## Created client");
 
-    stopHiveServer();
+    stopHiveServer2();
     Thread.sleep(5000);
 
     // submit few queries
@@ -166,10 +150,10 @@ public class TestRetryingThriftCLIServiceClient {
   }
 
   @Test
-  public void testTransportClose() throws InterruptedException, 
HiveSQLException {
+  public void testTransportClose() throws Exception {
     
hiveConf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_CLIENT_CONNECTION_RETRY_LIMIT,
 0);
     try {
-      startHiveServer();
+      startHiveServer2WithConf(hiveConf);
       RetryingThriftCLIServiceClient.CLIServiceClientWrapper client
         = 
RetryingThriftCLIServiceClientTest.newRetryingCLIServiceClient(hiveConf);
       client.closeTransport();
@@ -181,22 +165,22 @@ public class TestRetryingThriftCLIServiceClient {
       }
     } finally {
       
hiveConf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_CLIENT_CONNECTION_RETRY_LIMIT,
 3);
-      stopHiveServer();
+      stopHiveServer2();
     }
   }
 
   @Test
-  public void testSessionLifeAfterTransportClose() throws 
InterruptedException, HiveSQLException {
+  public void testSessionLifeAfterTransportClose() throws Exception {
     try {
-      startHiveServer();
+      startHiveServer2WithConf(hiveConf);
       CLIService service = null;
-      for (Service s : server.getServices()) {
+      for (Service s : hiveServer2.getServices()) {
         if (s instanceof CLIService) {
           service = (CLIService) s;
         }
       }
       if (service == null) {
-        service = new CLIService(server, true);
+        service = new CLIService(hiveServer2, true);
       }
       RetryingThriftCLIServiceClient.CLIServiceClientWrapper client
         = 
RetryingThriftCLIServiceClientTest.newRetryingCLIServiceClient(hiveConf);
@@ -226,7 +210,7 @@ public class TestRetryingThriftCLIServiceClient {
         }
       }
     } finally {
-      stopHiveServer();
+      stopHiveServer2();
     }
   }
 }
diff --git 
a/service/src/test/org/apache/hive/service/cli/thrift/AbstractThriftCLITest.java
 
b/service/src/test/org/apache/hive/service/cli/thrift/AbstractThriftCLITest.java
new file mode 100644
index 00000000000..65e0e678b9f
--- /dev/null
+++ 
b/service/src/test/org/apache/hive/service/cli/thrift/AbstractThriftCLITest.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hive.service.cli.thrift;
+
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConfForTest;
+import org.apache.hive.service.server.HiveServer2;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
+import org.junit.AfterClass;
+
+public abstract class AbstractThriftCLITest {
+    protected static int port;
+    protected static HiveServer2 hiveServer2;
+    protected static HiveConf hiveConf;
+    protected static String host = "localhost";
+    protected static ThriftCLIServiceClient client;
+    protected static String USERNAME = "anonymous";
+    protected static String PASSWORD = "anonymous";
+    protected static final int RETRY_COUNT = 10;
+
+    /**
+     * @throws java.lang.Exception
+     */
+    public static void initConf(Class<?> cls) throws Exception {
+        // Find a free port
+        port = findFreePort();
+        hiveServer2 = new HiveServer2();
+        hiveConf = new HiveConfForTest(cls);
+    }
+
+    /**
+     * @throws java.lang.Exception
+     */
+    @AfterClass
+    public static void tearDownAfterClass() throws Exception {
+        stopHiveServer2();
+    }
+
+    public static void startHiveServer2WithConf(HiveConf hiveConf) throws 
Exception {
+        Exception hs2Exception = null;
+        boolean hs2Started = false;
+        for(int tryCount = 0; (tryCount < RETRY_COUNT); tryCount++){
+            try {
+                hiveServer2 = new HiveServer2();
+                hiveServer2.init(hiveConf);
+                hiveServer2.start();
+                Thread.sleep(5000);
+                hs2Started = true;
+                break;
+            } catch (Exception e) {
+                hs2Exception = e;
+                hiveConf.setIntVar(hiveConf,
+                        HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT, 
findFreePort());
+                hiveConf.setIntVar(hiveConf,
+                        HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PORT, 
findFreePort());
+                hiveConf.setIntVar(hiveConf,
+                        HiveConf.ConfVars.HIVE_SERVER2_WEBUI_PORT, 
findFreePortExcepting(
+                        
Integer.valueOf(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_PORT.getDefaultValue())));
+            }
+        }
+        if (!hs2Started) {
+            throw(hs2Exception);
+        }
+        System.out.println("HiveServer2 started on port " + port);
+    }
+
+    public static void stopHiveServer2() throws Exception {
+        if (hiveServer2 != null) {
+            hiveServer2.stop();
+        }
+    }
+
+    public static int findFreePort() throws IOException {
+        ServerSocket socket= new ServerSocket(0);
+        int port = socket.getLocalPort();
+        socket.close();
+        return port;
+    }
+
+    public static int findFreePortExcepting(int portToExclude) throws 
IOException {
+        try (ServerSocket socket1 = new ServerSocket(0); ServerSocket socket2 
= new ServerSocket(0)) {
+            if (socket1.getLocalPort() != portToExclude) {
+                return socket1.getLocalPort();
+            }
+            // If we're here, then socket1.getLocalPort was the port to exclude
+            // Since both sockets were open together at a point in time, we're
+            // guaranteed that socket2.getLocalPort() is not the same.
+            return socket2.getLocalPort();
+        }
+    }
+
+}
diff --git 
a/service/src/test/org/apache/hive/service/cli/thrift/ThriftCLIServiceTest.java 
b/service/src/test/org/apache/hive/service/cli/thrift/ThriftCLIServiceTest.java
index 407bcc72bde..3c84b70703a 100644
--- 
a/service/src/test/org/apache/hive/service/cli/thrift/ThriftCLIServiceTest.java
+++ 
b/service/src/test/org/apache/hive/service/cli/thrift/ThriftCLIServiceTest.java
@@ -19,24 +19,17 @@ package org.apache.hive.service.cli.thrift;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.fail;
 
 import java.util.HashMap;
 import java.util.Map;
 
-import org.apache.hadoop.hive.conf.HiveConf;
-import org.apache.hadoop.hive.conf.HiveConfForTest;
-import org.apache.hadoop.hive.metastore.MetaStoreTestUtils;
 import org.apache.hive.service.Service;
 import org.apache.hive.service.cli.OperationHandle;
 import org.apache.hive.service.cli.OperationState;
 import org.apache.hive.service.cli.OperationStatus;
 import org.apache.hive.service.cli.SessionHandle;
-import org.apache.hive.service.server.HiveServer2;
 import org.junit.After;
-import org.junit.AfterClass;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
 /**
@@ -45,56 +38,7 @@ import org.junit.Test;
  * Subclass this to test more specific behaviour.
  *
  */
-public abstract class ThriftCLIServiceTest {
-
-  protected static int port;
-  protected static String host = "localhost";
-  protected static HiveServer2 hiveServer2;
-  protected static ThriftCLIServiceClient client;
-  protected static HiveConf hiveConf;
-  protected static String USERNAME = "anonymous";
-  protected static String PASSWORD = "anonymous";
-
-  /**
-   * @throws java.lang.Exception
-   */
-  @BeforeClass
-  public static void setUpBeforeClass() throws Exception {
-    // Find a free port
-    port = MetaStoreTestUtils.findFreePort();
-    hiveServer2 = new HiveServer2();
-    hiveConf = new HiveConfForTest(ThriftCLIServiceTest.class);
-  }
-
-  /**
-   * @throws java.lang.Exception
-   */
-  @AfterClass
-  public static void tearDownAfterClass() throws Exception {
-    stopHiveServer2();
-  }
-
-   static void startHiveServer2WithConf(HiveConf hiveConf) throws Exception {
-    hiveServer2.init(hiveConf);
-    // Start HiveServer2 with given config
-    // Fail if server doesn't start
-    try {
-      hiveServer2.start();
-    } catch (Throwable t) {
-      t.printStackTrace();
-      fail();
-    }
-    // Wait for startup to complete
-    Thread.sleep(2000);
-    System.out.println("HiveServer2 started on port " + port);
-  }
-
-  protected static void stopHiveServer2() throws Exception {
-    if (hiveServer2 != null) {
-      hiveServer2.stop();
-    }
-  }
-
+public abstract class ThriftCLIServiceTest extends AbstractThriftCLITest {
   static ThriftCLIServiceClient getServiceClientInternal() {
     for (Service service : hiveServer2.getServices()) {
       if (service instanceof ThriftBinaryCLIService) {
diff --git 
a/service/src/test/org/apache/hive/service/server/TestHS2HttpServer.java 
b/service/src/test/org/apache/hive/service/server/TestHS2HttpServer.java
index a1e30d3bb5e..5d4e91e32ac 100644
--- a/service/src/test/org/apache/hive/service/server/TestHS2HttpServer.java
+++ b/service/src/test/org/apache/hive/service/server/TestHS2HttpServer.java
@@ -24,11 +24,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.commons.io.IOUtils;
 import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
-import org.apache.hadoop.hive.metastore.MetaStoreTestUtils;
 import org.apache.hive.service.cli.CLIService;
 import org.apache.hive.service.cli.OperationHandle;
 import org.apache.hive.service.cli.SessionHandle;
 import org.apache.hive.service.cli.session.SessionManager;
+import org.apache.hive.service.cli.thrift.AbstractThriftCLITest;
 import org.apache.hive.service.rpc.thrift.TProtocolVersion;
 import org.apache.http.HttpEntity;
 import org.apache.http.client.methods.CloseableHttpResponse;
@@ -36,7 +36,6 @@ import org.apache.http.client.methods.HttpGet;
 import org.apache.http.impl.client.CloseableHttpClient;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
-import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -59,12 +58,10 @@ import static org.junit.Assert.assertNull;
 /**
  * TestHS2HttpServer -- executes tests of HiveServer2 HTTP Server.
  */
-public class TestHS2HttpServer {
+public class TestHS2HttpServer extends AbstractThriftCLITest {
 
-  private static HiveServer2 hiveServer2 = null;
   private static CLIService client = null;
   private static SessionManager sm = null;
-  private static HiveConf hiveConf = null;
   private static String metastorePasswd = "61ecbc41cdae3e6b32712a06c73606fa"; 
//random md5
   private static Integer webUIPort = null;
   private static String apiBaseURL = null;
@@ -72,39 +69,18 @@ public class TestHS2HttpServer {
 
   @BeforeClass
   public static void beforeTests() throws Exception {
-    webUIPort = MetaStoreTestUtils.findFreePortExcepting(
+    initConf(TestHS2HttpServer.class);
+    webUIPort = findFreePortExcepting(
         Integer.valueOf(ConfVars.HIVE_SERVER2_WEBUI_PORT.getDefaultValue()));
     apiBaseURL = "http://localhost:"; + webUIPort + "/api/v1";
-    hiveConf = new HiveConf();
     hiveConf.set(ConfVars.METASTORE_PWD.varname, metastorePasswd);
     hiveConf.set(ConfVars.HIVE_SERVER2_WEBUI_PORT.varname, 
webUIPort.toString());
     hiveConf.setVar(HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER,
         
"org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory");
 
-    Exception hs2Exception = null;
-    boolean hs2Started = false;
-    for (int tryCount = 0; (tryCount < MetaStoreTestUtils.RETRY_COUNT); 
tryCount++) {
-      try {
-        hiveServer2 = new HiveServer2();
-        hiveServer2.init(hiveConf);
-        hiveServer2.start();
-        client = hiveServer2.getCliService();
-        Thread.sleep(5000);
-        hs2Started = true;
-        break;
-      } catch (Exception t) {
-        HiveConf.setIntVar(hiveConf, 
HiveConf.ConfVars.HIVE_SERVER2_THRIFT_PORT,
-            MetaStoreTestUtils.findFreePort());
-        HiveConf.setIntVar(hiveConf, 
HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_PORT,
-            MetaStoreTestUtils.findFreePort());
-        HiveConf.setIntVar(hiveConf, HiveConf.ConfVars.HIVE_SERVER2_WEBUI_PORT,
-            MetaStoreTestUtils.findFreePort());
-        webUIPort = 
hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_PORT);
-      }
-    }
-    if (!hs2Started) {
-      throw (hs2Exception);
-    }
+    startHiveServer2WithConf(hiveConf);
+    client = hiveServer2.getCliService();
+    webUIPort = hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_WEBUI_PORT);
     sm = hiveServer2.getCliService().getSessionManager();
   }
 
@@ -324,10 +300,4 @@ public class TestHS2HttpServer {
     IOUtils.copy(conn.getInputStream(), writer, "UTF-8");
     return writer.toString();
   }
-
-
-  @AfterClass
-  public static void afterTests() throws Exception {
-    hiveServer2.stop();
-  }
 }


Reply via email to