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 1527a8fcb1c96b2270d215775687443dfadc480d Author: Felix Schumacher <[email protected]> AuthorDate: Mon Apr 18 16:42:41 2022 +0200 Get rid of warning about converting long to float by introducing a new private assertion method. While we are here, convert to junit5 and add another assertion method to check vor valid range. --- .../jmeter/timers/ConstantThroughputTimerTest.java | 91 ++++++------ .../apache/jmeter/samplers/TestSampleResult.java | 160 +++++++++++---------- 2 files changed, 130 insertions(+), 121 deletions(-) diff --git a/src/components/src/test/java/org/apache/jmeter/timers/ConstantThroughputTimerTest.java b/src/components/src/test/java/org/apache/jmeter/timers/ConstantThroughputTimerTest.java index f8c8df1391..a55b4bae4a 100644 --- a/src/components/src/test/java/org/apache/jmeter/timers/ConstantThroughputTimerTest.java +++ b/src/components/src/test/java/org/apache/jmeter/timers/ConstantThroughputTimerTest.java @@ -17,32 +17,26 @@ package org.apache.jmeter.timers; -import static org.junit.Assert.assertEquals; - import java.util.UUID; import org.apache.jmeter.threads.JMeterContextService; import org.apache.jmeter.threads.TestJMeterContextService; import org.apache.jmeter.util.BeanShellInterpreter; import org.apache.jmeter.util.ScriptingTestElement; -import org.junit.Assert; -import org.junit.Assume; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class ConstantThroughputTimerTest { - private static final Logger log = LoggerFactory.getLogger(ConstantThroughputTimerTest.class); +class ConstantThroughputTimerTest { @Test - public void testTimer1() throws Exception { + void testTimer1() throws Exception { ConstantThroughputTimer timer = new ConstantThroughputTimer(); - assertEquals(0,timer.getCalcMode());// Assume this thread only + Assertions.assertEquals(0, timer.getCalcMode());// Assume this thread only timer.setThroughput(60.0);// 1 per second long start = System.currentTimeMillis(); long delay = timer.delay(); // Initialise - assertEquals(0,delay); + Assertions.assertEquals(0, delay); // The test tries to check if the calculated delay is correct. // If the build machine is busy, then the sleep(500) may take longer in // which case the calculated delay should be shorter. @@ -55,107 +49,120 @@ public class ConstantThroughputTimerTest { if (expected < 0) { expected = 0; } - assertEquals("Expected delay of approx 500", expected, timer.delay(), 50); + assertAlmostEquals(expected, timer.delay(), 50); } + private static void assertAlmostEquals(long expected, long actual, long delta) { + long actualDelta = Math.abs(actual - expected); + if (actualDelta > delta) { + Assertions.fail(() -> "Expected " + expected + " within delta of " + delta + + ", but got " + actual + " which is a delta of " + actualDelta); + } + } @Test - public void testTimer2() throws Exception { + void testTimer2() throws Exception { ConstantThroughputTimer timer = new ConstantThroughputTimer(); - assertEquals(0,timer.getCalcMode());// Assume this thread only + Assertions.assertEquals(0, timer.getCalcMode());// Assume this thread only timer.setThroughput(60.0);// 1 per second - assertEquals(1000,timer.calculateCurrentTarget(0)); // Should delay for 1 second + Assertions.assertEquals(1000, timer.calculateCurrentTarget(0)); // Should delay for 1 second timer.setThroughput(60000.0);// 1 per milli-second - assertEquals(1,timer.calculateCurrentTarget(0)); // Should delay for 1 milli-second + Assertions.assertEquals(1, timer.calculateCurrentTarget(0)); // Should delay for 1 milli-second } @Test - public void testTimer3() throws Exception { + void testTimer3() throws Exception { ConstantThroughputTimer timer = new ConstantThroughputTimer(); timer.setMode(ConstantThroughputTimer.Mode.AllActiveThreads); //$NON-NLS-1$ - all threads - assertEquals(1,timer.getCalcMode());// All threads + Assertions.assertEquals(1, timer.getCalcMode());// All threads for(int i=1; i<=10; i++){ TestJMeterContextService.incrNumberOfThreads(); } - assertEquals(10,JMeterContextService.getNumberOfThreads()); + Assertions.assertEquals(10, JMeterContextService.getNumberOfThreads()); timer.setThroughput(600.0);// 10 per second - assertEquals(1000,timer.calculateCurrentTarget(0)); // Should delay for 1 second + Assertions.assertEquals(1000, timer.calculateCurrentTarget(0)); // Should delay for 1 second timer.setThroughput(600000.0);// 10 per milli-second - assertEquals(1,timer.calculateCurrentTarget(0)); // Should delay for 1 milli-second + Assertions.assertEquals(1, timer.calculateCurrentTarget(0)); // Should delay for 1 milli-second for(int i=1; i<=990; i++){ TestJMeterContextService.incrNumberOfThreads(); } - assertEquals(1000,JMeterContextService.getNumberOfThreads()); + Assertions.assertEquals(1000, JMeterContextService.getNumberOfThreads()); timer.setThroughput(60000000.0);// 1000 per milli-second - assertEquals(1,timer.calculateCurrentTarget(0)); // Should delay for 1 milli-second + Assertions.assertEquals(1, timer.calculateCurrentTarget(0)); // Should delay for 1 milli-second } @Test - public void testTimerBSH() throws Exception { - Assume.assumeTrue("BeanShell jar should be on the classpath, otherwise the test makes no sense", - BeanShellInterpreter.isInterpreterPresent()); + void testTimerBSH() throws Exception { + Assumptions.assumeTrue(BeanShellInterpreter.isInterpreterPresent(), + "BeanShell jar should be on the classpath, otherwise the test makes no sense"); BeanShellTimer timer = new BeanShellTimer(); timer.setScript("\"60\""); - assertEquals(60, timer.delay()); + Assertions.assertEquals(60, timer.delay()); timer.setScript("60"); - assertEquals(60, timer.delay()); + Assertions.assertEquals(60, timer.delay()); timer.setScript("5*3*4"); - assertEquals(60,timer.delay()); + Assertions.assertEquals(60, timer.delay()); } @Test - public void testTimerJSR223Timer() throws Exception { + void testTimerJSR223Timer() throws Exception { JSR223Timer timer = new JSR223Timer(); timer.setScriptLanguage(ScriptingTestElement.DEFAULT_SCRIPT_LANGUAGE); timer.setCacheKey(UUID.randomUUID().toString()); timer.setScript("\"60\""); - assertEquals(60, timer.delay()); + Assertions.assertEquals(60, timer.delay()); timer.setScript("60"); - assertEquals(60, timer.delay()); + Assertions.assertEquals(60, timer.delay()); timer.setScript("5*3*4"); - assertEquals(60,timer.delay()); + Assertions.assertEquals(60, timer.delay()); } @Test - public void testUniformRandomTimer() throws Exception { + void testUniformRandomTimer() throws Exception { UniformRandomTimer timer = new UniformRandomTimer(); timer.setDelay("1000"); timer.setRange(100d); timer.iterationStart(null); long delay = timer.delay(); - Assert.assertTrue("delay:"+delay +" is not in expected range", delay >= 1000 && delay <=1100); + assertBetween(1000, 1100, delay); } + private static void assertBetween(long expectedLow, long expectedHigh, long actual) { + if (actual < expectedLow || actual > expectedHigh) { + Assertions.fail(() -> "delay not in expected range: expected " + actual + + " to be within " + expectedLow + " and " + expectedHigh); + } + } @Test - public void testConstantTimer() throws Exception { + void testConstantTimer() throws Exception { ConstantTimer timer = new ConstantTimer(); timer.setDelay("1000"); timer.iterationStart(null); - assertEquals(1000, timer.delay()); + Assertions.assertEquals(1000, timer.delay()); } @Test - public void testPoissonRandomTimerRangeHigherThan30() throws Exception { + void testPoissonRandomTimerRangeHigherThan30() throws Exception { PoissonRandomTimer timer = new PoissonRandomTimer(); timer.setDelay("300"); timer.setRange(100d); timer.iterationStart(null); long delay = timer.delay(); - Assert.assertTrue("delay:"+delay +" is not in expected range", delay >= 356 && delay <=457); + assertBetween(356, 457, delay); } @Test - public void testPoissonRandomTimerRangeLowerThan30() throws Exception { + void testPoissonRandomTimerRangeLowerThan30() throws Exception { PoissonRandomTimer timer = new PoissonRandomTimer(); timer.setDelay("300"); timer.setRange(30d); timer.iterationStart(null); long delay = timer.delay(); - Assert.assertTrue("delay:"+delay +" is not in expected range", delay >= 305 && delay <=362); + assertBetween(305, 362, delay); } } diff --git a/src/core/src/test/java/org/apache/jmeter/samplers/TestSampleResult.java b/src/core/src/test/java/org/apache/jmeter/samplers/TestSampleResult.java index 9bdbea7469..a551a5287f 100644 --- a/src/core/src/test/java/org/apache/jmeter/samplers/TestSampleResult.java +++ b/src/core/src/test/java/org/apache/jmeter/samplers/TestSampleResult.java @@ -17,24 +17,18 @@ package org.apache.jmeter.samplers; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - import org.apache.jmeter.junit.JMeterTestCase; import org.apache.jmeter.testelement.TestPlan; import org.apache.jmeter.util.Calculator; import org.apache.jmeter.util.LogRecordingDelegatingLogger; import org.apache.jorphan.test.JMeterSerialTest; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class TestSampleResult implements JMeterSerialTest { +class TestSampleResult implements JMeterSerialTest { @Test - public void testElapsedTrue() throws Exception { + void testElapsedTrue() throws Exception { SampleResult res = new SampleResult(true); // Check sample increments OK @@ -43,12 +37,12 @@ public class TestSampleResult implements JMeterSerialTest { res.sampleEnd(); long time = res.getTime(); if (time < 100) { - fail("Sample time should be >=100, actual " + time); + Assertions.fail("Sample time should be >=100, actual " + time); } } @Test - public void testElapsedFalse() throws Exception { + void testElapsedFalse() throws Exception { SampleResult res = new SampleResult(false); // Check sample increments OK @@ -57,12 +51,12 @@ public class TestSampleResult implements JMeterSerialTest { res.sampleEnd(); long time = res.getTime(); if (time < 100) { - fail("Sample time should be >=100, actual " + time); + Assertions.fail("Sample time should be >=100, actual " + time); } } @Test - public void testPauseFalse() throws Exception { + void testPauseFalse() throws Exception { SampleResult res = new SampleResult(false); // Check sample increments OK res.sampleStart(); @@ -76,11 +70,19 @@ public class TestSampleResult implements JMeterSerialTest { totalSampleTime += sleep(100); res.sampleEnd(); long sampleTime = res.getTime(); - assertEquals("Accumulated sample time", totalSampleTime, sampleTime, 50); + assertAlmostEquals(totalSampleTime, sampleTime, 50, "Accumulated sample time"); } + private static void assertAlmostEquals(long expected, long actual, long delta, String message) { + long actualDelta = Math.abs(expected - actual); + if (actualDelta > delta) { + Assertions.fail(() -> message + ", expected " + expected + + " within delta of " + delta + ", but got " + actual + + " which results in actual delta of " + actualDelta); + } + } @Test - public void testPauseTrue() throws Exception { + void testPauseTrue() throws Exception { SampleResult res = new SampleResult(true); // Check sample increments OK res.sampleStart(); @@ -94,7 +96,7 @@ public class TestSampleResult implements JMeterSerialTest { totalSampleTime += sleep(100); res.sampleEnd(); long sampleTime = res.getTime(); - assertEquals("Accumulated sample time", totalSampleTime, sampleTime, 50); + assertAlmostEquals(totalSampleTime, sampleTime, 50, "Accumulated sample time"); } private LogRecordingDelegatingLogger recordLogger; @@ -110,76 +112,76 @@ public class TestSampleResult implements JMeterSerialTest { } @Test - public void testPause2True() throws Exception { + void testPause2True() throws Exception { divertLog(); SampleResult res = new SampleResult(true); res.sampleStart(); res.samplePause(); - assertEquals(0, recordLogger.getLogRecordCount()); + Assertions.assertEquals(0, recordLogger.getLogRecordCount()); res.samplePause(); - assertNotEquals(0, recordLogger.getLogRecordCount()); + Assertions.assertNotEquals(0, recordLogger.getLogRecordCount()); } @Test - public void testPause2False() throws Exception { + void testPause2False() throws Exception { divertLog(); SampleResult res = new SampleResult(false); res.sampleStart(); res.samplePause(); - assertEquals(0, recordLogger.getLogRecordCount()); + Assertions.assertEquals(0, recordLogger.getLogRecordCount()); res.samplePause(); - assertNotEquals(0, recordLogger.getLogRecordCount()); + Assertions.assertNotEquals(0, recordLogger.getLogRecordCount()); } @Test - public void testByteCount() throws Exception { + void testByteCount() throws Exception { SampleResult res = new SampleResult(); res.sampleStart(); res.setBytes(100L); res.setSampleLabel("sample of size 100 bytes"); res.sampleEnd(); - assertEquals(100, res.getBytesAsLong()); - assertEquals("sample of size 100 bytes", res.getSampleLabel()); + Assertions.assertEquals(100, res.getBytesAsLong()); + Assertions.assertEquals("sample of size 100 bytes", res.getSampleLabel()); } @Test - public void testSubResultsTrue() throws Exception { + void testSubResultsTrue() throws Exception { testSubResults(true, 0); } @Test - public void testSubResultsTrueThread() throws Exception { + void testSubResultsTrueThread() throws Exception { testSubResults(true, 500L, 0); } @Test - public void testSubResultsFalse() throws Exception { + void testSubResultsFalse() throws Exception { testSubResults(false, 0); } @Test - public void testSubResultsFalseThread() throws Exception { + void testSubResultsFalseThread() throws Exception { testSubResults(false, 500L, 0); } @Test - public void testSubResultsTruePause() throws Exception { + void testSubResultsTruePause() throws Exception { testSubResults(true, 100); } @Test - public void testSubResultsTruePauseThread() throws Exception { + void testSubResultsTruePauseThread() throws Exception { testSubResults(true, 500L, 100); } @Test - public void testSubResultsFalsePause() throws Exception { + void testSubResultsFalsePause() throws Exception { testSubResults(false, 100); } @Test - public void testSubResultsFalsePauseThread() throws Exception { + void testSubResultsFalsePauseThread() throws Exception { testSubResults(false, 500L, 100); } @@ -204,7 +206,7 @@ public class TestSampleResult implements JMeterSerialTest { SampleResult parent = new SampleResult(nanoTime, nanoThreadSleep); JMeterTestCase.assertPrimitiveEquals(nanoTime, parent.useNanoTime); - assertEquals(nanoThreadSleep, parent.nanoThreadSleep); + Assertions.assertEquals(nanoThreadSleep, parent.nanoThreadSleep); long beginTest = parent.currentTimeInMillis(); @@ -226,11 +228,11 @@ public class TestSampleResult implements JMeterSerialTest { child1.sampleEnd(); long child1Elapsed = child1.getTime(); - assertTrue(child1.isSuccessful()); - assertEquals(100, child1.getBytesAsLong()); - assertEquals("Child1 Sample", child1.getSampleLabel()); - assertEquals(1, child1.getSampleCount()); - assertEquals(0, child1.getSubResults().length); + Assertions.assertTrue(child1.isSuccessful()); + Assertions.assertEquals(100, child1.getBytesAsLong()); + Assertions.assertEquals("Child1 Sample", child1.getSampleLabel()); + Assertions.assertEquals(1, child1.getSampleCount()); + Assertions.assertEquals(0, child1.getSubResults().length); long actualPause = 0; if (pause > 0) { @@ -249,20 +251,20 @@ public class TestSampleResult implements JMeterSerialTest { child2.sampleEnd(); long child2Elapsed = child2.getTime(); - assertTrue(child2.isSuccessful()); - assertEquals(200, child2.getBytesAsLong()); - assertEquals("Child2 Sample", child2.getSampleLabel()); - assertEquals(1, child2.getSampleCount()); - assertEquals(0, child2.getSubResults().length); + Assertions.assertTrue(child2.isSuccessful()); + Assertions.assertEquals(200, child2.getBytesAsLong()); + Assertions.assertEquals("Child2 Sample", child2.getSampleLabel()); + Assertions.assertEquals(1, child2.getSampleCount()); + Assertions.assertEquals(0, child2.getSubResults().length); // Now add the subsamples to the sample parent.addSubResult(child1); parent.addSubResult(child2); - assertTrue(parent.isSuccessful()); - assertEquals(600, parent.getBytesAsLong()); - assertEquals("Parent Sample", parent.getSampleLabel()); - assertEquals(1, parent.getSampleCount()); - assertEquals(2, parent.getSubResults().length); + Assertions.assertTrue(parent.isSuccessful()); + Assertions.assertEquals(600, parent.getBytesAsLong()); + Assertions.assertEquals("Parent Sample", parent.getSampleLabel()); + Assertions.assertEquals(1, parent.getSampleCount()); + Assertions.assertEquals(2, parent.getSubResults().length); long parentElapsedTotal = parent.getTime(); long overallTime = parent.currentTimeInMillis() - beginTest; @@ -277,7 +279,7 @@ public class TestSampleResult implements JMeterSerialTest { long diff = parentElapsedTotal - sumSamplesTimes; long maxDiff = nanoTime ? 10 : 16; // TimeMillis has granularity of 10-20 if (diff < 0 || diff > maxDiff) { - fail("ParentElapsed: " + parentElapsedTotal + " - " + " sum(samples): " + sumSamplesTimes + Assertions.fail("ParentElapsed: " + parentElapsedTotal + " - " + " sum(samples): " + sumSamplesTimes + " => " + diff + " not in [0," + maxDiff + "]; nanotime=" + nanoTime); } @@ -286,53 +288,53 @@ public class TestSampleResult implements JMeterSerialTest { diff = overallTime - parentElapsedTotal; if (diff < 0 || diff > maxDiff) { - fail("TestElapsed: " + overallTime + " - " + " ParentElapsed: " + parentElapsedTotal + Assertions.fail("TestElapsed: " + overallTime + " - " + " ParentElapsed: " + parentElapsedTotal + " => " + diff + " not in [0," + maxDiff + "]; nanotime=" + nanoTime); } // Check that calculator gets the correct statistics from the sample Calculator calculator = new Calculator(); calculator.addSample(parent); - assertEquals(600, calculator.getTotalBytes()); - assertEquals(1, calculator.getCount()); - assertEquals(1d / (parentElapsedTotal / 1000d), calculator.getRate(), 0.0001d); // Allow for some margin of error + Assertions.assertEquals(600, calculator.getTotalBytes()); + Assertions.assertEquals(1, calculator.getCount()); + Assertions.assertEquals(1d / (parentElapsedTotal / 1000d), calculator.getRate(), 0.0001d); // Allow for some margin of error // Check that the throughput uses the time elapsed for the sub results - assertFalse(1d / (parentElapsed / 1000d) <= calculator.getRate()); + Assertions.assertFalse(1d / (parentElapsed / 1000d) <= calculator.getRate()); } // TODO some more invalid sequence tests needed @Test - public void testEncodingAndType() throws Exception { + void testEncodingAndType() throws Exception { // check default SampleResult res = new SampleResult(); - assertEquals(SampleResult.DEFAULT_ENCODING, res.getDataEncodingWithDefault()); - assertEquals("DataType should be blank", "", res.getDataType()); - assertNull(res.getDataEncodingNoDefault()); + Assertions.assertEquals(SampleResult.DEFAULT_ENCODING, res.getDataEncodingWithDefault()); + Assertions.assertEquals("", res.getDataType(), "DataType should be blank"); + Assertions.assertNull(res.getDataEncodingNoDefault()); // check null changes nothing res.setEncodingAndType(null); - assertEquals(SampleResult.DEFAULT_ENCODING, res.getDataEncodingWithDefault()); - assertEquals("DataType should be blank", "", res.getDataType()); - assertNull(res.getDataEncodingNoDefault()); + Assertions.assertEquals(SampleResult.DEFAULT_ENCODING, res.getDataEncodingWithDefault()); + Assertions.assertEquals("", res.getDataType(), "DataType should be blank"); + Assertions.assertNull(res.getDataEncodingNoDefault()); // check no charset res.setEncodingAndType("text/html"); - assertEquals(SampleResult.DEFAULT_ENCODING, res.getDataEncodingWithDefault()); - assertEquals("text", res.getDataType()); - assertNull(res.getDataEncodingNoDefault()); + Assertions.assertEquals(SampleResult.DEFAULT_ENCODING, res.getDataEncodingWithDefault()); + Assertions.assertEquals("text", res.getDataType()); + Assertions.assertNull(res.getDataEncodingNoDefault()); // Check unquoted charset res.setEncodingAndType("text/html; charset=aBcd"); - assertEquals("aBcd", res.getDataEncodingWithDefault()); - assertEquals("aBcd", res.getDataEncodingNoDefault()); - assertEquals("text", res.getDataType()); + Assertions.assertEquals("aBcd", res.getDataEncodingWithDefault()); + Assertions.assertEquals("aBcd", res.getDataEncodingNoDefault()); + Assertions.assertEquals("text", res.getDataType()); // Check quoted charset res.setEncodingAndType("text/html; charset=\"aBCd\""); - assertEquals("aBCd", res.getDataEncodingWithDefault()); - assertEquals("aBCd", res.getDataEncodingNoDefault()); - assertEquals("text", res.getDataType()); + Assertions.assertEquals("aBCd", res.getDataEncodingWithDefault()); + Assertions.assertEquals("aBCd", res.getDataEncodingNoDefault()); + Assertions.assertEquals("text", res.getDataType()); } // sleep and return how long we actually slept @@ -344,7 +346,7 @@ public class TestSampleResult implements JMeterSerialTest { } @Test - public void testCompareSampleLabels() { + void testCompareSampleLabels() { final boolean prevValue = TestPlan.getFunctionalMode(); TestPlan plan = new TestPlan(); plan.setFunctionalMode(true); @@ -358,25 +360,25 @@ public class TestSampleResult implements JMeterSerialTest { subResult.setSampleLabel("subResult label"); result.addSubResult(subResult); - assertEquals("subResult label", subResult.getSampleLabel()); + Assertions.assertEquals("subResult label", subResult.getSampleLabel()); plan.setFunctionalMode(false); subResult.setSampleLabel("parent label"); result.addRawSubResult(subResult); - assertEquals("parent label-0", subResult.getSampleLabel()); + Assertions.assertEquals("parent label-0", subResult.getSampleLabel()); } finally { plan.setFunctionalMode(prevValue); } } @Test - public void testBug63433() { + void testBug63433() { SampleResult firstResult = new SampleResult(); - assertFalse("Expected false on first call of markFile", firstResult.markFile("result.csv")); - assertTrue("Expected true on second call of markFile", firstResult.markFile("result.csv")); + Assertions.assertFalse(firstResult.markFile("result.csv"), "Expected false on first call of markFile"); + Assertions.assertTrue(firstResult.markFile("result.csv"), "Expected true on second call of markFile"); SampleResult secondResult = new SampleResult(); - assertFalse("Expected false on first call of markFile with null", secondResult.markFile(null)); - assertTrue("Expected true on second call of markFile with null", secondResult.markFile(null)); + Assertions.assertFalse(secondResult.markFile(null), "Expected false on first call of markFile with null"); + Assertions.assertTrue(secondResult.markFile(null), "Expected true on second call of markFile with null"); } }
