jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject svn commit: r1700115 - /jmeter/branches/SampleTimeout/src/components/org/apache/jmeter/timers/InterruptTimer.java
Date Sun, 30 Aug 2015 13:02:48 GMT
Author: sebb
Date: Sun Aug 30 13:02:48 2015
New Revision: 1700115

URL: http://svn.apache.org/r1700115
Log:
Switch to ThreadListener as that allows tidyup at end of thread
Use IODH for the Executor

Modified:
    jmeter/branches/SampleTimeout/src/components/org/apache/jmeter/timers/InterruptTimer.java

Modified: jmeter/branches/SampleTimeout/src/components/org/apache/jmeter/timers/InterruptTimer.java
URL: http://svn.apache.org/viewvc/jmeter/branches/SampleTimeout/src/components/org/apache/jmeter/timers/InterruptTimer.java?rev=1700115&r1=1700114&r2=1700115&view=diff
==============================================================================
--- jmeter/branches/SampleTimeout/src/components/org/apache/jmeter/timers/InterruptTimer.java
(original)
+++ jmeter/branches/SampleTimeout/src/components/org/apache/jmeter/timers/InterruptTimer.java
Sun Aug 30 13:02:48 2015
@@ -7,19 +7,18 @@ import java.util.concurrent.ScheduledFut
 import java.util.concurrent.ThreadFactory;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.jmeter.engine.event.LoopIterationEvent;
-import org.apache.jmeter.engine.event.LoopIterationListener;
 import org.apache.jmeter.samplers.Interruptible;
 import org.apache.jmeter.samplers.Sampler;
 import org.apache.jmeter.testelement.AbstractTestElement;
 import org.apache.jmeter.testelement.TestElement;
+import org.apache.jmeter.testelement.ThreadListener;
 import org.apache.jmeter.threads.JMeterContext;
 import org.apache.jmeter.threads.JMeterContextService;
 import org.apache.jmeter.util.JMeterUtils;
 import org.apache.jorphan.logging.LoggingManager;
 import org.apache.log.Logger;
 
-public class InterruptTimer extends AbstractTestElement implements Timer, Serializable, LoopIterationListener
{
+public class InterruptTimer extends AbstractTestElement implements Timer, Serializable, ThreadListener
{
 
     private static final long serialVersionUID = 1L;
 
@@ -27,13 +26,27 @@ public class InterruptTimer extends Abst
 
     private static final String TIMEOUT = "InterruptTimer.timeout"; //$NON-NLS-1$
 
-    private long timeout = 0;
+    private static class TPOOLHolder {
+        static final ScheduledExecutorService EXEC_SERVICE =
+                Executors.newScheduledThreadPool(1,
+                        new ThreadFactory() {
+                            public Thread newThread(Runnable r) {
+                                Thread t = Executors.defaultThreadFactory().newThread(r);
+                                t.setDaemon(true); // also ensures that Executor thread is
daemon
+                                return t;
+                            }
+                        });
+    }
+
+    private static ScheduledExecutorService getExecutorService() {
+        return TPOOLHolder.EXEC_SERVICE;
+    }
 
     private JMeterContext context;
 
     private ScheduledFuture<?> future;
     
-    private ScheduledExecutorService tpool;
+    private final ScheduledExecutorService execService;
     
     private final boolean debug;
 
@@ -41,8 +54,12 @@ public class InterruptTimer extends Abst
      * No-arg constructor.
      */
     public InterruptTimer() {
-//        LOG.setPriority(org.apache.log.Priority.DEBUG); // for local debugging
-        debug = LOG.isDebugEnabled(); // TODO is this the best place for this? 
+        LOG.setPriority(org.apache.log.Priority.DEBUG); // for local debugging when enabled
+        debug = LOG.isDebugEnabled();
+        execService = getExecutorService();
+        if (debug) {
+            LOG.debug(whoAmI("InterruptTimer()", this));
+        }
     }
 
     /**
@@ -65,7 +82,8 @@ public class InterruptTimer extends Abst
     /**
      * Retrieve the delay to use during test execution.
      * This is called just before starting a sampler.
-     * It is used to schedule future task to interrupt the sampler.
+     * It is used to schedule a future task to interrupt the sampler.
+     * It also cancels any existing timer
      * 
      * @return Always returns zero, because this timer does not wait
      */
@@ -74,81 +92,80 @@ public class InterruptTimer extends Abst
         if (debug) {
             LOG.debug(whoAmI("delay()", this));
         }
-        if (future != null) {
-            if (!future.isDone()) {
-                boolean cancelled = future.cancel(false);
-                if (debug) {
-                    LOG.debug("Cancelled the task:" + future + " with result " + cancelled);
-                }
-            }
-            future = null;
-        }
+        cancelTask(); // cancel previous if any
+        long timeout = getPropertyAsLong(TIMEOUT); // refetch each time so it can be a variable
         if (timeout <= 0) {
             return 0;
         }
         final Sampler samp = context.getCurrentSampler();
-        if (!(samp instanceof Interruptible)) {
-            // Log this?
-            return 0;
+        if (!(samp instanceof Interruptible)) { // may be applied to a whole test 
+            return 0; // Cannot time out in this case
         }
         final Interruptible sampler = (Interruptible) samp;
         Runnable run=new Runnable() {
             public void run() {
                   boolean interrupted = sampler.interrupt();
                   if (interrupted) {
-                      LOG.warn("Interruped the sampler " + getInfo(samp));
+                      LOG.warn("Done interrupting " + getInfo(samp));
                   } else {
                       if (debug) {
-                          LOG.debug("Did not interrupt the sampler " + getInfo(samp));  
                       
+                          LOG.debug("Didn't interrupt: " + getInfo(samp));              
           
                       }
                   }
             }
         };
-        // schedule the interrupt to occur 
+
+        // schedule the interrupt to occur and save for possible cancellation 
+        future = execService.schedule(run, timeout, TimeUnit.MILLISECONDS);
         if (debug) {
-            LOG.debug("Scheduling timer for " + getInfo(samp));
+            LOG.debug("Scheduled timer: @" + System.identityHashCode(future) + " " + getInfo(samp));
         }
-        future = tpool.schedule(run, timeout, TimeUnit.MILLISECONDS);
         return 0;
     }
 
-    /**
-     * Provide a description of this timer class.
-     *
-     * @return the description of this timer class.
-     */
     @Override
-    public String toString() {
-        return JMeterUtils.getResString("interrupt_timer_memo"); //$NON-NLS-1$
-    }
+    public void threadStarted() {
+        if (debug) {
+            LOG.debug(whoAmI("threadStarted()", this));
+        }
+        context = JMeterContextService.getContext();
+     }
+
+    @Override
+    public void threadFinished() {
+        if (debug) {
+            LOG.debug(whoAmI("threadFinished()", this));
+        }
+        cancelTask(); // cancel final if any
+     }
 
     /**
-     * Gain access to any variables that have been defined.
+     * Provide a description of this class.
      *
-     * @see LoopIterationListener#iterationStart(LoopIterationEvent)
+     * @return the description of this class.
      */
     @Override
-    public void iterationStart(LoopIterationEvent event) {
-        if (debug) {
-            LOG.debug(whoAmI("iterationStart()", this));
-        }
-        timeout = getPropertyAsLong(TIMEOUT);
-        context = JMeterContextService.getContext(); // TODO is this called from the correct
thread?
-        tpool = Executors.newScheduledThreadPool(1, // ditto
-            new ThreadFactory() {
-                public Thread newThread(Runnable r) {
-                    Thread t = Executors.defaultThreadFactory().newThread(r);
-                    t.setDaemon(true);
-                    return t;
-                }
-            });
+    public String toString() {
+        return JMeterUtils.getResString("interrupt_timer_memo"); //$NON-NLS-1$
     }
 
     private String whoAmI(String id, TestElement o) {
-        return id + " @" + System.identityHashCode(o)+ " '"+ o.getName() + "'";         
+        return id + " @" + System.identityHashCode(o)+ " '"+ o.getName() + "' " + (debug
?  Thread.currentThread().getName() : "");         
     }
 
     private String getInfo(TestElement o) {
         return whoAmI(o.getClass().getSimpleName(), o); 
     }
+
+    private void cancelTask() {
+        if (future != null) {
+            if (!future.isDone()) {
+                boolean cancelled = future.cancel(false);
+                if (debug) {
+                    LOG.debug("Cancelled the task: @" + System.identityHashCode(future) +
" with result " + cancelled);
+                }
+            }
+            future = null;
+        }        
+    }
 }



Mime
View raw message