Repository: lens Updated Branches: refs/heads/master 265c4667a -> 42c4dfcb6
LENS-991 : Remove shutdown hook getting added for each command Project: http://git-wip-us.apache.org/repos/asf/lens/repo Commit: http://git-wip-us.apache.org/repos/asf/lens/commit/42c4dfcb Tree: http://git-wip-us.apache.org/repos/asf/lens/tree/42c4dfcb Diff: http://git-wip-us.apache.org/repos/asf/lens/diff/42c4dfcb Branch: refs/heads/master Commit: 42c4dfcb64f59c341ad244b96872a921ea9043db Parents: 265c466 Author: Raju Bairishetti <r...@apache.org> Authored: Tue Jul 5 11:13:49 2016 +0530 Committer: Amareshwari Sriramadasu <amareshw...@apache.org> Committed: Tue Jul 5 11:13:49 2016 +0530 ---------------------------------------------------------------------- .../lens/cli/commands/BaseLensCommand.java | 12 +- .../lens/cli/TestLensConnectionCliCommands.java | 146 +++++++++---------- .../apache/lens/cli/TestLensCubeCommands.java | 6 +- .../lens/cli/TestLensDatabaseCommands.java | 5 +- .../lens/cli/TestLensNativeTableCommands.java | 4 +- .../apache/lens/cli/TestLensQueryCommands.java | 5 - .../org/apache/lens/client/TestLensClient.java | 56 +++---- 7 files changed, 107 insertions(+), 127 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lens/blob/42c4dfcb/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java ---------------------------------------------------------------------- diff --git a/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java b/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java index 9eb73b3..479746a 100644 --- a/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java +++ b/lens-cli/src/main/java/org/apache/lens/cli/commands/BaseLensCommand.java @@ -57,8 +57,7 @@ public class BaseLensCommand implements ExecutionProcessor { protected DefaultPrettyPrinter pp; /** The is connection active. */ - protected boolean isConnectionActive; - + protected static boolean isConnectionActive; public static final String DATE_FMT = "yyyy-MM-dd'T'HH:mm:ss:SSS"; private LensClient lensClient = null; @@ -75,7 +74,7 @@ public class BaseLensCommand implements ExecutionProcessor { return DATE_PARSER.get().format(dt); } - private void registerShutDownHook() { + static { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { closeClientConnection(); @@ -86,10 +85,10 @@ public class BaseLensCommand implements ExecutionProcessor { /** * Close client connection. */ - protected synchronized void closeClientConnection() { + protected static synchronized void closeClientConnection() { if (isConnectionActive) { log.debug("Request for stopping lens cli received"); - lensClient.closeConnection(); + getClientWrapper().getClient().closeConnection(); isConnectionActive = false; } } @@ -98,7 +97,6 @@ public class BaseLensCommand implements ExecutionProcessor { * Instantiates a new base lens command. */ public BaseLensCommand() { - registerShutDownHook(); mapper = new ObjectMapper(); mapper.setSerializationInclusion(Inclusion.NON_NULL); mapper.setSerializationInclusion(Inclusion.NON_DEFAULT); @@ -120,13 +118,13 @@ public class BaseLensCommand implements ExecutionProcessor { } public void setClient(LensClient client) { - isConnectionActive = true; lensClient = client; } public LensClient getClient() { if (lensClient == null) { setClient(getClientWrapper().getClient()); + isConnectionActive = true; } return lensClient; } http://git-wip-us.apache.org/repos/asf/lens/blob/42c4dfcb/lens-cli/src/test/java/org/apache/lens/cli/TestLensConnectionCliCommands.java ---------------------------------------------------------------------- diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensConnectionCliCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensConnectionCliCommands.java index d1ec94e..76d387d 100644 --- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensConnectionCliCommands.java +++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensConnectionCliCommands.java @@ -63,19 +63,19 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { */ @Test public void testConnectionCommand() { - LensClient client = new LensClient(); - LensConnectionCommands commands = new LensConnectionCommands(); - commands.setClient(client); - String key = "connectiontest1"; - String value = "connectiontest1val"; - String keyvalList = commands.showParameters(); - - Assert.assertFalse(keyvalList.contains("connectiontest1")); - - commands.setParam(key + "=" + value); - String val = commands.getParam(key); - Assert.assertEquals(val, key + "=" + value); - commands.quitShell(); + try (LensClient client = new LensClient()) { + LensConnectionCommands commands = new LensConnectionCommands(); + commands.setClient(client); + String key = "connectiontest1"; + String value = "connectiontest1val"; + String keyvalList = commands.showParameters(); + + Assert.assertFalse(keyvalList.contains("connectiontest1")); + + commands.setParam(key + "=" + value); + String val = commands.getParam(key); + Assert.assertEquals(val, key + "=" + value); + } } private File createNewPath(String fileName) { @@ -133,28 +133,28 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { */ @Test public void testFileCommands() { - LensClient client = new LensClient(); - LensConnectionCommands commands = new LensConnectionCommands(); - commands.setClient(client); - LOG.debug("Testing set/remove file operations"); + try (LensClient client = new LensClient()) { + LensConnectionCommands commands = new LensConnectionCommands(); + commands.setClient(client); + LOG.debug("Testing set/remove file operations"); - File f = null; - try { - String filename = "target/data"; - f = createNewPath(filename); + File f = null; + try { + String filename = "target/data"; + f = createNewPath(filename); - String result = commands.addFile(filename); - Assert.assertEquals("Add resource succeeded", result); + String result = commands.addFile(filename); + Assert.assertEquals("Add resource succeeded", result); - result = commands.removeFile(filename); - Assert.assertEquals("Delete resource succeeded", result); + result = commands.removeFile(filename); + Assert.assertEquals("Delete resource succeeded", result); - LOG.debug("Testing set/remove file operation done"); - } finally { - if (f != null) { - f.delete(); + LOG.debug("Testing set/remove file operation done"); + } finally { + if (f != null) { + f.delete(); + } } - commands.quitShell(); } } @@ -163,50 +163,50 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { */ @Test public void testFileCommandsWithURIRegex() { - LensClient client = new LensClient(); - LensConnectionCommands commands = new LensConnectionCommands(); - commands.setClient(client); - LOG.debug("Testing set/remove file operations"); + try (LensClient client = new LensClient()) { + LensConnectionCommands commands = new LensConnectionCommands(); + commands.setClient(client); + LOG.debug("Testing set/remove file operations"); - java.io.File file = new java.io.File(""); - String projectdir = file.getAbsolutePath(); + java.io.File file = new java.io.File(""); + String projectdir = file.getAbsolutePath(); /* Tests input file pattern file: and file:// */ - String filenameA = "file:" + projectdir + "/target/tempdata_a.txt"; - String filenameB = "file://" + projectdir + "/target/tempdata_b.txt"; + String filenameA = "file:" + projectdir + "/target/tempdata_a.txt"; + String filenameB = "file://" + projectdir + "/target/tempdata_b.txt"; - String fileRegex = "file:" + projectdir + "/target/tempdata_*.txt"; + String fileRegex = "file:" + projectdir + "/target/tempdata_*.txt"; - try { - createNewFile(filenameA); - createNewFile(filenameB); + try { + createNewFile(filenameA); + createNewFile(filenameB); - String result = commands.addFile(fileRegex); - Assert.assertEquals("Add resource succeeded", result); + String result = commands.addFile(fileRegex); + Assert.assertEquals("Add resource succeeded", result); - result = commands.removeFile(fileRegex); - Assert.assertEquals("Delete resource succeeded", result); + result = commands.removeFile(fileRegex); + Assert.assertEquals("Delete resource succeeded", result); - LOG.debug("Testing set/remove file operation done"); - } finally { - deleteFile(filenameA); - deleteFile(filenameB); - commands.quitShell(); + LOG.debug("Testing set/remove file operation done"); + } finally { + deleteFile(filenameA); + deleteFile(filenameB); + } } } + /** * Test jar commands. */ @Test public void testJarCommands() { - LensClient client = new LensClient(); + File jar = null; LensConnectionCommands commands = new LensConnectionCommands(); - commands.setClient(client); - LOG.debug("Testing set/remove file operations"); + try (LensClient client = new LensClient()) { + commands.setClient(client); + LOG.debug("Testing set/remove file operations"); - File jar = null; - try { String filename = "target/data.jar"; jar = createNewPath(filename); @@ -220,7 +220,6 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { if (jar != null) { jar.delete(); } - commands.quitShell(); } } @@ -229,14 +228,14 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { */ @Test public void testResourceCommandsWithRegex() { - LensClient client = new LensClient(); - LensConnectionCommands commands = new LensConnectionCommands(); - commands.setClient(client); - LOG.debug("Testing set/remove file operations"); - File fileA = null, fileB = null; String filenameA, filenameB, fileRegex, result; - try { + LensConnectionCommands commands = new LensConnectionCommands(); + + try (LensClient client = new LensClient()) { + commands.setClient(client); + LOG.debug("Testing set/remove file operations"); + filenameA = "target/tempdata_a"; filenameB = "target/tempdata_b"; fileRegex = "target/tempdata_*"; @@ -270,7 +269,6 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { if (fileB != null) { fileB.delete(); } - commands.quitShell(); } } @@ -279,14 +277,14 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { */ @Test public void testListResourcesCommands() { - LensClient client = new LensClient(); - LensConnectionCommands commands = new LensConnectionCommands(); - commands.setClient(client); - LOG.debug("Testing set/remove file operations"); - File file = null; File jar = null; - try { + + LensConnectionCommands commands = new LensConnectionCommands(); + try (LensClient client = new LensClient()) { + commands.setClient(client); + LOG.debug("Testing set/remove file operations"); + String fileName = "target/data.txt"; file = createNewPath(fileName); commands.addFile(fileName); @@ -324,7 +322,6 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { if (jar != null) { jar.delete(); } - commands.quitShell(); } } @@ -333,16 +330,13 @@ public class TestLensConnectionCliCommands extends LensCliApplicationTest { */ @Test public void testGetSessionHandle() { - LensClient client = new LensClient(); LensConnectionCommands commands = new LensConnectionCommands(); - commands.setClient(client); - try { + try (LensClient client = new LensClient()) { + commands.setClient(client); LensSessionHandle sessionHandle = client.getConnection().getSessionHandle(); Assert.assertNotNull(sessionHandle); String output = commands.getSessionHandle(); Assert.assertTrue(output.contains(sessionHandle.getPublicId().toString()), "session handle output: " + output); - } finally { - commands.quitShell(); } } } http://git-wip-us.apache.org/repos/asf/lens/blob/42c4dfcb/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java ---------------------------------------------------------------------- diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java index f53ed6b..c20766c 100644 --- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java +++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensCubeCommands.java @@ -51,8 +51,7 @@ public class TestLensCubeCommands extends LensCliApplicationTest { */ @Test public void testCubeCommands() throws Exception { - LensClient client = new LensClient(); - try { + try(LensClient client = new LensClient()) { LensDimensionCommands dimensionCommand = new LensDimensionCommands(); dimensionCommand.setClient(client); dimensionCommand.createDimension(new File( @@ -83,8 +82,6 @@ public class TestLensCubeCommands extends LensCliApplicationTest { assertFalse(cubeList.contains("sample_cube")); dimensionCommand.dropDimension("test_detail"); dimensionCommand.dropDimension("test_dim"); - } finally { - client.closeConnection(); } } @@ -176,7 +173,6 @@ public class TestLensCubeCommands extends LensCliApplicationTest { writer.close(); String desc = command.describeCube("sample_cube"); - LensClient client = command.getClient(); LOG.debug(desc); String propString = "sample_cube.prop: sample"; String propString1 = "sample_cube.prop1: sample1"; http://git-wip-us.apache.org/repos/asf/lens/blob/42c4dfcb/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java ---------------------------------------------------------------------- diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java index dfa02ec..41672ac 100644 --- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java +++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensDatabaseCommands.java @@ -47,8 +47,7 @@ public class TestLensDatabaseCommands extends LensCliApplicationTest { */ @Test public void testDatabaseCommands() throws URISyntaxException { - LensClient client = new LensClient(); - try { + try (LensClient client = new LensClient()) { LensDatabaseCommands command = new LensDatabaseCommands(); LensCubeCommands cubeCommand = new LensCubeCommands(); command.setClient(client); @@ -57,8 +56,6 @@ public class TestLensDatabaseCommands extends LensCliApplicationTest { for (int i = 0; i < 4; i++, cascade = !cascade) { testDrop(command, cubeCommand, cascade); } - } finally { - client.closeConnection(); } } http://git-wip-us.apache.org/repos/asf/lens/blob/42c4dfcb/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java ---------------------------------------------------------------------- diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java index 6ea6fe4..3f6e866 100644 --- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java +++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensNativeTableCommands.java @@ -48,8 +48,7 @@ public class TestLensNativeTableCommands extends LensCliApplicationTest { */ @Test public void testNativeTableCommands() throws Exception { - LensClient client = new LensClient(); - try { + try (LensClient client = new LensClient()) { SessionState.start(new HiveConf()); LensNativeTableCommands command = new LensNativeTableCommands(); command.setClient(client); @@ -68,7 +67,6 @@ public class TestLensNativeTableCommands extends LensCliApplicationTest { Assert.assertTrue(desc.contains("test.hive.table.prop")); } finally { LensServerTestUtil.dropHiveTable("test_native_table_command"); - client.closeConnection(); SessionState.detachSession(); } } http://git-wip-us.apache.org/repos/asf/lens/blob/42c4dfcb/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java ---------------------------------------------------------------------- diff --git a/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java b/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java index 9274bf1..0bf2a66 100644 --- a/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java +++ b/lens-cli/src/test/java/org/apache/lens/cli/TestLensQueryCommands.java @@ -397,7 +397,6 @@ public class TestLensQueryCommands extends LensCliApplicationTest { } System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"); - closeClientConnection(qCom); } private String readFile(String path) throws FileNotFoundException { @@ -449,9 +448,6 @@ public class TestLensQueryCommands extends LensCliApplicationTest { LensDimensionTableCommands dimTableCmd = new LensDimensionTableCommands(); dimTableCmd.setClient(client); dimTableCmd.dropDimensionTable("dim_table", true); - - // close client connection - client.closeConnection(); } /** @@ -503,7 +499,6 @@ public class TestLensQueryCommands extends LensCliApplicationTest { String qh = qCom.executeQuery(sql, true, "testQuery4"); String result = qCom.getQueryResults(qh, null, false); assertTrue(result.contains("1\tfirst"), result); - closeClientConnection(qCom); } /** http://git-wip-us.apache.org/repos/asf/lens/blob/42c4dfcb/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java ---------------------------------------------------------------------- diff --git a/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java b/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java index ea464d5..c3f01d5 100644 --- a/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java +++ b/lens-client/src/test/java/org/apache/lens/client/TestLensClient.java @@ -143,16 +143,17 @@ public class TestLensClient extends LensAllApplicationJerseyTest { lensClientConfig.setLensDatabase(TEST_DB); Assert.assertEquals(lensClientConfig.getLensDatabase(), TEST_DB); - LensClient client = new LensClient(lensClientConfig); - Assert.assertEquals(client.getCurrentDatabae(), TEST_DB, - "current database"); + try (LensClient client = new LensClient(lensClientConfig)) { + Assert.assertEquals(client.getCurrentDatabae(), TEST_DB, + "current database"); - client.createDatabase("testclientdb", true); - Assert.assertTrue(client.getAllDatabases().contains("testclientdb")); - client.dropDatabase("testclientdb", false); - Assert.assertFalse(client.getAllDatabases().contains("testclientdb")); + client.createDatabase("testclientdb", true); + Assert.assertTrue(client.getAllDatabases().contains("testclientdb")); + client.dropDatabase("testclientdb", false); + Assert.assertFalse(client.getAllDatabases().contains("testclientdb")); - Assert.assertTrue(RequestTestFilter.isAccessed(), "RequestTestFilter not invoked"); + Assert.assertTrue(RequestTestFilter.isAccessed(), "RequestTestFilter not invoked"); + } } @DataProvider(name = "testIterableHttpResultSetDP") @@ -275,27 +276,28 @@ public class TestLensClient extends LensAllApplicationJerseyTest { @Test public void testWaitForQueryToCompleteWithAndWithoutRetryOnTimeOut() throws LensAPIException { LensClientConfig config = createLensClientConfigWithServerUrl(); - config.setInt(LensClientConfig.READ_TIMEOUT_MILLIS, 3000); - LensClient lensClient = new LensClient(config); - assertTrue(lensClient.setDatabase(TEST_DB)); - lensClient.setConnectionParam(ENABLE_SLEEP_FOR_GET_QUERY_OP, "true"); - - //Test waitForQueryToComplete without retry on timeout - QueryHandle handle = lensClient.executeQueryAsynch("cube select id,name from test_dim", "test3"); - try { - lensClient.getStatement().waitForQueryToComplete(handle, false); - fail("SocketTimeoutException was expected"); - } catch (Exception e) { - if (!isExceptionDueToSocketTimeout(e)) { - fail("SocketTimeoutException was excepted as part of Read Timeout"); + try (LensClient lensClient = new LensClient(config)) { + config.setInt(LensClientConfig.READ_TIMEOUT_MILLIS, 3000); + assertTrue(lensClient.setDatabase(TEST_DB)); + lensClient.setConnectionParam(ENABLE_SLEEP_FOR_GET_QUERY_OP, "true"); + + //Test waitForQueryToComplete without retry on timeout + QueryHandle handle = lensClient.executeQueryAsynch("cube select id,name from test_dim", "test3"); + try { + lensClient.getStatement().waitForQueryToComplete(handle, false); + fail("SocketTimeoutException was expected"); + } catch (Exception e) { + if (!isExceptionDueToSocketTimeout(e)) { + fail("SocketTimeoutException was excepted as part of Read Timeout"); + } } - } - //Test waitForQueryToComplete with Retry on timeout - handle = lensClient.executeQueryAsynch("cube select id,name from test_dim", "test3"); - lensClient.getStatement().waitForQueryToComplete(handle); - LensQuery query = lensClient.getQueryDetails(handle); - assertTrue(query.getStatus().successful()); + //Test waitForQueryToComplete with Retry on timeout + handle = lensClient.executeQueryAsynch("cube select id,name from test_dim", "test3"); + lensClient.getStatement().waitForQueryToComplete(handle); + LensQuery query = lensClient.getQueryDetails(handle); + assertTrue(query.getStatus().successful()); + } } private LensClientConfig createLensClientConfigWithServerUrl() {