From 07aead3ca41ed8ae392a11a368cf1b2d94a66b4a Mon Sep 17 00:00:00 2001 From: mcilwain Date: Tue, 26 Jun 2018 06:48:10 -0700 Subject: [PATCH] Increase RDAP unit test speed There's no reason to have to create 2,500 simulated database entities for a single test (which makes it take a really long time). Better to just set the relevant limit to be lower for testing purposes. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=202120722 --- .../registry/rdap/RdapDomainSearchAction.java | 18 +++++---- .../rdap/RdapDomainSearchActionTest.java | 40 +++++++++---------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/java/google/registry/rdap/RdapDomainSearchAction.java b/java/google/registry/rdap/RdapDomainSearchAction.java index c23c4a864..cc6ae752c 100644 --- a/java/google/registry/rdap/RdapDomainSearchAction.java +++ b/java/google/registry/rdap/RdapDomainSearchAction.java @@ -47,6 +47,7 @@ import google.registry.request.Parameter; import google.registry.request.auth.Auth; import google.registry.util.Clock; import google.registry.util.Idn; +import google.registry.util.NonFinalForTesting; import java.net.InetAddress; import java.util.ArrayList; import java.util.Comparator; @@ -73,11 +74,12 @@ import org.joda.time.DateTime; ) public class RdapDomainSearchAction extends RdapSearchActionBase { - public static final String PATH = "/rdap/domains"; + static final String PATH = "/rdap/domains"; - public static final int RESULT_SET_SIZE_SCALING_FACTOR = 30; + static final int RESULT_SET_SIZE_SCALING_FACTOR = 30; - public static final int MAX_NAMESERVERS_IN_FIRST_STAGE = 300; + @NonFinalForTesting + static int maxNameserversInFirstStage = 300; private static final FluentLogger logger = FluentLogger.forEnclosingClass(); @@ -319,7 +321,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase { // must be present, to avoid querying every host in the system. This restriction is enforced by // {@link queryItems}. // - // Only return the first MAX_NAMESERVERS_IN_FIRST_STAGE nameservers. This could result in an + // Only return the first maxNameserversInFirstStage nameservers. This could result in an // incomplete result set if a search asks for something like "ns*", but we need to enforce a // limit in order to avoid arbitrarily long-running queries. Query query = @@ -328,7 +330,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase { "fullyQualifiedHostName", partialStringQuery, DeletedItemHandling.EXCLUDE, - MAX_NAMESERVERS_IN_FIRST_STAGE); + maxNameserversInFirstStage); Optional desiredRegistrar = getDesiredRegistrar(); if (desiredRegistrar.isPresent()) { query = query.filter("currentSponsorClientId", desiredRegistrar.get()); @@ -416,7 +418,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase { *

In theory, we could have any number of hosts using the same IP address. To make sure we get * all the associated domains, we have to retrieve all of them, and use them to look up domains. * This could open us up to a kind of DoS attack if huge number of hosts are defined on a single - * IP. To avoid this, fetch only the first {@link #MAX_NAMESERVERS_IN_FIRST_STAGE} nameservers. In + * IP. To avoid this, fetch only the first {@link #maxNameserversInFirstStage} nameservers. In * all normal circumstances, this should be orders of magnitude more than there actually are. But * it could result in us missing some domains. * @@ -433,7 +435,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase { Optional.empty(), Optional.empty(), DeletedItemHandling.EXCLUDE, - MAX_NAMESERVERS_IN_FIRST_STAGE); + maxNameserversInFirstStage); Optional desiredRegistrar = getDesiredRegistrar(); if (desiredRegistrar.isPresent()) { query = query.filter("currentSponsorClientId", desiredRegistrar.get()); @@ -494,7 +496,7 @@ public class RdapDomainSearchAction extends RdapSearchActionBase { // so, indicate the result might be incomplete. return makeSearchResults( domains, - (numHostKeysSearched >= MAX_NAMESERVERS_IN_FIRST_STAGE) + (numHostKeysSearched >= maxNameserversInFirstStage) ? IncompletenessWarningType.MIGHT_BE_INCOMPLETE : IncompletenessWarningType.COMPLETE, (numHostKeysSearched > 0) ? Optional.of((long) domains.size()) : Optional.empty(), diff --git a/javatests/google/registry/rdap/RdapDomainSearchActionTest.java b/javatests/google/registry/rdap/RdapDomainSearchActionTest.java index 641306270..b2bb88b84 100644 --- a/javatests/google/registry/rdap/RdapDomainSearchActionTest.java +++ b/javatests/google/registry/rdap/RdapDomainSearchActionTest.java @@ -87,13 +87,9 @@ import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class RdapDomainSearchActionTest extends RdapSearchActionTestCase { - @Rule - public final AppEngineRule appEngine = AppEngineRule.builder() - .withDatastore() - .build(); + @Rule public final AppEngineRule appEngine = AppEngineRule.builder().withDatastore().build(); - @Rule - public final InjectRule inject = new InjectRule(); + @Rule public final InjectRule inject = new InjectRule(); private final HttpServletRequest request = mock(HttpServletRequest.class); private final FakeClock clock = new FakeClock(DateTime.parse("2000-01-01T00:00:00Z")); @@ -176,6 +172,7 @@ public class RdapDomainSearchActionTest extends RdapSearchActionTestCase { @Before public void setUp() { + RdapDomainSearchAction.maxNameserversInFirstStage = 40; inject.setStaticField(Ofy.class, "clock", clock); // cat.lol and cat2.lol @@ -1761,16 +1758,16 @@ public class RdapDomainSearchActionTest extends RdapSearchActionTestCase { @Test public void testNameserverMatchManyNameserversForTheSameDomainsWithSuffix() { - // Same as above, except that we find all 40 nameservers because of the wildcard. But we + // Same as above, except that we find all 39 nameservers because of the wildcard. But we // should still only return 3 domains, because we merge duplicate domains together in a set. // Since we fetch domains by nameserver in batches of 30 nameservers, we need to make sure to // have more than that number of nameservers for an effective test. - createManyDomainsAndHosts(3, 1, 40); + createManyDomainsAndHosts(3, 1, 39); rememberWildcardType("ns*.domain1.lol"); Object obj = generateActualJson(RequestType.NS_LDH_NAME, "ns*.domain1.lol"); assertThat(response.getStatus()).isEqualTo(200); checkNumberOfDomainsInResult(obj, 3); - verifyMetrics(SearchType.BY_NAMESERVER_NAME, Optional.of(3L), Optional.of(40L)); + verifyMetrics(SearchType.BY_NAMESERVER_NAME, Optional.of(3L), Optional.of(39L)); } @Test @@ -1827,37 +1824,36 @@ public class RdapDomainSearchActionTest extends RdapSearchActionTestCase { @Test public void testNameserverMatch_duplicatesNotTruncated() { - // 60 nameservers for each of 4 domains; these should translate into 2 30-nameserver domain - // fetches, which should _not_ trigger the truncation warning because all the domains will be - // duplicates. - createManyDomainsAndHosts(4, 1, 60); + // 36 nameservers for each of 4 domains; these should translate into two fetches, which should + // not trigger the truncation warning because all the domains will be duplicates. + createManyDomainsAndHosts(4, 1, 36); rememberWildcardType("ns*.domain1.lol"); assertThat(generateActualJson(RequestType.NS_LDH_NAME, "ns*.domain1.lol")) .isEqualTo(readMultiDomainFile( "rdap_nontruncated_domains.json", "domain1.lol", - "BA-LOL", + "8A-LOL", "domain2.lol", - "B9-LOL", + "89-LOL", "domain3.lol", - "B8-LOL", + "88-LOL", "domain4.lol", - "B7-LOL")); + "87-LOL")); assertThat(response.getStatus()).isEqualTo(200); - verifyMetrics(SearchType.BY_NAMESERVER_NAME, Optional.of(4L), Optional.of(60L)); + verifyMetrics(SearchType.BY_NAMESERVER_NAME, Optional.of(4L), Optional.of(36L)); } @Test public void testNameserverMatch_incompleteResultsSet() { - createManyDomainsAndHosts(2, 1, 2500); + createManyDomainsAndHosts(2, 1, 41); rememberWildcardType("ns*.domain1.lol"); assertThat(generateActualJson(RequestType.NS_LDH_NAME, "ns*.domain1.lol")) .isEqualTo(readMultiDomainFile( "rdap_incomplete_domains.json", "domain1.lol", - "13C8-LOL", + "92-LOL", "domain2.lol", - "13C7-LOL", + "91-LOL", "x", "x", "x", @@ -1866,7 +1862,7 @@ public class RdapDomainSearchActionTest extends RdapSearchActionTestCase { verifyMetrics( SearchType.BY_NAMESERVER_NAME, Optional.of(2L), - Optional.of(2500L), + Optional.of(41L), IncompletenessWarningType.MIGHT_BE_INCOMPLETE); }