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

@ -47,11 +47,25 @@ public enum RegistryEnvironment {
*/
UNITTEST;
/** Sets this enum as the name of the registry environment. */
public RegistryEnvironment setup() {
return setup(SystemPropertySetter.PRODUCTION_IMPL);
}
/**
* Sets this enum as the name of the registry environment using specified {@link
* SystemPropertySetter}.
*/
public RegistryEnvironment setup(SystemPropertySetter systemPropertySetter) {
systemPropertySetter.setProperty(PROPERTY, name());
return this;
}
/** Returns environment configured by system property {@value #PROPERTY}. */
public static RegistryEnvironment get() {
return valueOf(Ascii.toUpperCase(System.getProperty(PROPERTY, UNITTEST.name())));
}
/** System property for configuring which environment we should use. */
public static final String PROPERTY = "google.registry.environment";
private static final String PROPERTY = "google.registry.environment";
}

View file

@ -0,0 +1,47 @@
// Copyright 2018 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.config;
import javax.annotation.Nullable;
/**
* Wrapper interface around {@link System#setProperty(String, String)} and {@link
* System#clearProperty(String)}. Tests that modify system properties may provide custom
* implementations that keeps track of changes and restores original property values on test
* completion.
*/
public interface SystemPropertySetter {
/**
* Updates the system property specified by {@code key}. If {@code value} is not null, {@link
* System#setProperty(String, String)} is invoked; otherwise {@link System#clearProperty(String)}
* is invoked.
*/
SystemPropertySetter setProperty(String key, @Nullable String value);
/** Production implementation of {@link SystemPropertySetter}. */
SystemPropertySetter PRODUCTION_IMPL =
new SystemPropertySetter() {
@Override
public SystemPropertySetter setProperty(String key, @Nullable String value) {
if (value == null) {
System.clearProperty(key);
} else {
System.setProperty(key, value);
}
return this;
}
};
}

View file

@ -17,10 +17,12 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import google.registry.config.RegistryEnvironment;
import google.registry.config.SystemPropertySetter;
/** Enum of production environments, used for the {@code --environment} flag. */
enum RegistryToolEnvironment {
@ -75,12 +77,24 @@ enum RegistryToolEnvironment {
return instance;
}
/** Setup execution environment. Call this method before any classes are loaded. */
/** Resets static class state to uninitialized state. */
@VisibleForTesting
static void reset() {
instance = null;
}
/** Sets up execution environment. Call this method before any classes are loaded. */
void setup() {
setup(SystemPropertySetter.PRODUCTION_IMPL);
}
/** Sets up execution environment. Call this method before any classes are loaded. */
@VisibleForTesting
void setup(SystemPropertySetter systemPropertySetter) {
instance = this;
System.setProperty(RegistryEnvironment.PROPERTY, actualEnvironment.name());
actualEnvironment.setup(systemPropertySetter);
for (ImmutableMap.Entry<String, String> entry : extraProperties.entrySet()) {
System.setProperty(entry.getKey(), entry.getValue());
systemPropertySetter.setProperty(entry.getKey(), entry.getValue());
}
}