This is an automated email from the ASF dual-hosted git repository. fschumacher pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jmeter.git
commit f2c5260abe7a3e1022793474c0a1e322dd0a2010 Author: Felix Schumacher <[email protected]> AuthorDate: Sat Sep 7 15:20:24 2019 +0200 Revert behaviour of TimerService#adjustDelay to that of 5.1.1 Changing the adjustDelay method to return -1 when the delay would be longer than the remaining scheduled duration, lead to bug 63711. This change reverts the default adjustDelay to the old behaviour and adds a flag to enable the "new" feature of returning -1 when the sleep time would be too long. Bugzilla Id: 63711 --- .../org/apache/jmeter/timers/TimerServiceTest.java | 33 +++++++++++++++- .../org/apache/jmeter/threads/JMeterThread.java | 3 +- .../org/apache/jmeter/timers/TimerService.java | 46 +++++++++++++++++++--- 3 files changed, 74 insertions(+), 8 deletions(-) diff --git a/src/components/src/test/java/org/apache/jmeter/timers/TimerServiceTest.java b/src/components/src/test/java/org/apache/jmeter/timers/TimerServiceTest.java index 428efd2..0c46662 100644 --- a/src/components/src/test/java/org/apache/jmeter/timers/TimerServiceTest.java +++ b/src/components/src/test/java/org/apache/jmeter/timers/TimerServiceTest.java @@ -18,7 +18,9 @@ package org.apache.jmeter.timers; +import org.hamcrest.BaseMatcher; import org.hamcrest.CoreMatchers; +import org.hamcrest.Description; import org.junit.Assert; import org.junit.Test; @@ -27,14 +29,41 @@ public class TimerServiceTest { TimerService sut = TimerService.getInstance(); @Test - public void testBigInitialDelay() { + public void testBigInitialDelayAndDontWait() { long now = System.currentTimeMillis(); - long adjustedDelay = sut.adjustDelay(Long.MAX_VALUE, now + 1000L); + long adjustedDelay = sut.adjustDelay(Long.MAX_VALUE, now + 1000L, false); Assert.assertThat("TimerService should return -1 as delay would lead to a time after end time", Long.valueOf(adjustedDelay), CoreMatchers.is(Long.valueOf(-1))); } @Test + public void testBigInitialDelayAndWait() { + long now = System.currentTimeMillis(); + long adjustedDelay = sut.adjustDelay(Long.MAX_VALUE, now + 1000L); + Assert.assertThat("TimerService should return -1 as delay would lead to a time after end time", + Long.valueOf(adjustedDelay), isAlmost(1000L, 200L)); + } + + private BaseMatcher<Long> isAlmost(long value, long precision) { + return new BaseMatcher<Long>() { + + @Override + public boolean matches(Object item) { + if (item instanceof Long) { + Long other = (Long) item; + return Math.abs(other.longValue() - value) < precision; + } + return false; + } + + @Override + public void describeTo(Description description) { + description.appendText("the number is within a precision of " + precision + " near " + value); + } + }; + } + + @Test public void testSmallInitialDelay() { long now = System.currentTimeMillis(); Assert.assertThat("TimerService should not change the delay as the end time is far away", diff --git a/src/core/src/main/java/org/apache/jmeter/threads/JMeterThread.java b/src/core/src/main/java/org/apache/jmeter/threads/JMeterThread.java index 258c901..6fc1194 100644 --- a/src/core/src/main/java/org/apache/jmeter/threads/JMeterThread.java +++ b/src/core/src/main/java/org/apache/jmeter/threads/JMeterThread.java @@ -494,6 +494,7 @@ public class JMeterThread implements Runnable, Interruptible { // checks the scheduler to stop the iteration stopSchedulerIfNeeded(); } + } catch (JMeterStopTestException e) { // NOSONAR if (log.isInfoEnabled()) { log.info("Stopping Test: {}", e.toString()); @@ -969,7 +970,7 @@ public class JMeterThread implements Runnable, Interruptible { if (scheduler) { // We reduce pause to ensure end of test is not delayed by a sleep ending after test scheduled end // See Bug 60049 - totalDelay = TIMER_SERVICE.adjustDelay(totalDelay, endTime); + totalDelay = TIMER_SERVICE.adjustDelay(totalDelay, endTime, false); if (totalDelay < 0) { log.debug("The delay would be longer than the scheduled period, so stop thread now."); running = false; diff --git a/src/core/src/main/java/org/apache/jmeter/timers/TimerService.java b/src/core/src/main/java/org/apache/jmeter/timers/TimerService.java index 2fec09e..1b54571 100644 --- a/src/core/src/main/java/org/apache/jmeter/timers/TimerService.java +++ b/src/core/src/main/java/org/apache/jmeter/timers/TimerService.java @@ -47,28 +47,64 @@ public class TimerService { /** * Adjust delay so that initialDelay does not exceed end of test - * @param initialDelay initial delay in millis + * @param initialDelay initial delay in milliseconds * @return initialDelay or adjusted delay */ public long adjustDelay(final long initialDelay) { + return adjustDelay(initialDelay, true); + } + + /** + * Adjust delay so that initialDelay does not exceed end of test<br> + * If {@code runTillEnd} is {@code false} the delay will be shortened + * to {@code -1} if the delay would be longer than the remaining run time. + * + * @param initialDelay initial delay in milliseconds + * @param runTillEnd adjust delay to match the scheduled end + * @return initialDelay or adjusted delay + */ + public long adjustDelay(final long initialDelay, boolean runTillEnd) { JMeterThread thread = JMeterContextService.getContext().getThread(); long endTime = thread != null ? thread.getEndTime() : 0; - return adjustDelay(initialDelay, endTime); + return adjustDelay(initialDelay, endTime, runTillEnd); } + /** - * Adjust delay so that initialDelay does not exceed end of test - * @param initialDelay initial delay in millis + * Adjust delay so that initialDelay does not exceed end of test< + * + * @param initialDelay initial delay in milliseconds * @param endTime End time of JMeterThread - * @return initialDelay or {@code -1} if delay is too long + * @return initialDelay or a shortened delay if delay is longer than the test is + * scheduled */ public long adjustDelay(final long initialDelay, long endTime) { + return adjustDelay(initialDelay, endTime, true); + } + + /** + * Adjust delay so that initialDelay does not exceed end of test<br> + * If {@code runTillEnd} is {@code false} the delay will be shortened + * to {@code -1} if the delay would be longer than the remaining run time. + * + * @param initialDelay initial delay in milliseconds + * @param endTime End time of JMeterThread + * @param runTillEnd adjust delay to match the scheduled end + * @return initialDelay or a shortened delay if delay is longer than the test is + * scheduled + */ + public long adjustDelay(final long initialDelay, long endTime, boolean runTillEnd) { if (endTime > 0) { long now = System.currentTimeMillis(); if (initialDelay > endTime - now) { + if (runTillEnd) { + // shorten the delay to match endTime + return endTime - now; + } return -1; } } return initialDelay; } + }
