Merged in feature/postpone_restart_for_plugin (pull request #362)

Feature/postpone restart for plugin
This commit is contained in:
Eduard Heimbuch
2019-11-25 12:25:10 +00:00
2 changed files with 30 additions and 18 deletions

View File

@@ -187,7 +187,7 @@ public class DefaultPluginManager implements PluginManager {
if (!pendingInstallations.isEmpty()) {
if (restartAfterInstallation) {
restart("plugin installation");
triggerRestart("plugin installation");
} else {
pendingInstallQueue.addAll(pendingInstallations);
updateMayUninstallFlag();
@@ -205,7 +205,7 @@ public class DefaultPluginManager implements PluginManager {
markForUninstall(installed);
if (restartAfterInstallation) {
restart("plugin installation");
triggerRestart("plugin installation");
} else {
updateMayUninstallFlag();
}
@@ -238,12 +238,19 @@ public class DefaultPluginManager implements PluginManager {
public void executePendingAndRestart() {
PluginPermissions.manage().check();
if (!pendingInstallQueue.isEmpty() || getInstalled().stream().anyMatch(InstalledPlugin::isMarkedForUninstall)) {
restart("execute pending plugin changes");
triggerRestart("execute pending plugin changes");
}
}
private void restart(String cause) {
eventBus.post(new RestartEvent(PluginManager.class, cause));
@VisibleForTesting
void triggerRestart(String cause) {
new Thread(() -> {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
}
eventBus.post(new RestartEvent(PluginManager.class, cause));
}).start();
}
private void cancelPending(List<PendingPluginInstallation> pendingInstallations) {

View File

@@ -12,13 +12,10 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junitpioneer.jupiter.TempDirectory;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.NotFoundException;
import sonia.scm.ScmConstraintViolationException;
import sonia.scm.event.ScmEventBus;
import sonia.scm.lifecycle.RestartEvent;
import java.io.IOException;
import java.nio.file.Files;
@@ -48,9 +45,6 @@ import static sonia.scm.plugin.PluginTestHelper.createInstalled;
@ExtendWith(TempDirectory.class)
class DefaultPluginManagerTest {
@Mock
private ScmEventBus eventBus;
@Mock
private PluginLoader loader;
@@ -60,12 +54,13 @@ class DefaultPluginManagerTest {
@Mock
private PluginInstaller installer;
@InjectMocks
private DefaultPluginManager manager;
@Mock
private Subject subject;
private boolean restartTriggered = false;
@BeforeEach
void mockInstaller() {
lenient().when(installer.install(any())).then(ic -> {
@@ -74,6 +69,16 @@ class DefaultPluginManagerTest {
});
}
@BeforeEach
void createPluginManagerToTestWithCapturedRestart() {
manager = new DefaultPluginManager(null, loader, center, installer) { // event bus is only used in restart and this is replaced here
@Override
void triggerRestart(String cause) {
restartTriggered = true;
}
};
}
@Nested
class WithAdminPermissions {
@@ -180,7 +185,7 @@ class DefaultPluginManagerTest {
manager.install("scm-git-plugin", false);
verify(installer).install(git);
verify(eventBus, never()).post(any());
assertThat(restartTriggered).isFalse();
}
@Test
@@ -258,7 +263,7 @@ class DefaultPluginManagerTest {
manager.install("scm-git-plugin", true);
verify(installer).install(git);
verify(eventBus).post(any(RestartEvent.class));
assertThat(restartTriggered).isTrue();
}
@Test
@@ -267,7 +272,7 @@ class DefaultPluginManagerTest {
when(loader.getInstalledPlugins()).thenReturn(ImmutableList.of(gitInstalled));
manager.install("scm-git-plugin", true);
verify(eventBus, never()).post(any());
assertThat(restartTriggered).isFalse();
}
@Test
@@ -289,14 +294,14 @@ class DefaultPluginManagerTest {
manager.install("scm-review-plugin", false);
manager.executePendingAndRestart();
verify(eventBus).post(any(RestartEvent.class));
assertThat(restartTriggered).isTrue();
}
@Test
void shouldNotSendRestartEventWithoutPendingPlugins() {
manager.executePendingAndRestart();
verify(eventBus, never()).post(any());
assertThat(restartTriggered).isFalse();
}
@Test
@@ -447,7 +452,7 @@ class DefaultPluginManagerTest {
manager.executePendingAndRestart();
verify(eventBus).post(any(RestartEvent.class));
assertThat(restartTriggered).isTrue();
}
@Test