Fix flaky Spec11PipelineTest (#1133)

This commit is contained in:
Lai Jiang 2021-05-07 15:01:11 -04:00 committed by GitHub
parent 235fbfd18e
commit afbb36f68c
3 changed files with 35 additions and 29 deletions

View file

@ -20,6 +20,7 @@
package google.registry.beam; package google.registry.beam;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
@ -27,6 +28,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings; import com.google.common.base.Strings;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -295,6 +297,16 @@ public class TestPipelineExtension extends Pipeline
enableAbandonedNodeEnforcement(true); enableAbandonedNodeEnforcement(true);
} }
// Clear this property so that when default Guava ThreadFactory is created
// it will not think that it is in App Engine and return an unusable
// ThreadFactory.
System.clearProperty("com.google.appengine.runtime.environment");
assertWithMessage(
"Beam pipelines don't run in an App Engine environment, and thus"
+ " the tests shouldn't be mocking one either.")
.that(isAppEngine())
.isFalse();
} }
@Override @Override
@ -500,6 +512,25 @@ public class TestPipelineExtension extends Pipeline
} }
} }
// Adapted from Guava's MoreExecutors (where it is a private method)
private static boolean isAppEngine() {
if (System.getProperty("com.google.appengine.runtime.environment") == null) {
return false;
} else {
try {
return Class.forName("com.google.apphosting.api.ApiProxy")
.getMethod("getCurrentEnvironment")
.invoke(null)
!= null;
} catch (ClassNotFoundException
| InvocationTargetException
| IllegalAccessException
| NoSuchMethodException e) {
return false;
}
}
}
private static class IsEmptyVisitor extends PipelineVisitor.Defaults { private static class IsEmptyVisitor extends PipelineVisitor.Defaults {
private boolean empty = true; private boolean empty = true;

View file

@ -16,7 +16,6 @@ package google.registry.beam.spec11;
import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.model.ImmutableObjectSubject.immutableObjectCorrespondence; import static google.registry.model.ImmutableObjectSubject.immutableObjectCorrespondence;
import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm;
@ -35,7 +34,6 @@ import google.registry.testing.DatastoreEntityExtension;
import google.registry.testing.FakeClock; import google.registry.testing.FakeClock;
import google.registry.util.ResourceUtils; import google.registry.util.ResourceUtils;
import java.io.File; import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import org.apache.beam.sdk.coders.KvCoder; import org.apache.beam.sdk.coders.KvCoder;
@ -120,11 +118,6 @@ class Spec11PipelineTest {
KvCoder.of( KvCoder.of(
SerializableCoder.of(Subdomain.class), SerializableCoder.of(Subdomain.class),
SerializableCoder.of(ThreatMatch.class)))); SerializableCoder.of(ThreatMatch.class))));
assertWithMessage(
"Beam pipelines don't run in an App Engine environment, and thus the tests shouldn't be"
+ " mocking one either")
.that(isAppEngine())
.isFalse();
} }
@Test @Test
@ -195,25 +188,6 @@ class Spec11PipelineTest {
.containsExactlyElementsIn(expectedFileContents.subList(1, expectedFileContents.size())); .containsExactlyElementsIn(expectedFileContents.subList(1, expectedFileContents.size()));
} }
// Adapted from Guava's MoreExecutors (where it is a private method)
private static boolean isAppEngine() {
if (System.getProperty("com.google.appengine.runtime.environment") == null) {
return false;
} else {
try {
return Class.forName("com.google.apphosting.api.ApiProxy")
.getMethod("getCurrentEnvironment")
.invoke(null)
!= null;
} catch (ClassNotFoundException
| InvocationTargetException
| IllegalAccessException
| NoSuchMethodException e) {
return false;
}
}
}
/** Returns the text contents of a file under the beamBucket/results directory. */ /** Returns the text contents of a file under the beamBucket/results directory. */
private ImmutableList<String> resultFileContents() throws Exception { private ImmutableList<String> resultFileContents() throws Exception {
File resultFile = File resultFile =

View file

@ -26,9 +26,10 @@ import org.testcontainers.shaded.com.google.common.collect.ImmutableMap;
* Allows instantiation of Datastore {@code Entity entities} without the heavyweight {@code * Allows instantiation of Datastore {@code Entity entities} without the heavyweight {@code
* AppEngineRule}. * AppEngineRule}.
* *
* <p>When used together with {@code JpaIntegrationWithCoverageExtension}, this extension must be * <p>When used together with {@code JpaIntegrationWithCoverageExtension} or @{@code
* registered first. For consistency's sake, it is recommended that the field for this extension be * TestPipelineExtension}, this extension must be registered first. For consistency's sake, it is
* annotated with {@code @org.junit.jupiter.api.Order(value = 1)}. Please refer to {@link * recommended that the field for this extension be annotated with
* {@code @org.junit.jupiter.api.Order(value = 1)}. Please refer to {@link
* google.registry.model.domain.DomainBaseSqlTest} for example, and to <a * google.registry.model.domain.DomainBaseSqlTest} for example, and to <a
* href="https://junit.org/junit5/docs/current/user-guide/#extensions-registration-programmatic"> * href="https://junit.org/junit5/docs/current/user-guide/#extensions-registration-programmatic">
* JUnit 5 User Guide</a> for details of extension ordering. * JUnit 5 User Guide</a> for details of extension ordering.