You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2016/12/28 23:12:36 UTC

svn commit: r1776356 - /jmeter/trunk/src/core/org/apache/jmeter/engine/StandardJMeterEngine.java

Author: pmouawad
Date: Wed Dec 28 23:12:36 2016
New Revision: 1776356

URL: http://svn.apache.org/viewvc?rev=1776356&view=rev
Log:
sonar: fix errors 

Modified:
    jmeter/trunk/src/core/org/apache/jmeter/engine/StandardJMeterEngine.java

Modified: jmeter/trunk/src/core/org/apache/jmeter/engine/StandardJMeterEngine.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/core/org/apache/jmeter/engine/StandardJMeterEngine.java?rev=1776356&r1=1776355&r2=1776356&view=diff
==============================================================================
--- jmeter/trunk/src/core/org/apache/jmeter/engine/StandardJMeterEngine.java (original)
+++ jmeter/trunk/src/core/org/apache/jmeter/engine/StandardJMeterEngine.java Wed Dec 28 23:12:36 2016
@@ -57,14 +57,14 @@ public class StandardJMeterEngine implem
     private static final Logger log = LoggingManager.getLoggerForClass();
 
     // Should we exit at end of the test? (only applies to server, because host is non-null)
-    private static final boolean exitAfterTest =
+    private static final boolean EXIT_AFTER_TEST =
         JMeterUtils.getPropDefault("server.exitaftertest", false);  // $NON-NLS-1$
 
     // Allow engine and threads to be stopped from outside a thread
     // e.g. from beanshell server
     // Assumes that there is only one instance of the engine
     // at any one time so it is not guaranteed to work ...
-    private volatile static StandardJMeterEngine engine;
+    private static volatile StandardJMeterEngine engine;
 
     /*
      * Allow functions etc to register for testStopped notification.
@@ -104,6 +104,16 @@ public class StandardJMeterEngine implem
     // The list of current thread groups; may be setUp, main, or tearDown.
     private final List<AbstractThreadGroup> groups = new CopyOnWriteArrayList<>();
 
+    public StandardJMeterEngine() {
+        this(null);
+    }
+
+    public StandardJMeterEngine(String host) {
+        this.host = host;
+        // Hack to allow external control
+        engine = this;
+    }
+    
     public static void stopEngineNow() {
         if (engine != null) {// May be null if called from Unit test
             engine.stopTest(true);
@@ -142,16 +152,6 @@ public class StandardJMeterEngine implem
 
     // End of code to allow engine to be controlled remotely
 
-    public StandardJMeterEngine() {
-        this(null);
-    }
-
-    public StandardJMeterEngine(String host) {
-        this.host = host;
-        // Hack to allow external control
-        engine = this;
-    }
-
     @Override
     public void configure(HashTree testTree) {
         // Is testplan serialised?
@@ -172,7 +172,7 @@ public class StandardJMeterEngine implem
     public void runTest() throws JMeterEngineException {
         if (host != null){
             long now=System.currentTimeMillis();
-            System.out.println("Starting the test on host " + host + " @ "+new Date(now)+" ("+now+")");
+            System.out.println("Starting the test on host " + host + " @ "+new Date(now)+" ("+now+")"); // NOSONAR Intentional
         }
         try {
             Thread runningThread = new Thread(this, "StandardJMeterEngine");
@@ -224,9 +224,9 @@ public class StandardJMeterEngine implem
         if (host != null) {
             log.info("Test has ended on host "+host);
             long now=System.currentTimeMillis();
-            System.out.println("Finished the test on host " + host + " @ "+new Date(now)+" ("+now+")"
-            +(exitAfterTest ? " - exit requested." : ""));
-            if (exitAfterTest){
+            System.out.println("Finished the test on host " + host + " @ "+new Date(now)+" ("+now+")" // NOSONAR Intentional
+            +(EXIT_AFTER_TEST ? " - exit requested." : ""));
+            if (EXIT_AFTER_TEST){
                 exit();
             }
         }
@@ -257,7 +257,53 @@ public class StandardJMeterEngine implem
         private StopTest(boolean b) {
             now = b;
         }
+        
+        /**
+         * For each current thread group, invoke:
+         * <ul> 
+         * <li>{@link AbstractThreadGroup#stop()} - set stop flag</li>
+         * </ul> 
+         */
+        private void stopAllThreadGroups() {
+            // ConcurrentHashMap does not need synch. here
+            for (AbstractThreadGroup threadGroup : groups) {
+                threadGroup.stop();
+            }
+        }
+        
+        /**
+         * For each thread group, invoke {@link AbstractThreadGroup#tellThreadsToStop()}
+         */
+        private void tellThreadGroupsToStop() {
+            // ConcurrentHashMap does not need protecting
+            for (AbstractThreadGroup threadGroup : groups) {
+                threadGroup.tellThreadsToStop();
+            }
+        }
+        
+        /**
+         * @return boolean true if all threads of all Threead Groups stopped
+         */
+        private boolean verifyThreadsStopped() {
+            boolean stoppedAll = true;
+            // ConcurrentHashMap does not need synch. here
+            for (AbstractThreadGroup threadGroup : groups) {
+                stoppedAll = stoppedAll && threadGroup.verifyThreadsStopped();
+            }
+            return stoppedAll;
+        }
 
+        /**
+         * @return total of active threads in all Thread Groups
+         */
+        private int countStillActiveThreads() {
+            int reminingThreads= 0;
+            for (AbstractThreadGroup threadGroup : groups) {
+                reminingThreads += threadGroup.numberOfActiveThreads();
+            }            
+            return reminingThreads; 
+        }
+        
         @Override
         public void run() {
             running = false;
@@ -272,10 +318,10 @@ public class StandardJMeterEngine implem
                         log.fatalError(JMeterUtils.getResString("stopping_test_failed")); //$NON-NLS-1$
                         if (SYSTEM_EXIT_ON_STOP_FAIL) { // default is true
                             log.fatalError("Exiting");
-                            System.out.println("Fatal error, could not stop test, exiting");
-                            System.exit(1);
+                            System.out.println("Fatal error, could not stop test, exiting"); // NOSONAR Intentional
+                            System.exit(1); // NOSONAR Intentional
                         } else {
-                            System.out.println("Fatal error, could not stop test");                            
+                            System.out.println("Fatal error, could not stop test"); // NOSONAR Intentional                            
                         }
                     } else {
                         JMeterUtils.reportErrorToUser(
@@ -296,7 +342,6 @@ public class StandardJMeterEngine implem
 
         /*
          * Ensure that the sample variables are correctly initialised for each run.
-         * TODO is this the best way to do this? should it be done elsewhere ?
          */
         SampleEvent.initSampleVariables();
 
@@ -306,7 +351,7 @@ public class StandardJMeterEngine implem
             test.traverse(compiler);
         } catch (RuntimeException e) {
             log.error("Error occurred compiling the tree:",e);
-            JMeterUtils.reportErrorToUser("Error occurred compiling the tree: - see log file");
+            JMeterUtils.reportErrorToUser("Error occurred compiling the tree: - see log file", e);
             return; // no point continuing
         }
         /**
@@ -384,10 +429,8 @@ public class StandardJMeterEngine implem
             AbstractThreadGroup group = iter.next();
             //ignore Setup and Post here.  We could have filtered the searcher. but then
             //future Thread Group objects wouldn't execute.
-            if (group instanceof SetupThreadGroup) {
-                continue;
-            }
-            if (group instanceof PostThreadGroup) {
+            if (group instanceof SetupThreadGroup ||
+                    group instanceof PostThreadGroup) {
                 continue;
             }
             groupCount++;
@@ -438,20 +481,9 @@ public class StandardJMeterEngine implem
         JMeterContextService.endTest();
         if (JMeter.isNonGUI() && SYSTEM_EXIT_FORCED) {
             log.info("Forced JVM shutdown requested at end of test");
-            System.exit(0);
+            System.exit(0); // NOSONAR Intentional
         }
     }
-
-    /**
-     * @return total of active threads in all Thread Groups
-     */
-    private int countStillActiveThreads() {
-        int reminingThreads= 0;
-        for (AbstractThreadGroup threadGroup : groups) {
-            reminingThreads += threadGroup.numberOfActiveThreads();
-        }            
-        return reminingThreads; 
-    }
     
     private void startThreadGroup(AbstractThreadGroup group, int groupCount, SearchByClass<?> searcher, List<?> testLevelElements, ListenerNotifier notifier)
     {
@@ -481,25 +513,14 @@ public class StandardJMeterEngine implem
     
             groups.add(group);
             group.start(groupCount, notifier, threadGroupTree, this);
-        } catch (JMeterStopTestException ex) {
-            JMeterUtils.reportErrorToUser("Error occurred compiling the tree:\r\n "+ex.getMessage()+", \r\nsee log file for more details");
+        } catch (JMeterStopTestException ex) { // NOSONAR Reported by log
+            JMeterUtils.reportErrorToUser("Error occurred starting thread group :" + group.getName()+ ", error message:"+ex.getMessage()
+                +", \r\nsee log file for more details", ex);
             return; // no point continuing
         }
     }
 
     /**
-     * @return boolean true if all threads of all Threead Groups stopped
-     */
-    private boolean verifyThreadsStopped() {
-        boolean stoppedAll = true;
-        // ConcurrentHashMap does not need synch. here
-        for (AbstractThreadGroup threadGroup : groups) {
-            stoppedAll = stoppedAll && threadGroup.verifyThreadsStopped();
-        }
-        return stoppedAll;
-    }
-
-    /**
      * Wait for Group Threads to stop
      */
     private void waitThreadsStopped() {
@@ -509,40 +530,19 @@ public class StandardJMeterEngine implem
         }
     }
 
-    /**
-     * For each thread group, invoke {@link AbstractThreadGroup#tellThreadsToStop()}
-     */
-    private void tellThreadGroupsToStop() {
-        // ConcurrentHashMap does not need protecting
-        for (AbstractThreadGroup threadGroup : groups) {
-            threadGroup.tellThreadsToStop();
-        }
-    }
-
     public void askThreadsToStop() {
         if (engine != null) { // Will be null if StopTest thread has started
             engine.stopTest(false);
         }
     }
 
-    /**
-     * For each current thread group, invoke:
-     * <ul> 
-     * <li>{@link AbstractThreadGroup#stop()} - set stop flag</li>
-     * </ul> 
+    /** 
+     * Remote exit
+     * Called by RemoteJMeterEngineImpl.rexit()
+     * and by notifyTestListenersOfEnd() iff exitAfterTest is true;
+     * in turn that is called by the run() method and the StopTest class
+     * also called
      */
-    private void stopAllThreadGroups() {
-        // ConcurrentHashMap does not need synch. here
-        for (AbstractThreadGroup threadGroup : groups) {
-            threadGroup.stop();
-        }
-    }
-
-    // Remote exit
-    // Called by RemoteJMeterEngineImpl.rexit()
-    // and by notifyTestListenersOfEnd() iff exitAfterTest is true;
-    // in turn that is called by the run() method and the StopTest class
-    // also called
     @Override
     public void exit() {
         ClientJMeterEngine.tidyRMI(log); // This should be enough to allow server to exit.
@@ -554,8 +554,8 @@ public class StandardJMeterEngine implem
                 public void run() {
                     pause(1000); // Allow RMI to complete
                     log.info("Bye from "+host);
-                    System.out.println("Bye from "+host);
-                    System.exit(0);
+                    System.out.println("Bye from "+host); // NOSONAR Intentional
+                    System.exit(0); // NOSONAR Intentional
                 }
             };
             t.start();
@@ -566,6 +566,7 @@ public class StandardJMeterEngine implem
         try {
             TimeUnit.MILLISECONDS.sleep(ms);
         } catch (InterruptedException e) {
+            Thread.currentThread().interrupt();
         }
     }