jmeter-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmoua...@apache.org
Subject [jmeter] branch master updated: Speed up DNSCacheManagerSpec + simplify some code in DNSCacheManager. (#580)
Date Sat, 25 Apr 2020 08:59:42 GMT
This is an automated email from the ASF dual-hosted git repository.

pmouawad pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 9fedd18  Speed up DNSCacheManagerSpec + simplify some code in DNSCacheManager. (#580)
9fedd18 is described below

commit 9fedd185d3f1b541e6095977fd755449bed71a2a
Author: Graham Russell <graham@ham1.co.uk>
AuthorDate: Sat Apr 25 09:59:33 2020 +0100

    Speed up DNSCacheManagerSpec + simplify some code in DNSCacheManager. (#580)
    
    * Speed up DNSCacheManagerSpec + simplify some code in DNSCacheManager.
    
    * Remove printnln
---
 .../protocol/http/control/DNSCacheManager.java     |  8 ++--
 .../http/control/DNSCacheManagerSpec.groovy        | 53 +++++++++++-----------
 2 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/DNSCacheManager.java
b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/DNSCacheManager.java
index b65de82..11c66be 100644
--- a/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/DNSCacheManager.java
+++ b/src/protocol/http/src/main/java/org/apache/jmeter/protocol/http/control/DNSCacheManager.java
@@ -35,7 +35,6 @@ import org.apache.jmeter.testelement.property.BooleanProperty;
 import org.apache.jmeter.testelement.property.CollectionProperty;
 import org.apache.jmeter.testelement.property.JMeterProperty;
 import org.apache.jmeter.testelement.property.NullProperty;
-import org.apache.jmeter.testelement.property.PropertyIterator;
 import org.apache.jmeter.testelement.property.TestElementProperty;
 import org.apache.jmeter.threads.JMeterContextService;
 import org.slf4j.Logger;
@@ -184,9 +183,8 @@ public class DNSCacheManager extends ConfigTestElement implements TestIterationL
             return false;
         }
         CollectionProperty property = (CollectionProperty) p;
-        PropertyIterator iterator = property.iterator();
-        while (iterator.hasNext()) {
-            TestElementProperty possibleEntry = (TestElementProperty) iterator.next();
+        for (JMeterProperty jMeterProperty : property) {
+            TestElementProperty possibleEntry = (TestElementProperty) jMeterProperty;
             if (log.isDebugEnabled()) {
                 log.debug("Look for {} at {}: {}", host, possibleEntry.getObjectValue(),
possibleEntry.getObjectValue().getClass());
             }
@@ -210,7 +208,7 @@ public class DNSCacheManager extends ConfigTestElement implements TestIterationL
         }
         CollectionProperty property = (CollectionProperty) p;
         for (JMeterProperty jMeterProperty : property) {
-            StaticHost entry = (StaticHost) ((TestElementProperty) jMeterProperty).getObjectValue();
+            StaticHost entry = (StaticHost) jMeterProperty.getObjectValue();
             if (!entry.getName().equals(host)) {
                 continue; // try the next property
             }
diff --git a/src/protocol/http/src/test/groovy/org/apache/jmeter/protocol/http/control/DNSCacheManagerSpec.groovy
b/src/protocol/http/src/test/groovy/org/apache/jmeter/protocol/http/control/DNSCacheManagerSpec.groovy
index 51de58f..0ff63dc 100644
--- a/src/protocol/http/src/test/groovy/org/apache/jmeter/protocol/http/control/DNSCacheManagerSpec.groovy
+++ b/src/protocol/http/src/test/groovy/org/apache/jmeter/protocol/http/control/DNSCacheManagerSpec.groovy
@@ -17,23 +17,25 @@
 
 package org.apache.jmeter.protocol.http.control
 
-import org.apache.jmeter.junit.spock.JMeterSpec
 import org.xbill.DNS.ExtendedResolver
 import org.xbill.DNS.ResolverConfig
 
 import spock.lang.IgnoreIf
+import spock.lang.Requires
+import spock.lang.Specification
 
-class DNSCacheManagerSpec extends JMeterSpec {
+class DNSCacheManagerSpec extends Specification {
 
-    private static final String[] VALID_DNS_SERVERS  = ResolverConfig.getCurrentConfig().servers()
+    private static final String[] VALID_DNS_SERVERS = ResolverConfig.getCurrentConfig().servers()
     private static final String INVALID_DNS_SERVER = "512.1.1.1"
+    private static final String VALID_HOSTNAME = "jmeter.apache.org"
 
-    static def localDNSResolverFailed() {
+    private static final boolean localDNSResolverOK = {
         try {
             new DNSCacheManager().resolve("apache.org")
-            return false
-        } catch (UnknownHostException uhe) {
             return true
+        } catch (UnknownHostException uhe) {
+            return false
         }
     }
 
@@ -66,16 +68,16 @@ class DNSCacheManagerSpec extends JMeterSpec {
                      InetAddress.getByName("1.2.3.4")].toArray()
     }
 
-    @IgnoreIf({ DNSCacheManagerSpec.localDNSResolverFailed() })
+    @Requires({ localDNSResolverOK })
     def "Clear removes custom resolver status and any added hosts"() {
         given:
             sut.setCustomResolver(true)
-            sut.addHost("apache.jmeter.org", "127.0.0.1")
+            sut.addHost(VALID_HOSTNAME, "127.0.0.1")
             sut.clear()
         expect:
             // uses real DNS server
-            sut.resolve("jmeter.apache.org").contains(InetAddress.getByName("jmeter.apache.org"))
-            !sut.resolve("jmeter.apache.org").contains(InetAddress.getByName("127.0.0.1"))
+            sut.resolve(VALID_HOSTNAME).contains(InetAddress.getByName(VALID_HOSTNAME))
+            !sut.resolve(VALID_HOSTNAME).contains(InetAddress.getByName("127.0.0.1"))
     }
 
     def "If using an invalid server resolve throws UnknownHostException"() {
@@ -83,17 +85,15 @@ class DNSCacheManagerSpec extends JMeterSpec {
             sut.addServer(INVALID_DNS_SERVER)
             sut.setCustomResolver(true)
         when:
-            sut.resolve("jmeter.apache.org")
+            sut.resolve(VALID_HOSTNAME)
         then:
             thrown(UnknownHostException)
             sut.resolver == null
             sut.initFailed
     }
 
-    @IgnoreIf({
-        (Boolean.getBoolean("skip.test_TestDNSCacheManager.testWithCustomResolverAnd1Server")
-                || DNSCacheManagerSpec.localDNSResolverFailed())
-    })
+    @IgnoreIf({ Boolean.getBoolean("skip.test_TestDNSCacheManager.testWithCustomResolverAnd1Server")
})
+    @Requires({ localDNSResolverOK })
     def "Valid DNS resolves and caches with custom resolve true"() {
         given:
             for (dns in VALID_DNS_SERVERS) {
@@ -102,7 +102,7 @@ class DNSCacheManagerSpec extends JMeterSpec {
             sut.setCustomResolver(true)
             sut.setTimeoutMs(5000)
         when:
-            sut.resolve("jmeter.apache.org")
+            sut.resolve(VALID_HOSTNAME)
         then:
             sut.resolver != null
             ((ExtendedResolver) sut.resolver).getResolvers().length == VALID_DNS_SERVERS.length
@@ -117,24 +117,23 @@ class DNSCacheManagerSpec extends JMeterSpec {
             sut.setCustomResolver(true)
             sut.setTimeoutMs(5000)
         when:
-            sut.cache.put("jmeter.apache.org", new InetAddress[0])
+            sut.cache.put(VALID_HOSTNAME, new InetAddress[0])
         then:
-            sut.resolve("jmeter.apache.org") == new InetAddress[0]
+            sut.resolve(VALID_HOSTNAME) == new InetAddress[0]
 
         when:
-            sut.cache.put("jmeter.apache.org", null)
+            sut.cache.put(VALID_HOSTNAME, null)
         then:
-            sut.resolve("jmeter.apache.org") == null
+            sut.resolve(VALID_HOSTNAME) == null
     }
 
-    @IgnoreIf({ DNSCacheManagerSpec.localDNSResolverFailed() })
+    @Requires({ localDNSResolverOK })
     def "set custom resolver but without an address should use system resolver"() {
         given:
             sut.setCustomResolver(true)
             sut.setTimeoutMs(5000)
         when:
-            // This will use Default System DNS resolver
-            sut.resolve("jmeter.apache.org")
+            sut.resolve(VALID_HOSTNAME)
         then:
             sut.resolver != null
             ((ExtendedResolver) sut.resolver).getResolvers().length == 0
@@ -147,14 +146,14 @@ class DNSCacheManagerSpec extends JMeterSpec {
             DNSCacheManager clone = (DNSCacheManager) sut.clone()
             clone.setTimeoutMs(5000)
         when:
-            clone.resolve("jmeter.apache.org")
+            clone.resolve(VALID_HOSTNAME)
         then:
             thrown(UnknownHostException)
             clone.resolver == sut.resolver
     }
 
-    @IgnoreIf({ DNSCacheManagerSpec.localDNSResolverFailed() })
-    def "Resolve Existing Host With System Default Dns Server"() {
+    @Requires({ localDNSResolverOK })
+    def "Resolve Existing Host With System Default DNS Server"() {
         given:
             sut.setCustomResolver(false)
         when:
@@ -165,7 +164,7 @@ class DNSCacheManagerSpec extends JMeterSpec {
             result.length > 0 // IPv4 and/or IPv6
     }
 
-    def "Resolve Non-existing Host With System Default Dns Server"() {
+    def "Resolve Non-existing Host With System Default DNS Server"() {
         given:
             sut.setCustomResolver(false)
         when:


Mime
View raw message