Restore original System Properties after tests

Many registry tools tests modify system properties but do not
restore them to original state. These tests must be isolated
from each other and cannot share the same test execution process.

This has a huge impact on test performance under Gradle, which
seems to have higher process startup overhead. Current Gradle
test config has to set 'forEvery' to 1, i.e., every test class
must be run in a freshly started process.

This change significantly reduces the number of tests that need
isolation, making it easier to optimize test config for the
remaining tests.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=221350284
This commit is contained in:
weiminyu 2018-11-13 15:57:01 -08:00 committed by jianglai
parent 9fa2a84c35
commit 75add42a1b
11 changed files with 125 additions and 26 deletions

View file

@ -17,6 +17,7 @@ package google.registry.testing;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import google.registry.config.SystemPropertySetter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -25,10 +26,8 @@ import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.rules.ExternalResource;
/**
* JUnit Rule for overriding the values Java system properties during tests.
*/
public final class SystemPropertyRule extends ExternalResource {
/** JUnit Rule for overriding the values Java system properties during tests. */
public final class SystemPropertyRule extends ExternalResource implements SystemPropertySetter {
/** Class representing a system property key value pair. */
private static class Property {
@ -56,14 +55,15 @@ public final class SystemPropertyRule extends ExternalResource {
/**
* Change the value of a system property which is restored to its original value after the test.
*
* <p>It's safe to call this method multiple times with the same {@code key} within a single
* test. Only the truly original property value will be restored at the end.
* <p>It's safe to call this method multiple times with the same {@code key} within a single test.
* Only the truly original property value will be restored at the end.
*
* <p>This method can be called fluently when declaring the Rule field, or within a Test method.
*
* @see java.lang.System#setProperty(String, String)
*/
public SystemPropertyRule override(String key, @Nullable String value) {
@Override
public SystemPropertyRule setProperty(String key, @Nullable String value) {
originals.computeIfAbsent(
key, k -> new Property(k, Optional.ofNullable(System.getProperty(k))));
Property property = new Property(key, Optional.ofNullable(value));