mirror of
https://github.com/google/nomulus.git
synced 2025-08-05 09:21:49 +02:00
Migrate away from VoidWorks
This is one last hanging piece of work left over from last year's Java 8 migration. There's no functionality changes in this CL, just refactoring. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=201947600
This commit is contained in:
parent
7d3cb3d426
commit
d3364b0387
18 changed files with 349 additions and 502 deletions
|
@ -17,7 +17,6 @@ package google.registry.model.common;
|
|||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static google.registry.model.ofy.ObjectifyService.ofy;
|
||||
|
||||
import com.googlecode.objectify.VoidWork;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
|
@ -47,11 +46,10 @@ public class GaeUserIdConverterTest {
|
|||
|
||||
@Test
|
||||
public void testSuccess_inTransaction() {
|
||||
ofy().transactNew(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
assertThat(GaeUserIdConverter.convertEmailAddressToGaeUserId("example@example.com"))
|
||||
.matches("[0-9]+");
|
||||
}});
|
||||
ofy()
|
||||
.transactNew(
|
||||
() ->
|
||||
assertThat(GaeUserIdConverter.convertEmailAddressToGaeUserId("example@example.com"))
|
||||
.matches("[0-9]+"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -83,13 +83,13 @@ public class OfyTest {
|
|||
// This can't be initialized earlier because namespaces need the AppEngineRule to work.
|
||||
}
|
||||
|
||||
private void doBackupGroupRootTimestampInversionTest(VoidWork work) {
|
||||
private void doBackupGroupRootTimestampInversionTest(Runnable runnable) {
|
||||
DateTime groupTimestamp = ofy().load().key(someObject.getParent()).now()
|
||||
.getUpdateAutoTimestamp().getTimestamp();
|
||||
// Set the clock in Ofy to the same time as the backup group root's save time.
|
||||
Ofy ofy = new Ofy(new FakeClock(groupTimestamp));
|
||||
TimestampInversionException thrown =
|
||||
assertThrows(TimestampInversionException.class, () -> ofy.transact(work));
|
||||
assertThrows(TimestampInversionException.class, () -> ofy.transact(runnable));
|
||||
assertThat(thrown)
|
||||
.hasMessageThat()
|
||||
.contains(
|
||||
|
@ -101,20 +101,12 @@ public class OfyTest {
|
|||
|
||||
@Test
|
||||
public void testBackupGroupRootTimestampsMustIncreaseOnSave() {
|
||||
doBackupGroupRootTimestampInversionTest(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().save().entity(someObject);
|
||||
}});
|
||||
doBackupGroupRootTimestampInversionTest(() -> ofy().save().entity(someObject));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBackupGroupRootTimestampsMustIncreaseOnDelete() {
|
||||
doBackupGroupRootTimestampInversionTest(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().delete().entity(someObject);
|
||||
}});
|
||||
doBackupGroupRootTimestampInversionTest(() -> ofy().delete().entity(someObject));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -125,12 +117,9 @@ public class OfyTest {
|
|||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().save().entity(someObject);
|
||||
ofy().save().entity(someObject);
|
||||
}
|
||||
() -> {
|
||||
ofy().save().entity(someObject);
|
||||
ofy().save().entity(someObject);
|
||||
}));
|
||||
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
|
||||
}
|
||||
|
@ -143,12 +132,9 @@ public class OfyTest {
|
|||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().delete().entity(someObject);
|
||||
ofy().delete().entity(someObject);
|
||||
}
|
||||
() -> {
|
||||
ofy().delete().entity(someObject);
|
||||
ofy().delete().entity(someObject);
|
||||
}));
|
||||
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
|
||||
}
|
||||
|
@ -161,12 +147,9 @@ public class OfyTest {
|
|||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().save().entity(someObject);
|
||||
ofy().delete().entity(someObject);
|
||||
}
|
||||
() -> {
|
||||
ofy().save().entity(someObject);
|
||||
ofy().delete().entity(someObject);
|
||||
}));
|
||||
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
|
||||
}
|
||||
|
@ -179,12 +162,9 @@ public class OfyTest {
|
|||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().delete().entity(someObject);
|
||||
ofy().save().entity(someObject);
|
||||
}
|
||||
() -> {
|
||||
ofy().delete().entity(someObject);
|
||||
ofy().save().entity(someObject);
|
||||
}));
|
||||
assertThat(thrown).hasMessageThat().contains("Multiple entries with same key");
|
||||
}
|
||||
|
@ -193,15 +173,7 @@ public class OfyTest {
|
|||
public void testSavingKeyTwiceInOneCall() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().save().entities(someObject, someObject);
|
||||
}
|
||||
}));
|
||||
() -> ofy().transact(() -> ofy().save().entities(someObject, someObject)));
|
||||
}
|
||||
|
||||
/** Simple entity class with lifecycle callbacks. */
|
||||
|
@ -241,11 +213,7 @@ public class OfyTest {
|
|||
public void testLifecycleCallbacks_loadFromDatastore() {
|
||||
ofy().factory().register(LifecycleObject.class);
|
||||
final LifecycleObject object = new LifecycleObject();
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().save().entity(object).now();
|
||||
}});
|
||||
ofy().transact(() -> ofy().save().entity(object).now());
|
||||
assertThat(object.onSaveCalled).isTrue();
|
||||
ofy().clearSessionCache();
|
||||
assertThat(ofy().load().entity(object).now().onLoadCalled).isTrue();
|
||||
|
|
|
@ -22,7 +22,6 @@ import static google.registry.model.rde.RdeRevision.saveRevision;
|
|||
import static google.registry.testing.JUnitBackports.assertThrows;
|
||||
|
||||
import com.google.common.base.VerifyException;
|
||||
import com.googlecode.objectify.VoidWork;
|
||||
import google.registry.testing.AppEngineRule;
|
||||
import org.joda.time.DateTime;
|
||||
import org.junit.Rule;
|
||||
|
@ -51,17 +50,12 @@ public class RdeRevisionTest {
|
|||
|
||||
@Test
|
||||
public void testSaveRevision_objectDoesntExist_newRevisionIsZero_nextRevIsOne() {
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
saveRevision("despondency", DateTime.parse("1984-12-18TZ"), FULL, 0);
|
||||
}});
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
assertThat(getNextRevision("despondency", DateTime.parse("1984-12-18TZ"), FULL))
|
||||
.isEqualTo(1);
|
||||
}});
|
||||
ofy().transact(() -> saveRevision("despondency", DateTime.parse("1984-12-18TZ"), FULL, 0));
|
||||
ofy()
|
||||
.transact(
|
||||
() ->
|
||||
assertThat(getNextRevision("despondency", DateTime.parse("1984-12-18TZ"), FULL))
|
||||
.isEqualTo(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -72,12 +66,8 @@ public class RdeRevisionTest {
|
|||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
saveRevision("despondency", DateTime.parse("1984-12-18TZ"), FULL, 1);
|
||||
}
|
||||
}));
|
||||
() ->
|
||||
saveRevision("despondency", DateTime.parse("1984-12-18TZ"), FULL, 1)));
|
||||
assertThat(thrown).hasMessageThat().contains("object missing");
|
||||
}
|
||||
|
||||
|
@ -90,29 +80,19 @@ public class RdeRevisionTest {
|
|||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
saveRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL, 0);
|
||||
}
|
||||
}));
|
||||
() -> saveRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL, 0)));
|
||||
assertThat(thrown).hasMessageThat().contains("object already created");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSaveRevision_objectExistsAtZero_newRevisionIsOne_nextRevIsTwo() {
|
||||
save("melancholy", DateTime.parse("1984-12-18TZ"), FULL, 0);
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
saveRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL, 1);
|
||||
}});
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
assertThat(getNextRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL))
|
||||
.isEqualTo(2);
|
||||
}});
|
||||
ofy().transact(() -> saveRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL, 1));
|
||||
ofy()
|
||||
.transact(
|
||||
() ->
|
||||
assertThat(getNextRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL))
|
||||
.isEqualTo(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -124,12 +104,7 @@ public class RdeRevisionTest {
|
|||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
saveRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL, 2);
|
||||
}
|
||||
}));
|
||||
() -> saveRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL, 2)));
|
||||
assertThat(thrown).hasMessageThat().contains("should be at 1 ");
|
||||
}
|
||||
|
||||
|
@ -141,12 +116,8 @@ public class RdeRevisionTest {
|
|||
() ->
|
||||
ofy()
|
||||
.transact(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
saveRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL, -1);
|
||||
}
|
||||
}));
|
||||
() ->
|
||||
saveRevision("melancholy", DateTime.parse("1984-12-18TZ"), FULL, -1)));
|
||||
assertThat(thrown).hasMessageThat().contains("Negative revision");
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@ import com.google.common.collect.ImmutableMap;
|
|||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.common.collect.ImmutableSortedSet;
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.VoidWork;
|
||||
import google.registry.model.EntityTestCase;
|
||||
import google.registry.model.common.EntityGroupRoot;
|
||||
import google.registry.model.registrar.Registrar.State;
|
||||
|
@ -418,15 +417,16 @@ public class RegistrarTest extends EntityTestCase {
|
|||
|
||||
@Test
|
||||
public void testLoadByClientIdCached_isTransactionless() {
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
assertThat(Registrar.loadByClientIdCached("registrar")).isPresent();
|
||||
// Load something as a control to make sure we are seeing loaded keys in the session cache.
|
||||
ofy().load().entity(abuseAdminContact).now();
|
||||
assertThat(ofy().getSessionKeys()).contains(Key.create(abuseAdminContact));
|
||||
assertThat(ofy().getSessionKeys()).doesNotContain(Key.create(registrar));
|
||||
}});
|
||||
ofy()
|
||||
.transact(
|
||||
() -> {
|
||||
assertThat(Registrar.loadByClientIdCached("registrar")).isPresent();
|
||||
// Load something as a control to make sure we are seeing loaded keys in the session
|
||||
// cache.
|
||||
ofy().load().entity(abuseAdminContact).now();
|
||||
assertThat(ofy().getSessionKeys()).contains(Key.create(abuseAdminContact));
|
||||
assertThat(ofy().getSessionKeys()).doesNotContain(Key.create(registrar));
|
||||
});
|
||||
ofy().clearSessionCache();
|
||||
// Conversely, loads outside of a transaction should end up in the session cache.
|
||||
assertThat(Registrar.loadByClientIdCached("registrar")).isPresent();
|
||||
|
|
|
@ -41,7 +41,6 @@ import com.google.common.collect.ImmutableList;
|
|||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.VoidWork;
|
||||
import google.registry.dns.writer.VoidDnsWriter;
|
||||
import google.registry.model.pricing.StaticPremiumListPricingEngine;
|
||||
import google.registry.model.registry.Registry;
|
||||
|
@ -193,18 +192,14 @@ public class PremiumListUtilsTest {
|
|||
// Remove one of the premium list entries from behind the Bloom filter's back.
|
||||
ofy()
|
||||
.transactNew(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
() ->
|
||||
ofy()
|
||||
.delete()
|
||||
.keys(
|
||||
Key.create(
|
||||
PremiumList.getCached("tld").get().getRevisionKey(),
|
||||
PremiumListEntry.class,
|
||||
"rich"));
|
||||
}
|
||||
});
|
||||
"rich")));
|
||||
ofy().clearSessionCache();
|
||||
|
||||
assertThat(getPremiumPrice("rich", Registry.get("tld"))).isEmpty();
|
||||
|
|
|
@ -53,7 +53,6 @@ import com.google.common.collect.Iterables;
|
|||
import com.google.common.collect.Streams;
|
||||
import com.google.common.net.InetAddresses;
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.VoidWork;
|
||||
import com.googlecode.objectify.cmd.Saver;
|
||||
import google.registry.dns.writer.VoidDnsWriter;
|
||||
import google.registry.model.Buildable;
|
||||
|
@ -366,11 +365,7 @@ public class DatastoreHelper {
|
|||
final DomainResource persistedDomain = persistResource(domain);
|
||||
// Calls {@link LordnTask#enqueueDomainResourceTask} wrapped in an ofy transaction so that the
|
||||
// transaction time is set correctly.
|
||||
ofy().transactNew(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
LordnTask.enqueueDomainResourceTask(persistedDomain);
|
||||
}});
|
||||
ofy().transactNew(() -> LordnTask.enqueueDomainResourceTask(persistedDomain));
|
||||
return persistedDomain;
|
||||
}
|
||||
|
||||
|
@ -947,11 +942,7 @@ public class DatastoreHelper {
|
|||
assertWithMessage("Attempting to persist a Builder is almost certainly an error in test code")
|
||||
.that(resource)
|
||||
.isNotInstanceOf(Buildable.Builder.class);
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
saveResource(resource, wantBackup);
|
||||
}});
|
||||
ofy().transact(() -> saveResource(resource, wantBackup));
|
||||
// Force the session cache to be cleared so that when we read the resource back, we read from
|
||||
// Datastore and not from the session cache. This is needed to trigger Objectify's load process
|
||||
// (unmarshalling entity protos to POJOs, nulling out empty collections, calling @OnLoad
|
||||
|
@ -964,13 +955,13 @@ public class DatastoreHelper {
|
|||
public static <R extends EppResource> R persistEppResourceInFirstBucket(final R resource) {
|
||||
final EppResourceIndex eppResourceIndex =
|
||||
EppResourceIndex.create(Key.create(EppResourceIndexBucket.class, 1), Key.create(resource));
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
Saver saver = ofy().save();
|
||||
saver.entity(resource);
|
||||
persistEppResourceExtras(resource, eppResourceIndex, saver);
|
||||
}});
|
||||
ofy()
|
||||
.transact(
|
||||
() -> {
|
||||
Saver saver = ofy().save();
|
||||
saver.entity(resource);
|
||||
persistEppResourceExtras(resource, eppResourceIndex, saver);
|
||||
});
|
||||
ofy().clearSessionCache();
|
||||
return ofy().load().entity(resource).now();
|
||||
}
|
||||
|
@ -987,13 +978,7 @@ public class DatastoreHelper {
|
|||
}
|
||||
// Persist domains ten at a time, to avoid exceeding the entity group limit.
|
||||
for (final List<R> chunk : Iterables.partition(resources, 10)) {
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
for (R resource : chunk) {
|
||||
saveResource(resource, wantBackup);
|
||||
}
|
||||
}});
|
||||
ofy().transact(() -> chunk.forEach(resource -> saveResource(resource, wantBackup)));
|
||||
}
|
||||
// Force the session to be cleared so that when we read it back, we read from Datastore
|
||||
// and not from the transaction's session cache.
|
||||
|
@ -1015,18 +1000,20 @@ public class DatastoreHelper {
|
|||
*/
|
||||
public static <R extends EppResource> R persistEppResource(final R resource) {
|
||||
checkState(!ofy().inTransaction());
|
||||
ofy().transact(new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().save().<ImmutableObject>entities(
|
||||
resource,
|
||||
new HistoryEntry.Builder()
|
||||
.setParent(resource)
|
||||
.setType(getHistoryEntryType(resource))
|
||||
.setModificationTime(ofy().getTransactionTime())
|
||||
.build());
|
||||
ofy().save().entity(ForeignKeyIndex.create(resource, resource.getDeletionTime()));
|
||||
}});
|
||||
ofy()
|
||||
.transact(
|
||||
() -> {
|
||||
ofy()
|
||||
.save()
|
||||
.<ImmutableObject>entities(
|
||||
resource,
|
||||
new HistoryEntry.Builder()
|
||||
.setParent(resource)
|
||||
.setType(getHistoryEntryType(resource))
|
||||
.setModificationTime(ofy().getTransactionTime())
|
||||
.build());
|
||||
ofy().save().entity(ForeignKeyIndex.create(resource, resource.getDeletionTime()));
|
||||
});
|
||||
ofy().clearSessionCache();
|
||||
return ofy().load().entity(resource).safe();
|
||||
}
|
||||
|
@ -1099,11 +1086,7 @@ public class DatastoreHelper {
|
|||
* ForeignKeyedEppResources.
|
||||
*/
|
||||
public static <R> ImmutableList<R> persistSimpleResources(final Iterable<R> resources) {
|
||||
ofy().transact(new VoidWork(){
|
||||
@Override
|
||||
public void vrun() {
|
||||
ofy().saveWithoutBackup().entities(resources);
|
||||
}});
|
||||
ofy().transact(() -> ofy().saveWithoutBackup().entities(resources));
|
||||
// Force the session to be cleared so that when we read it back, we read from Datastore
|
||||
// and not from the transaction's session cache.
|
||||
ofy().clearSessionCache();
|
||||
|
|
|
@ -35,7 +35,6 @@ import com.google.appengine.api.taskqueue.TransientFailureException;
|
|||
import com.google.apphosting.api.DeadlineExceededException;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.googlecode.objectify.Key;
|
||||
import com.googlecode.objectify.VoidWork;
|
||||
import google.registry.model.domain.DomainResource;
|
||||
import google.registry.model.domain.launch.LaunchNotice;
|
||||
import google.registry.model.ofy.Ofy;
|
||||
|
@ -174,15 +173,7 @@ public class LordnTaskTest {
|
|||
public void test_enqueueDomainResourceTask_throwsNpeOnNullDomain() {
|
||||
assertThrows(
|
||||
NullPointerException.class,
|
||||
() ->
|
||||
ofy()
|
||||
.transactNew(
|
||||
new VoidWork() {
|
||||
@Override
|
||||
public void vrun() {
|
||||
LordnTask.enqueueDomainResourceTask(null);
|
||||
}
|
||||
}));
|
||||
() -> ofy().transactNew(() -> LordnTask.enqueueDomainResourceTask(null)));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue