Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEndFunctionListener.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEndFunctionListener.java (revision 1411461) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEndFunctionListener.java (working copy) @@ -33,24 +33,10 @@ * {@link org.apache.hadoop.hive.metastore.MetaStoreEndFunctionListener} */ public class TestMetaStoreEndFunctionListener extends TestCase { - private static final String msPort = "20002"; private HiveConf hiveConf; private HiveMetaStoreClient msc; private Driver driver; - private static class RunMS implements Runnable { - - @Override - public void run() { - try { - HiveMetaStore.main(new String[]{msPort}); - } catch (Throwable e) { - e.printStackTrace(System.err); - assert false; - } - } - } - @Override protected void setUp() throws Exception { @@ -61,11 +47,12 @@ DummyPreListener.class.getName()); System.setProperty("hive.metastore.end.function.listeners", DummyEndFunctionListener.class.getName()); - Thread t = new Thread(new RunMS()); + String port = String.valueOf(MetastoreTestUtils.findFreePort()); + Thread t = new Thread(new MetastoreTestUtils.RunMS(port)); t.start(); Thread.sleep(40000); hiveConf = new HiveConf(this.getClass()); - hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + msPort); + hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + port); hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTRETRIES, 3); hiveConf.set(HiveConf.ConfVars.PREEXECHOOKS.varname, ""); hiveConf.set(HiveConf.ConfVars.POSTEXECHOOKS.varname, ""); Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java (revision 1411461) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java (working copy) @@ -27,7 +27,6 @@ import org.apache.hadoop.hive.cli.CliSessionState; import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hadoop.hive.metastore.api.Database; import org.apache.hadoop.hive.metastore.api.Partition; import org.apache.hadoop.hive.metastore.api.PartitionEventType; @@ -61,7 +60,6 @@ * {@link org.apache.hadoop.hive.metastore.MetaStorePreEventListener} */ public class TestMetaStoreEventListener extends TestCase { - private static final String msPort = "20001"; private HiveConf hiveConf; private HiveMetaStoreClient msc; private Driver driver; @@ -70,19 +68,6 @@ private static final String tblName = "tmptbl"; private static final String renamed = "tmptbl2"; - private static class RunMS implements Runnable { - - @Override - public void run() { - try { - HiveMetaStore.main(new String[]{msPort}); - } catch (Throwable e) { - e.printStackTrace(System.err); - assert false; - } - } - } - @Override protected void setUp() throws Exception { @@ -93,12 +78,14 @@ System.setProperty("hive.metastore.pre.event.listeners", DummyPreListener.class.getName()); - Thread t = new Thread(new RunMS()); + String port = String.valueOf(MetastoreTestUtils.findFreePort()); + + Thread t = new Thread(new MetastoreTestUtils.RunMS(port)); t.start(); Thread.sleep(40000); hiveConf = new HiveConf(this.getClass()); - hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + msPort); + hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + port); hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTRETRIES, 3); hiveConf.set(HiveConf.ConfVars.PREEXECHOOKS.varname, ""); hiveConf.set(HiveConf.ConfVars.POSTEXECHOOKS.varname, ""); Index: metastore/src/test/org/apache/hadoop/hive/metastore/MetastoreTestUtils.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/MetastoreTestUtils.java (revision 0) +++ metastore/src/test/org/apache/hadoop/hive/metastore/MetastoreTestUtils.java (revision 0) @@ -0,0 +1,62 @@ +/** + * 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.hadoop.hive.metastore; + +import java.io.IOException; +import java.net.ServerSocket; + +public class MetastoreTestUtils { + + /** + * + * RunMS. + * + * Runnable that can be used to start a remote metastore + */ + public static class RunMS implements Runnable { + String port; + + public RunMS(String port) { + this.port = port; + } + + @Override + public void run() { + try { + HiveMetaStore.main(new String[]{port}); + } catch (Throwable e) { + e.printStackTrace(System.err); + assert false; + } + } + } + + /** + * Finds a free port on the machine. + * + * @return + * @throws IOException + */ + public static int findFreePort() throws IOException { + ServerSocket socket= new ServerSocket(0); + int port = socket.getLocalPort(); + socket.close(); + return port; + } +} Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStoreIpAddress.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStoreIpAddress.java (revision 1411461) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStoreIpAddress.java (working copy) @@ -18,13 +18,9 @@ package org.apache.hadoop.hive.metastore; -import java.io.IOException; -import java.net.ServerSocket; - import junit.framework.TestCase; import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hadoop.hive.metastore.api.Database; import org.apache.hadoop.util.StringUtils; @@ -40,34 +36,11 @@ private static HiveConf hiveConf; private static HiveMetaStoreClient msc; - private String port; - - private static class RunMS implements Runnable { - String port; - - public RunMS(String port) { - this.port = port; - } - - @Override - public void run() { - try { - System.setProperty(ConfVars.METASTORE_EVENT_LISTENERS.varname, - IpAddressListener.class.getName()); - HiveMetaStore.main(new String[] { port }); - } catch (Throwable e) { - e.printStackTrace(System.err); - assert false; - } - } - - } - @Override protected void setUp() throws Exception { super.setUp(); hiveConf = new HiveConf(this.getClass()); - port = findFreePort(); + String port = String.valueOf(MetastoreTestUtils.findFreePort()); if (isServerStarted) { assertNotNull("Unable to connect to the MetaStore server", msc); @@ -75,7 +48,7 @@ } System.out.println("Starting MetaStore Server on port " + port); - Thread t = new Thread(new RunMS(port)); + Thread t = new Thread(new MetastoreTestUtils.RunMS(port)); t.start(); isServerStarted = true; @@ -83,7 +56,7 @@ // a better way of detecting if the metastore has started? Thread.sleep(5000); // This is default case with setugi off for both client and server - createClient(); + createClient(port); } public void testIpAddress() throws Exception { @@ -100,15 +73,8 @@ } } - protected void createClient() throws Exception { + protected void createClient(String port) throws Exception { hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + port); msc = new HiveMetaStoreClient(hiveConf); } - - private String findFreePort() throws IOException { - ServerSocket socket= new ServerSocket(0); - int port = socket.getLocalPort(); - socket.close(); - return String.valueOf(port); - } } Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java (revision 1411461) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java (working copy) @@ -23,7 +23,6 @@ public class TestRemoteHiveMetaStore extends TestHiveMetaStore { - protected static final String METASTORE_PORT = "29083"; private static boolean isServerStarted = false; public TestRemoteHiveMetaStore() { @@ -31,20 +30,6 @@ isThriftClient = true; } - private static class RunMS implements Runnable { - - @Override - public void run() { - try { - HiveMetaStore.main(new String[] { METASTORE_PORT }); - } catch (Throwable e) { - e.printStackTrace(System.err); - assert false; - } - } - - } - @Override protected void setUp() throws Exception { super.setUp(); @@ -54,8 +39,10 @@ return; } - System.out.println("Starting MetaStore Server on port " + METASTORE_PORT); - Thread t = new Thread(new RunMS()); + String port = String.valueOf(MetastoreTestUtils.findFreePort()); + + System.out.println("Starting MetaStore Server on port " + port); + Thread t = new Thread(new MetastoreTestUtils.RunMS(port)); t.start(); isServerStarted = true; @@ -63,11 +50,11 @@ // a better way of detecting if the metastore has started? Thread.sleep(5000); // This is default case with setugi off for both client and server - createClient(false); + createClient(false, port); } - protected void createClient(boolean setugi) throws Exception { - hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + METASTORE_PORT); + protected void createClient(boolean setugi, String port) throws Exception { + hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + port); hiveConf.setBoolVar(ConfVars.METASTORE_EXECUTE_SET_UGI,setugi); client = new HiveMetaStoreClient(hiveConf); }