jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fschumac...@apache.org
Subject svn commit: r1816321 - in /jmeter/trunk/test/src/org/apache/jmeter: control/ junit/spock/ protocol/jdbc/ protocol/jdbc/sampler/ timers/
Date Sat, 25 Nov 2017 16:51:45 GMT
Author: fschumacher
Date: Sat Nov 25 16:51:45 2017
New Revision: 1816321

URL: http://svn.apache.org/viewvc?rev=1816321&view=rev
Log:
Add first spock tests.

Part of pr #332 from github
Contributed by Graham Russell

Added:
    jmeter/trunk/test/src/org/apache/jmeter/control/RunTimeSpec.groovy   (with props)
    jmeter/trunk/test/src/org/apache/jmeter/junit/spock/
    jmeter/trunk/test/src/org/apache/jmeter/junit/spock/JMeterSpec.groovy   (with props)
    jmeter/trunk/test/src/org/apache/jmeter/protocol/jdbc/
    jmeter/trunk/test/src/org/apache/jmeter/protocol/jdbc/sampler/
    jmeter/trunk/test/src/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerSpec.groovy 
 (with props)
    jmeter/trunk/test/src/org/apache/jmeter/timers/UniformRandomTimerSpec.groovy   (with props)
Removed:
    jmeter/trunk/test/src/org/apache/jmeter/control/TestRunTime.java

Added: jmeter/trunk/test/src/org/apache/jmeter/control/RunTimeSpec.groovy
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/control/RunTimeSpec.groovy?rev=1816321&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/control/RunTimeSpec.groovy (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/control/RunTimeSpec.groovy Sat Nov 25 16:51:45
2017
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.control
+
+import org.apache.jmeter.junit.stubs.TestSampler
+import org.apache.jmeter.samplers.Sampler
+import spock.lang.Specification
+
+class RunTimeSpec extends Specification {
+
+    def sut = new RunTime()
+
+    def "RunTime stops within a tolerance after specified runtime"() {
+        given:
+            sut.setRuntime(1)
+
+            def runTimeMillis = 1000
+            def expectedLoops = 5
+            def tolerance = Math.ceil(0.1f * expectedLoops)
+            int samplerWaitTime = runTimeMillis / expectedLoops
+            TestSampler samp1 = new TestSampler("Sample 1", samplerWaitTime)
+            TestSampler samp2 = new TestSampler("Sample 2", samplerWaitTime)
+
+            def sampler1Loops = 2
+            LoopController loop1 = new LoopController()
+            loop1.setLoops(sampler1Loops)
+            loop1.setContinueForever(false)
+            loop1.addTestElement(samp1)
+
+            LoopController loop2 = new LoopController()
+            loop2.setLoops(expectedLoops * 2)
+            loop2.setContinueForever(false)
+            loop2.addTestElement(samp2)
+
+            sut.addTestElement(loop1)
+            sut.addTestElement(loop2)
+            sut.setRunningVersion(true)
+            loop1.setRunningVersion(true)
+            loop2.setRunningVersion(true)
+            sut.initialize()
+        when:
+            def sampler
+            int loopCount = 0
+            long now = System.currentTimeMillis()
+            while ((sampler = sut.next()) != null) {
+                loopCount++
+                sampler.sample(null)
+            }
+            long elapsed = System.currentTimeMillis() - now
+        then:
+            sut.getIterCount() == 1
+            loopCount >= expectedLoops
+            loopCount <= expectedLoops + tolerance
+            elapsed >= runTimeMillis
+            elapsed <= runTimeMillis + (tolerance * samplerWaitTime)
+            samp1.getSamples() == sampler1Loops
+            samp2.getSamples() >= expectedLoops - sampler1Loops
+            samp2.getSamples() <= expectedLoops - sampler1Loops + tolerance
+    }
+
+    def "Immediately returns null when runtime is set to 0"() {
+        given:
+            sut.setRuntime(0)
+            sut.addTestElement(Mock(Sampler))
+        when:
+            def sampler = sut.next()
+        then:
+            sampler == null
+    }
+
+    def "Immediately returns null if only Controllers are present"() {
+        given:
+            sut.setRuntime(10)
+            sut.addTestElement(Mock(Controller))
+            sut.addTestElement(Mock(Controller))
+        when:
+            def sampler = sut.next()
+        then:
+            sampler == null
+    }
+    def "within time limit samplers are returned until empty"() {
+        given:
+            def mockSampler = Mock(Sampler)
+            sut.setRuntime(10)
+            sut.addTestElement(mockSampler)
+            sut.addTestElement(mockSampler)
+        when:
+            def samplers = [sut.next(), sut.next(), sut.next()]
+        then:
+            samplers == [mockSampler, mockSampler, null]
+    }
+
+    def "RunTime immediately returns null when there are no test elements"() {
+        given:
+            sut.setRuntime(10)
+        when:
+            def sampler = sut.next()
+        then:
+            sampler == null
+    }
+
+}

Propchange: jmeter/trunk/test/src/org/apache/jmeter/control/RunTimeSpec.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jmeter/trunk/test/src/org/apache/jmeter/junit/spock/JMeterSpec.groovy
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/junit/spock/JMeterSpec.groovy?rev=1816321&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/junit/spock/JMeterSpec.groovy (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/junit/spock/JMeterSpec.groovy Sat Nov 25 16:51:45
2017
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License") you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.junit.spock
+
+import org.apache.jmeter.util.JMeterUtils
+import spock.lang.Specification
+
+import java.nio.charset.Charset
+
+/**
+ * Common setup for Spock test cases
+ */
+abstract class JMeterSpec extends Specification {
+
+    // Used by findTestFile
+    private static final String filePrefix
+
+    /*
+    * If not running under AllTests.java, make sure that the properties (and
+    * log file) are set up correctly.
+    *
+    * N.B. This assumes the JUnit test are executed in the
+    * project root, bin directory or one level down, and all the JMeter jars
+    * (plus any others needed at run-time) need to be on the classpath.
+    */
+    static {
+        if (JMeterUtils.getJMeterProperties() == null) {
+            String file = "jmeter.properties"
+            File f = new File(file)
+            if (!f.canRead()) {
+                System.out.println("Can't find " + file + " - trying bin directory")
+                if (!new File("bin/" + file).canRead()) {
+                    // When running tests inside IntelliJ
+                    System.out.println("Can't find " + file + " - trying ../bin directory")
+                    filePrefix = "../bin/" // JMeterUtils assumes Unix-style separators
+                    file = filePrefix + file
+                } else {
+                    filePrefix = "bin/" // JMeterUtils assumes Unix-style separators
+                    file = filePrefix + file
+                }
+            } else {
+                filePrefix = ""
+            }
+            // Used to be done in initializeProperties
+            String home = new File(System.getProperty("user.dir"), filePrefix).getParent()
+            System.out.println("Setting JMeterHome: " + home)
+            JMeterUtils.setJMeterHome(home)
+            System.setProperty("jmeter.home", home) // needed for scripts
+            JMeterUtils jmu = new JMeterUtils()
+            try {
+                jmu.initializeProperties(file)
+            } catch (MissingResourceException e) {
+                System.out.println("** Can't find resources - continuing anyway **")
+            }
+            System.out.println("JMeterVersion=" + JMeterUtils.getJMeterVersion())
+            logprop("java.version")
+            logprop("java.vm.name")
+            logprop("java.vendor")
+            logprop("java.home")
+            logprop("file.encoding")
+            // Display actual encoding used (will differ if file.encoding is not recognised)
+            System.out.println("default encoding=" + Charset.defaultCharset())
+            logprop("user.home")
+            logprop("user.dir")
+            logprop("user.language")
+            logprop("user.region")
+            logprop("user.country")
+            logprop("user.variant")
+            System.out.println("Locale=" + Locale.getDefault().toString())
+            logprop("java.class.version")
+            logprop("java.awt.headless")
+            logprop("os.name")
+            logprop("os.version")
+            logprop("os.arch")
+            logprop("java.class.path")
+        } else {
+            filePrefix = ""
+        }
+    }
+
+    private static void logprop(String prop) {
+        System.out.println(prop + "=" + System.getProperty(prop))
+    }
+
+}

Propchange: jmeter/trunk/test/src/org/apache/jmeter/junit/spock/JMeterSpec.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jmeter/trunk/test/src/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerSpec.groovy
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerSpec.groovy?rev=1816321&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerSpec.groovy (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerSpec.groovy Sat
Nov 25 16:51:45 2017
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.protocol.jdbc.sampler
+
+import org.apache.jmeter.config.ConfigTestElement
+import org.apache.jmeter.junit.spock.JMeterSpec
+import org.apache.jmeter.samplers.SampleResult
+import org.apache.jmeter.testelement.property.JMeterProperty
+import spock.lang.Unroll
+
+import java.sql.Connection
+import java.sql.ResultSet
+import java.sql.ResultSetMetaData
+import java.sql.SQLException
+import java.sql.Statement
+
+@Unroll
+class JDBCSamplerSpec extends JMeterSpec {
+
+    def sut = new JDBCSampler()
+
+    def "applies matches SimpleConfigGui"() {
+        given:
+            def mockConfig = Mock(ConfigTestElement)
+            def mockProperty = Mock(JMeterProperty)
+        when:
+            def applies = sut.applies(mockConfig)
+        then:
+            1 * mockConfig.getProperty(_ as String) >> mockProperty
+            1 * mockProperty.getStringValue() >> propertyValue
+            applies == expectedApplies
+            // this check will catch any future additions
+            sut.APPLIABLE_CONFIG_CLASSES.size() == 1
+        where:
+            propertyValue                                  | expectedApplies
+            "org.apache.jmeter.config.gui.SimpleConfigGui" | true
+            "org.apache.jmeter.config.gui.SomethingElse"   | false
+    }
+
+    /* AbstractJDBCTestElement tests */
+
+    def "execute with SELECT query"() {
+        given:
+            def conn = Mock(Connection)
+            def sample = Mock(SampleResult)
+            def stmt = Mock(Statement)
+            def rs = Mock(ResultSet)
+            def meta = Mock(ResultSetMetaData)
+            sut.setQuery("SELECT")
+        when:
+            def response = sut.execute(conn, sample)
+        then:
+            1 * conn.createStatement() >> stmt
+            1 * stmt.setQueryTimeout(0)
+            1 * stmt.executeQuery(_ as String) >> rs
+            1 * sample.latencyEnd()
+            // getStringFromResultSet
+            1 * rs.getMetaData() >> meta
+            1 * rs.next()
+            1 * rs.close() >> { throw new SQLException() }
+            1 * stmt.close()
+            // 1 * conn.close() // closed by JDBCSampler
+            1 * meta.getColumnCount() >> 0
+            response == [] as byte[]
+    }
+
+    def "Catches SQLException during Connection closing"() {
+        given:
+            def mockConnection = Mock(Connection)
+        when:
+            sut.close(mockConnection)
+        then:
+            1 * mockConnection.close() >> { throw new SQLException() }
+            noExceptionThrown()
+    }
+
+    def "Catches SQLException during Statement closing"() {
+        given:
+            def mockStatement = Mock(Statement)
+        when:
+            sut.close(mockStatement)
+        then:
+            1 * mockStatement.close() >> { throw new SQLException() }
+            noExceptionThrown()
+    }
+
+    def "Catches SQLException during ResultSet closing"() {
+        given:
+            def mockResultSet = Mock(ResultSet)
+        when:
+            sut.close(mockResultSet)
+        then:
+            1 * mockResultSet.close() >> { throw new SQLException() }
+            noExceptionThrown()
+    }
+
+    def "getIntegerQueryTimeout returns #expectedTimeout from #initialTimeout"() {
+        given:
+            sut.setQueryTimeout(initialTimeout)
+        when:
+            def timeout = sut.getIntegerQueryTimeout()
+        then:
+            timeout == expectedTimeout
+        where:
+            initialTimeout | expectedTimeout
+            "0"            | 0
+            "1"            | 1
+            "2147483647"   | Integer.MAX_VALUE
+            "-1"           | -1
+            "-2147483648"  | Integer.MIN_VALUE
+            "2147483648"   | 0 // max int + 1
+            "-2147483649"  | 0 // min int - 1
+            "nan"          | 0
+            ""             | 0
+    }
+}

Propchange: jmeter/trunk/test/src/org/apache/jmeter/protocol/jdbc/sampler/JDBCSamplerSpec.groovy
------------------------------------------------------------------------------
    svn:eol-style = native

Added: jmeter/trunk/test/src/org/apache/jmeter/timers/UniformRandomTimerSpec.groovy
URL: http://svn.apache.org/viewvc/jmeter/trunk/test/src/org/apache/jmeter/timers/UniformRandomTimerSpec.groovy?rev=1816321&view=auto
==============================================================================
--- jmeter/trunk/test/src/org/apache/jmeter/timers/UniformRandomTimerSpec.groovy (added)
+++ jmeter/trunk/test/src/org/apache/jmeter/timers/UniformRandomTimerSpec.groovy Sat Nov 25
16:51:45 2017
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jmeter.timers
+
+import spock.lang.Specification
+import spock.lang.Unroll
+
+class UniformRandomTimerSpec extends Specification {
+
+    def sut = new UniformRandomTimer()
+
+    def "default delay is 0"() {
+        given:
+            sut.iterationStart(null)
+        when:
+            def computedDelay = sut.delay()
+        then:
+            computedDelay == 0L
+    }
+
+    def "default range is 0"() {
+        given:
+            sut.setDelay("1")
+            sut.iterationStart(null)
+        when:
+            def computedDelay = sut.delay()
+        then:
+            computedDelay == 1L
+    }
+
+    @Unroll
+    def "#delay <= computedDelay <= trunc(#delay + abs(#range))"() {
+        given:
+            sut.setDelay(delay)
+            sut.setRange(range)
+            sut.iterationStart(null)
+        when:
+            def computedDelay = sut.delay()
+        then:
+            min <= computedDelay
+            computedDelay <= max
+        where:
+            delay | range | min | max
+            "1"   | 10.5  | 1   | 11
+            "1"   | 0.1   | 1   | 1
+            "0"   | -50.0 | 0   | 50
+    }
+}

Propchange: jmeter/trunk/test/src/org/apache/jmeter/timers/UniformRandomTimerSpec.groovy
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message