Fix flakiness caused by ofy entity name conflict (#636)

* Fix flakiness caused by ofy entity name conflict

Overrode the 'kind' of two test entities to prevent name conflicts.
Tests are flaky because of this.

Added a check in AppEngineRuleTest for conflicting kinds.
This commit is contained in:
Weimin Yu 2020-06-18 13:14:10 -04:00 committed by GitHub
parent 4c7bdbf3c6
commit f7ca068f8e
3 changed files with 41 additions and 2 deletions

View file

@ -288,7 +288,7 @@ public class TransactionManagerTest {
entities.forEach(TransactionManagerTest::assertEntityNotExist);
}
@Entity(name = "TestEntity")
@Entity(name = "TxnMgrTestEntity")
@javax.persistence.Entity(name = "TestEntity")
private static class TestEntity extends ImmutableObject {
@Id @javax.persistence.Id private String name;

View file

@ -99,7 +99,7 @@ public class TransactionTest {
StreamCorruptedException.class, () -> Transaction.deserialize(new byte[] {1, 2, 3, 4}));
}
@Entity(name = "TestEntity")
@Entity(name = "TxnTestEntity")
@javax.persistence.Entity(name = "TestEntity")
private static class TestEntity extends ImmutableObject {
@Id @javax.persistence.Id private String name;

View file

@ -14,13 +14,23 @@
package google.registry.testing;
import static com.google.common.truth.Truth.assertWithMessage;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.Multimaps;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.ScanResult;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@ -106,6 +116,35 @@ public class AppEngineRuleTest {
appEngineRule.after();
}
@Test
public void testOfyEntities_uniqueKinds() {
try (ScanResult scanResult =
new ClassGraph()
.enableAnnotationInfo()
.ignoreClassVisibility()
.whitelistPackages("google.registry")
.scan()) {
Multimap<String, Class<?>> kindToEntityMultiMap =
scanResult.getClassesWithAnnotation(Entity.class.getName()).stream()
.filter(clazz -> !clazz.getName().equals(TestObject.class.getName()))
.map(clazz -> clazz.loadClass())
.collect(
Multimaps.toMultimap(
Key::getKind,
clazz -> clazz,
MultimapBuilder.hashKeys().linkedListValues()::build));
Map<String, Collection<Class<?>>> conflictingKinds =
kindToEntityMultiMap.asMap().entrySet().stream()
.filter(e -> e.getValue().size() > 1)
.collect(ImmutableMap.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue));
assertWithMessage(
"Conflicting Ofy kinds found. Tests will break if they are registered with "
+ " AppEngineRule in the same test executor.")
.that(conflictingKinds)
.isEmpty();
}
}
private void writeAutoIndexFile(String content) throws IOException {
com.google.common.io.Files.asCharSink(
new File(temporaryFolder.getRoot(), "datastore-indexes-auto.xml"), UTF_8)