ruthst00 commented on code in PR #6770:
URL: https://github.com/apache/jmeter/pull/6770#discussion_r4106391603


##########
src/components/src/test/java/org/apache/jmeter/control/TestTransactionController.java:
##########
@@ -18,23 +18,224 @@
 package org.apache.jmeter.control;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import org.apache.jmeter.assertions.ResponseAssertion;
 import org.apache.jmeter.junit.JMeterTestCase;
 import org.apache.jmeter.sampler.DebugSampler;
+import org.apache.jmeter.samplers.AbstractSampler;
+import org.apache.jmeter.samplers.Entry;
+import org.apache.jmeter.samplers.SampleResult;
 import org.apache.jmeter.test.samplers.CollectSamplesListener;
+import org.apache.jmeter.testelement.AbstractTestElement;
 import org.apache.jmeter.threads.JMeterContextService;
 import org.apache.jmeter.threads.JMeterThread;
 import org.apache.jmeter.threads.JMeterVariables;
 import org.apache.jmeter.threads.ListenerNotifier;
 import org.apache.jmeter.threads.TestCompiler;
 import org.apache.jmeter.threads.ThreadGroup;
+import org.apache.jmeter.timers.Timer;
 import org.apache.jorphan.collections.ListedHashTree;
 import org.junit.jupiter.api.Test;
 
 
 public class TestTransactionController extends JMeterTestCase {
 
+    /**
+     * A simple sampler that returns a successful result with a fixed elapsed 
time.
+     */
+    private static class FixedElapsedTimeSampler extends AbstractSampler {
+        private static final long serialVersionUID = 1L;
+        private final long elapsedTimeMs;
+
+        FixedElapsedTimeSampler(long elapsedTimeMs) {
+            this.elapsedTimeMs = elapsedTimeMs;
+        }
+
+        @Override
+        public SampleResult sample(Entry e) {
+            SampleResult result = new SampleResult();
+            result.setSampleLabel(getName());
+            result.sampleStart();
+            result.setSuccessful(true);
+            result.setResponseCodeOK();
+            // Simulate elapsed time by setting start/end times directly
+            long start = result.getStartTime();
+            result.setEndTime(start + elapsedTimeMs);
+            return result;
+        }
+    }
+
+    /**
+     * A timer that returns a fixed delay.
+     */
+    private static class FixedDelayTimer extends AbstractTestElement 
implements Timer {
+        private static final long serialVersionUID = 1L;
+        private final long delayMs;
+
+        FixedDelayTimer(long delayMs) {
+            this.delayMs = delayMs;
+        }
+
+        @Override
+        public long delay() {
+            return delayMs;
+        }
+    }
+
+    /**
+     * Test for GitHub issue #6496: transaction elapsed time should not 
include timer delay
+     * in non-parent mode (TransactionController with "Generate parent sample" 
unchecked).
+     *
+     * <p>The test sets up a transaction with two samplers and a timer between 
them.
+     * The transaction elapsed time should equal the sum of child sample 
elapsed times,
+     * not be inflated by the timer delay between samples.
+     */
+    @Test
+    public void testIssue6496NonParentMode() throws Exception {
+        JMeterContextService.getContext().setVariables(new JMeterVariables());
+
+        CollectSamplesListener listener = new CollectSamplesListener();
+
+        TransactionController transactionController = new 
TransactionController();
+        transactionController.setGenerateParentSample(false);
+        transactionController.setIncludeTimers(false);
+
+        // Use a simulated elapsed time much smaller than the timer delay
+        long childElapsedMs = 10L;
+        long timerDelayMs = 200L; // timer delay before second sampler
+
+        FixedElapsedTimeSampler firstSampler = new 
FixedElapsedTimeSampler(childElapsedMs);
+        firstSampler.setName("First Sampler");
+
+        FixedDelayTimer timer = new FixedDelayTimer(timerDelayMs);
+        timer.setName("Timer Before Second Sampler");
+        timer.setEnabled(true);
+
+        FixedElapsedTimeSampler secondSampler = new 
FixedElapsedTimeSampler(childElapsedMs);
+        secondSampler.setName("Second Sampler");
+
+        LoopController loop = new LoopController();
+        loop.setLoops(1);
+        loop.setContinueForever(false);
+        loop.setEnabled(true);
+
+        ListedHashTree hashTree = new ListedHashTree();
+        hashTree.add(loop);
+        hashTree.add(loop, transactionController);
+        // In non-parent mode, the TransactionController fires events using 
the SamplePackage
+        // of the last sampler that ran. The listener must be a child of that 
sampler.
+        hashTree.add(transactionController, firstSampler);
+        hashTree.add(transactionController, secondSampler);
+        hashTree.add(secondSampler, timer);
+        hashTree.add(secondSampler, listener);
+
+        TestCompiler compiler = new TestCompiler(hashTree);
+        hashTree.traverse(compiler);
+
+        ThreadGroup threadGroup = new ThreadGroup();
+        threadGroup.setNumThreads(1);
+
+        ListenerNotifier notifier = new ListenerNotifier();
+
+        JMeterThread thread = new JMeterThread(hashTree, threadGroup, 
notifier);
+        thread.setThreadGroup(threadGroup);
+        thread.run();
+
+        assertFalse(listener.getEvents().isEmpty(),
+                "At least one transaction sample should have been collected");
+
+        // Find the transaction result (not the individual sample results)
+        SampleResult transactionResult = listener.getEvents().stream()
+                .map(e -> e.getResult())
+                .filter(TransactionController::isFromTransactionController)
+                .findFirst()
+                .orElse(null);
+        assertTrue(transactionResult != null,
+                "A transaction result should have been collected");
+        // The transaction elapsed time should be the sum of child sample 
times,
+        // not inflated by the timer delay between samples.
+        // Both child samples have very short simulated elapsed times (10ms 
each),
+        // so the total should be well under the timer delay (200ms).
+        assertTrue(transactionResult.getTime() < timerDelayMs,

Review Comment:
   Fixed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to