Reimplement restarting of scm-manager

SCM-Manager tries now to figure out which is the best strategy for the restart.
It chooses from one of the following strategies:

* PosixRestartStrategy which uses native LibC
* ExitRestartStrategy uses System.exit and relies on external mechanism to start again
* InjectionContextRestartStrategy destroys and re initializes the injection context
This commit is contained in:
Sebastian Sdorra
2020-02-12 12:13:10 +01:00
parent e30796b995
commit 56b8dbdb22
11 changed files with 412 additions and 8 deletions

View File

@@ -0,0 +1,75 @@
package sonia.scm.lifecycle;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.function.IntConsumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
class ExitRestartStrategyTest {
@Mock
private RestartStrategy.InjectionContext context;
private ExitRestartStrategy strategy;
private CapturingExiter exiter;
@BeforeEach
void setUpStrategy() {
strategy = new ExitRestartStrategy();
exiter = new CapturingExiter();
strategy.setExiter(exiter);
}
@Test
void shouldTearDownContextAndThenExit() {
strategy.restart(context);
verify(context).destroy();
assertThat(exiter.getExitCode()).isEqualTo(0);
}
@Test
void shouldUseExitCodeFromSystemProperty() {
System.setProperty(ExitRestartStrategy.PROPERTY_EXIT_CODE, "42");
try {
strategy.restart(context);
verify(context).destroy();
assertThat(exiter.getExitCode()).isEqualTo(42);
} finally {
System.clearProperty(ExitRestartStrategy.PROPERTY_EXIT_CODE);
}
}
@Test
void shouldThrowExceptionForNonNumericExitCode() {
System.setProperty(ExitRestartStrategy.PROPERTY_EXIT_CODE, "xyz");
try {
assertThrows(RestartNotSupportedException.class, () -> strategy.restart(context));
} finally {
System.clearProperty(ExitRestartStrategy.PROPERTY_EXIT_CODE);
}
}
private static class CapturingExiter implements IntConsumer {
private int exitCode = -1;
public int getExitCode() {
return exitCode;
}
@Override
public void accept(int exitCode) {
this.exitCode = exitCode;
}
}
}

View File

@@ -0,0 +1,101 @@
package sonia.scm.lifecycle;
import com.google.common.base.Strings;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import sonia.scm.util.SystemUtil;
import java.util.Optional;
import java.util.function.Consumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
class RestartStrategyTest {
private final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
@Test
void shouldReturnRestartStrategyFromSystemProperty() {
withStrategy(TestingRestartStrategy.class.getName(), (rs) -> {
assertThat(rs).containsInstanceOf(TestingRestartStrategy.class);
});
}
@Test
void shouldThrowExceptionForNonStrategyClass() {
withStrategy(RestartStrategyTest.class.getName(), () -> {
assertThrows(RestartNotSupportedException.class, () -> RestartStrategy.get(classLoader));
});
}
@Test
void shouldReturnEmpty() {
withStrategy(RestartStrategyFactory.STRATEGY_NONE, (rs) -> {
assertThat(rs).isEmpty();
});
}
@Test
void shouldReturnEmptyForUnknownOs() {
withSystemProperty(SystemUtil.PROPERTY_OSNAME, "hitchhiker-os", () -> {
Optional<RestartStrategy> restartStrategy = RestartStrategy.get(classLoader);
assertThat(restartStrategy).isEmpty();
});
}
@Test
void shouldReturnExitRestartStrategy() {
withStrategy(ExitRestartStrategy.NAME, (rs) -> {
assertThat(rs).containsInstanceOf(ExitRestartStrategy.class);
});
}
@Test
void shouldReturnInjectionContextRestartStrategy() {
withStrategy(InjectionContextRestartStrategy.NAME, (rs) -> {
assertThat(rs).containsInstanceOf(InjectionContextRestartStrategy.class);
});
}
@ParameterizedTest
@ValueSource(strings = { "linux", "darwin", "solaris", "freebsd", "openbsd" })
void shouldReturnPosixRestartStrategyForPosixBased(String os) {
withSystemProperty(SystemUtil.PROPERTY_OSNAME, os, () -> {
Optional<RestartStrategy> restartStrategy = RestartStrategy.get(classLoader);
assertThat(restartStrategy).containsInstanceOf(PosixRestartStrategy.class);
});
}
private void withStrategy(String strategy, Consumer<Optional<RestartStrategy>> consumer) {
withStrategy(strategy, () -> {
consumer.accept(RestartStrategy.get(classLoader));
});
}
private void withStrategy(String strategy, Runnable runnable) {
withSystemProperty(RestartStrategyFactory.PROPERTY_STRATEGY, strategy, runnable);
}
private void withSystemProperty(String key, String value, Runnable runnable) {
String oldValue = System.getProperty(key);
System.setProperty(key, value);
try {
runnable.run();
} finally {
if (Strings.isNullOrEmpty(oldValue)) {
System.clearProperty(key);
} else {
System.setProperty(key, oldValue);
}
}
}
public static class TestingRestartStrategy implements RestartStrategy {
@Override
public void restart(InjectionContext context) {
}
}
}