continuum-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From batkin...@apache.org
Subject svn commit: r1656158 - in /continuum/trunk/continuum-api: ./ src/main/java/org/apache/continuum/utils/shell/ src/test/java/org/apache/continuum/utils/shell/
Date Sat, 31 Jan 2015 03:34:03 GMT
Author: batkinson
Date: Sat Jan 31 03:34:03 2015
New Revision: 1656158

URL: http://svn.apache.org/r1656158
Log:
Added a test for shell helper isRunning method.

Added:
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/LogOutputConsumer.java
      - copied, changed from r1656157, continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
    continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/
    continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/DefaultShellCommandHelperTest.java
    continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/Sleep.java
Modified:
    continuum/trunk/continuum-api/pom.xml
    continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java

Modified: continuum/trunk/continuum-api/pom.xml
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/pom.xml?rev=1656158&r1=1656157&r2=1656158&view=diff
==============================================================================
--- continuum/trunk/continuum-api/pom.xml (original)
+++ continuum/trunk/continuum-api/pom.xml Sat Jan 31 03:34:03 2015
@@ -69,8 +69,25 @@
       <artifactId>slf4j-api</artifactId>
     </dependency>
     <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
     </dependency>
   </dependencies>
+    <build>
+        <plugins>
+            <plugin>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <configuration>
+                    <systemProperties>
+                        <sleepClasspath>${project.build.testOutputDirectory}</sleepClasspath>
+                    </systemProperties>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 </project>

Modified: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java?rev=1656158&r1=1656157&r2=1656158&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
(original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
Sat Jan 31 03:34:03 2015
@@ -25,7 +25,7 @@ import java.io.IOException;
 import java.io.PrintWriter;
 
 /**
- * Collects output to a file using a buffered writer. Unlike the list-based consumer, this
should be safer to use when
+ * Collects output to a file using a buffered writer. Unlike the list-based consumer, this
should be safe to use when
  * the output size is expected to be large.
  */
 public class FileOutputConsumer

Copied: continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/LogOutputConsumer.java
(from r1656157, continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java)
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/LogOutputConsumer.java?p2=continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/LogOutputConsumer.java&p1=continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java&r1=1656157&r2=1656158&rev=1656158&view=diff
==============================================================================
--- continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/FileOutputConsumer.java
(original)
+++ continuum/trunk/continuum-api/src/main/java/org/apache/continuum/utils/shell/LogOutputConsumer.java
Sat Jan 31 03:34:03 2015
@@ -19,47 +19,25 @@ package org.apache.continuum.utils.shell
  * under the License.
  */
 
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.PrintWriter;
+import org.slf4j.Logger;
 
 /**
- * Collects output to a file using a buffered writer. Unlike the list-based consumer, this
should be safer to use when
- * the output size is expected to be large.
+ * Collects output to a logger. Unlike the list-based consumer, this should be safe to use
when the output size is
+ * expected to be large. It logs the output at level INFO.
  */
-public class FileOutputConsumer
+public class LogOutputConsumer
     implements OutputConsumer
 {
-    private PrintWriter writer;
+    private Logger log;
 
-    /**
-     * Creates a output consumer for the given file.
-     *
-     * @param outputFile the file to write the results to
-     * @throws IOException if there is a problem creating a file
-     */
-    public FileOutputConsumer( File outputFile )
-        throws IOException
+    public LogOutputConsumer( Logger log )
     {
-        this.writer = new PrintWriter( new FileWriter( outputFile ) );
+        this.log = log;
     }
 
     public void consume( String line )
     {
-        if ( writer != null )
-            writer.println( line );
+        if ( log != null )
+            log.info( line );
     }
-
-    public void flush()
-    {
-        if ( writer != null )
-            writer.flush();
-    }
-
-    public void close()
-    {
-        if ( writer != null )
-            writer.close();
-    }
-}
\ No newline at end of file
+}

Added: continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/DefaultShellCommandHelperTest.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/DefaultShellCommandHelperTest.java?rev=1656158&view=auto
==============================================================================
--- continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/DefaultShellCommandHelperTest.java
(added)
+++ continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/DefaultShellCommandHelperTest.java
Sat Jan 31 03:34:03 2015
@@ -0,0 +1,105 @@
+package org.apache.continuum.utils.shell;
+
+/*
+ * 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.
+ */
+
+import junit.framework.TestCase;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @see org.apache.continuum.utils.shell.DefaultShellCommandHelper
+ */
+public class DefaultShellCommandHelperTest
+    extends TestCase
+{
+    private static final Logger log = LoggerFactory.getLogger( DefaultShellCommandHelper.class
);
+
+    DefaultShellCommandHelper helper;
+
+    public void setUp()
+    {
+        helper = new DefaultShellCommandHelper();
+    }
+
+    public void tearDown()
+    {
+        helper = null;
+    }
+
+    /**
+     * To concurrently check the run status of a process.
+     */
+    private static class RunChecker
+        implements Runnable
+    {
+        ShellCommandHelper shellHelper;
+
+        long pid;
+
+        boolean wasRunning;
+
+        long sleepMillis;
+
+        public RunChecker( ShellCommandHelper shellHelper, long pid, long sleepMillis )
+        {
+            this.shellHelper = shellHelper;
+            this.pid = pid;
+            this.sleepMillis = sleepMillis;
+        }
+
+        public void run()
+        {
+            try
+            {
+                Thread.sleep( sleepMillis );
+                this.wasRunning = shellHelper.isRunning( pid );
+            }
+            catch ( InterruptedException e )
+            {
+                log.error( "run checker interrupted", e );
+            }
+        }
+    }
+
+    public void testIsRunning()
+        throws Exception
+    {
+        long virtualPid = 1, sleepMillis = 100;
+        RunChecker checker = new RunChecker( helper, virtualPid, sleepMillis );
+        String[] cmdArgs = { "-cp", System.getProperty( "sleepClasspath" ), Sleep.class.getCanonicalName(),
"1" };
+
+        // Verify process isn't running initially
+        checker.run();
+        assertFalse( "Expected that command was not running", checker.wasRunning );
+
+        // Verify running status is true when running
+        Thread checkerThread = new Thread( checker );
+        checkerThread.start();
+
+        helper.executeShellCommand( null, "java", cmdArgs, new LogOutputConsumer( log ),
virtualPid, null );
+        checkerThread.join();
+        assertTrue( "Expected that command was running", checker.wasRunning );
+
+        // Verify process isn't running after
+        checker.run();
+        assertFalse( "Expected that command was not running", checker.wasRunning );
+    }
+
+}

Added: continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/Sleep.java
URL: http://svn.apache.org/viewvc/continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/Sleep.java?rev=1656158&view=auto
==============================================================================
--- continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/Sleep.java
(added)
+++ continuum/trunk/continuum-api/src/test/java/org/apache/continuum/utils/shell/Sleep.java
Sat Jan 31 03:34:03 2015
@@ -0,0 +1,39 @@
+package org.apache.continuum.utils.shell;
+
+/*
+ * 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.
+ */
+
+/**
+ * A portable program that sleeps. Useful for cross platform shell tests.
+ */
+public class Sleep
+{
+
+    public static void main( String[] args )
+        throws InterruptedException
+    {
+        if ( args.length != 1 )
+        {
+            System.err.printf( "usage: java %s <time-to-sleep-in-seconds>\n", Sleep.class.getName()
);
+            System.exit( 1 );
+        }
+        int sleepTimeInMillis = Integer.valueOf( args[0] ) * 1000;
+        Thread.sleep( sleepTimeInMillis );
+    }
+}



Mime
View raw message