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

@ -27,6 +27,7 @@ import com.beust.jcommander.Parameters;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import google.registry.testing.FakeClock;
import google.registry.testing.SystemPropertyRule;
import google.registry.tools.ShellCommand.JCommanderCompletor;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
@ -40,6 +41,7 @@ import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@ -47,6 +49,8 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class ShellCommandTest {
@Rule public final SystemPropertyRule systemPropertyRule = new SystemPropertyRule();
CommandRunner cli = mock(CommandRunner.class);
FakeClock clock = new FakeClock(DateTime.parse("2000-01-01TZ"));
@ -107,7 +111,7 @@ public class ShellCommandTest {
@Test
public void testNoIdleWhenInAlpha() throws Exception {
RegistryToolEnvironment.ALPHA.setup();
RegistryToolEnvironment.ALPHA.setup(systemPropertyRule);
MockCli cli = new MockCli();
ShellCommand shellCommand =
createShellCommand(cli, Duration.standardDays(1), "test1 foo bar", "test2 foo bar");
@ -116,7 +120,7 @@ public class ShellCommandTest {
@Test
public void testNoIdleWhenInSandbox() throws Exception {
RegistryToolEnvironment.SANDBOX.setup();
RegistryToolEnvironment.SANDBOX.setup(systemPropertyRule);
MockCli cli = new MockCli();
ShellCommand shellCommand =
createShellCommand(cli, Duration.standardDays(1), "test1 foo bar", "test2 foo bar");
@ -125,7 +129,7 @@ public class ShellCommandTest {
@Test
public void testIdleWhenOverHourInProduction() throws Exception {
RegistryToolEnvironment.PRODUCTION.setup();
RegistryToolEnvironment.PRODUCTION.setup(systemPropertyRule);
MockCli cli = new MockCli();
ShellCommand shellCommand =
createShellCommand(cli, Duration.standardMinutes(61), "test1 foo bar", "test2 foo bar");
@ -135,7 +139,7 @@ public class ShellCommandTest {
@Test
public void testNoIdleWhenUnderHourInProduction() throws Exception {
RegistryToolEnvironment.PRODUCTION.setup();
RegistryToolEnvironment.PRODUCTION.setup(systemPropertyRule);
MockCli cli = new MockCli();
ShellCommand shellCommand =
createShellCommand(cli, Duration.standardMinutes(59), "test1 foo bar", "test2 foo bar");
@ -155,7 +159,7 @@ public class ShellCommandTest {
public void testMultipleCommandInvocations() throws Exception {
try (RegistryCli cli =
new RegistryCli("unittest", ImmutableMap.of("test_command", TestCommand.class))) {
RegistryToolEnvironment.UNITTEST.setup();
RegistryToolEnvironment.UNITTEST.setup(systemPropertyRule);
cli.setEnvironment(RegistryToolEnvironment.UNITTEST);
cli.run(new String[] {"test_command", "-x", "xval", "arg1", "arg2"});
cli.run(new String[] {"test_command", "-x", "otherxval", "arg3"});
@ -272,7 +276,7 @@ public class ShellCommandTest {
@Test
public void testEncapsulatedOutput_command() throws Exception {
RegistryToolEnvironment.ALPHA.setup();
RegistryToolEnvironment.ALPHA.setup(systemPropertyRule);
captureOutput();
ShellCommand shellCommand =
new ShellCommand(
@ -296,7 +300,7 @@ public class ShellCommandTest {
@Test
public void testEncapsulatedOutput_throws() throws Exception {
RegistryToolEnvironment.ALPHA.setup();
RegistryToolEnvironment.ALPHA.setup(systemPropertyRule);
captureOutput();
ShellCommand shellCommand =
new ShellCommand(