Add MemcacheHelper to simplify memcache usage in tests

And make two callsites use it.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=154189438
This commit is contained in:
cgoldfeder 2017-04-25 10:32:04 -07:00 committed by Ben McIlwain
parent 4553422ade
commit 51cfc12c1d
3 changed files with 49 additions and 10 deletions

View file

@ -14,7 +14,6 @@
package google.registry.flows.domain; package google.registry.flows.domain;
import static com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig.getLocalMemcacheService;
import static com.google.common.base.Predicates.equalTo; import static com.google.common.base.Predicates.equalTo;
import static com.google.common.io.BaseEncoding.base16; import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@ -24,11 +23,10 @@ import static google.registry.testing.DatastoreHelper.createTld;
import static google.registry.testing.DatastoreHelper.persistActiveContact; import static google.registry.testing.DatastoreHelper.persistActiveContact;
import static google.registry.testing.DatastoreHelper.persistActiveHost; import static google.registry.testing.DatastoreHelper.persistActiveHost;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.MemcacheHelper.clearMemcache;
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions; import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
import static google.registry.util.DatastoreServiceUtils.KEY_TO_KIND_FUNCTION; import static google.registry.util.DatastoreServiceUtils.KEY_TO_KIND_FUNCTION;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheFlushRequest;
import com.google.appengine.tools.development.LocalRpcService;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
@ -336,8 +334,7 @@ public class DomainApplicationInfoFlowTest
persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST); persistTestEntities(HostsState.HOSTS_EXIST, MarksState.NO_MARKS_EXIST);
// Clear out memcache and session cache so that we count actual Datastore calls. // Clear out memcache and session cache so that we count actual Datastore calls.
ofy().clearSessionCache(); ofy().clearSessionCache();
getLocalMemcacheService().flushAll( clearMemcache();
new LocalRpcService.Status(), MemcacheFlushRequest.newBuilder().build());
int numPreviousReads = RequestCapturingAsyncDatastoreService.getReads().size(); int numPreviousReads = RequestCapturingAsyncDatastoreService.getReads().size();
doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST); doSuccessfulTest("domain_info_sunrise_response.xml", HostsState.HOSTS_EXIST);
// Get all of the keys loaded in the flow, with each distinct load() call as a list of keys. // Get all of the keys loaded in the flow, with each distinct load() call as a list of keys.

View file

@ -14,7 +14,6 @@
package google.registry.flows.domain; package google.registry.flows.domain;
import static com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig.getLocalMemcacheService;
import static com.google.common.base.Predicates.equalTo; import static com.google.common.base.Predicates.equalTo;
import static com.google.common.io.BaseEncoding.base16; import static com.google.common.io.BaseEncoding.base16;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
@ -25,11 +24,10 @@ import static google.registry.testing.DatastoreHelper.newDomainResource;
import static google.registry.testing.DatastoreHelper.persistActiveContact; import static google.registry.testing.DatastoreHelper.persistActiveContact;
import static google.registry.testing.DatastoreHelper.persistActiveHost; import static google.registry.testing.DatastoreHelper.persistActiveHost;
import static google.registry.testing.DatastoreHelper.persistResource; import static google.registry.testing.DatastoreHelper.persistResource;
import static google.registry.testing.MemcacheHelper.clearMemcache;
import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions; import static google.registry.testing.TestDataHelper.loadFileWithSubstitutions;
import static google.registry.util.DatastoreServiceUtils.KEY_TO_KIND_FUNCTION; import static google.registry.util.DatastoreServiceUtils.KEY_TO_KIND_FUNCTION;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheFlushRequest;
import com.google.appengine.tools.development.LocalRpcService;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
@ -641,8 +639,7 @@ public class DomainInfoFlowTest extends ResourceFlowTestCase<DomainInfoFlow, Dom
persistTestEntities(false); persistTestEntities(false);
// Clear out memcache and session cache so that we count actual Datastore calls. // Clear out memcache and session cache so that we count actual Datastore calls.
ofy().clearSessionCache(); ofy().clearSessionCache();
getLocalMemcacheService().flushAll( clearMemcache();
new LocalRpcService.Status(), MemcacheFlushRequest.newBuilder().build());
int numPreviousReads = RequestCapturingAsyncDatastoreService.getReads().size(); int numPreviousReads = RequestCapturingAsyncDatastoreService.getReads().size();
doSuccessfulTest("domain_info_response.xml", false); doSuccessfulTest("domain_info_response.xml", false);
// Get all of the keys loaded in the flow, with each distinct load() call as a list of keys. // Get all of the keys loaded in the flow, with each distinct load() call as a list of keys.

View file

@ -0,0 +1,45 @@
// Copyright 2017 The Nomulus Authors. All Rights Reserved.
//
// Licensed 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 google.registry.testing;
import static com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig.getLocalMemcacheService;
import static google.registry.model.ofy.ObjectifyService.ofy;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheFlushRequest;
import com.google.appengine.tools.development.LocalRpcService;
import com.googlecode.objectify.Key;
/** Utilities for manipulating memcache in tests. */
public class MemcacheHelper {
/** Clear out memcache */
public static void clearMemcache() {
getLocalMemcacheService().flushAll(
new LocalRpcService.Status(), MemcacheFlushRequest.newBuilder().build());
}
/** Clears memcache, inserts the specific keys requested, and clears the session cache. */
public static void setMemcacheContents(Key<?>... keys) {
// Clear out the session cache. This is needed so that the calls to loadWithMemcache() below
// actually go to datastore and write to memcache instead of bottoming out at the session cache.
ofy().clearSessionCache();
clearMemcache();
// Load the entities we want to be in memcache. If an entity's type is not marked with @Cache it
// will be ignored.
ofy().loadWithMemcache().keys(keys).values();
// Clear out the session cache again, since it now contains the newly loaded types.
ofy().clearSessionCache();
}
}