2019-06-25 08:36:57 +02:00
|
|
|
package sonia.scm.lifecycle;
|
2019-06-19 11:53:58 +02:00
|
|
|
|
|
|
|
|
import com.github.legman.Subscribe;
|
|
|
|
|
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 sonia.scm.event.RecreateEventBusEvent;
|
|
|
|
|
import sonia.scm.event.ScmEventBus;
|
|
|
|
|
|
2020-03-03 15:15:17 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
2019-06-19 11:53:58 +02:00
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
2020-03-03 15:15:17 +01:00
|
|
|
import static org.awaitility.Awaitility.await;
|
2019-06-19 11:53:58 +02:00
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
|
|
|
|
|
@ExtendWith(MockitoExtension.class)
|
|
|
|
|
class InjectionContextRestartStrategyTest {
|
|
|
|
|
|
|
|
|
|
@Mock
|
|
|
|
|
private RestartStrategy.InjectionContext context;
|
|
|
|
|
|
2019-11-21 16:20:55 +01:00
|
|
|
private InjectionContextRestartStrategy strategy = new InjectionContextRestartStrategy(Thread.currentThread().getContextClassLoader());
|
2019-06-19 11:53:58 +02:00
|
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
|
void setWaitToZero() {
|
|
|
|
|
strategy.setWaitInMs(0L);
|
2019-11-21 16:20:55 +01:00
|
|
|
// disable gc during tests
|
|
|
|
|
strategy.setGcEnabled(false);
|
2019-06-19 11:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
2020-03-03 15:15:17 +01:00
|
|
|
void shouldCallDestroyAndInitialize() {
|
|
|
|
|
TestingInjectionContext ctx = new TestingInjectionContext();
|
|
|
|
|
strategy.restart(ctx);
|
|
|
|
|
await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ctx.destroyed).isTrue());
|
|
|
|
|
await().atMost(1, TimeUnit.SECONDS).untilAsserted(() -> assertThat(ctx.initialized).isTrue());
|
2019-06-19 11:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldFireRecreateEventBusEvent() {
|
|
|
|
|
Listener listener = new Listener();
|
|
|
|
|
ScmEventBus.getInstance().register(listener);
|
|
|
|
|
|
|
|
|
|
strategy.restart(context);
|
|
|
|
|
|
|
|
|
|
assertThat(listener.event).isNotNull();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldRegisterContextAfterRestart() throws InterruptedException {
|
|
|
|
|
TestingInjectionContext ctx = new TestingInjectionContext();
|
|
|
|
|
strategy.restart(ctx);
|
|
|
|
|
|
2020-03-03 15:15:17 +01:00
|
|
|
await().atMost(1, TimeUnit.SECONDS).until(() -> ctx.initialized);
|
2019-06-19 11:53:58 +02:00
|
|
|
Thread.sleep(50L);
|
|
|
|
|
ScmEventBus.getInstance().post("hello event");
|
|
|
|
|
|
|
|
|
|
assertThat(ctx.event).isEqualTo("hello event");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class Listener {
|
|
|
|
|
|
|
|
|
|
private RecreateEventBusEvent event;
|
|
|
|
|
|
|
|
|
|
@Subscribe(async = false)
|
|
|
|
|
public void setEvent(RecreateEventBusEvent event) {
|
|
|
|
|
this.event = event;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class TestingInjectionContext implements RestartStrategy.InjectionContext {
|
|
|
|
|
|
|
|
|
|
private volatile String event;
|
2020-03-03 15:15:17 +01:00
|
|
|
private boolean initialized = false;
|
|
|
|
|
private boolean destroyed = false;
|
2019-06-19 11:53:58 +02:00
|
|
|
|
|
|
|
|
@Subscribe(async = false)
|
|
|
|
|
public void setEvent(String event) {
|
|
|
|
|
this.event = event;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void initialize() {
|
2020-03-03 15:15:17 +01:00
|
|
|
this.initialized = true;
|
2019-06-19 11:53:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void destroy() {
|
2020-03-03 15:15:17 +01:00
|
|
|
this.destroyed = true;
|
2019-06-19 11:53:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|