Skip to content

Commit

Permalink
[IMG-2365] [IMG] Perte de connexion avec CALIN
Browse files Browse the repository at this point in the history
  • Loading branch information
CR36053T committed Jun 7, 2024
1 parent 338bb2c commit 01001a9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import jakarta.inject.Singleton;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
Expand All @@ -28,6 +31,7 @@
@Named
@Singleton
public class AppDataBean {
private static final Logger LOGGER = LoggerFactory.getLogger(AppDataBean.class);

protected AppData appData;

Expand Down Expand Up @@ -97,20 +101,28 @@ public void clean() {
* This method reinit only the computation manager (no effect on other connexions with other backends)
*
*/
public void reinitComputationManager() {
public void reinitComputationManager(boolean throwException) {
try {
if (shortTimeExecutionComputationManager != null) {
shortTimeExecutionComputationManager.close();
}
} catch (Exception e) {
// Do nothing
if (throwException) {
throw e;
} else {
LOGGER.warn("shortTimeExecutionComputationManager is not in a closable state. Had exception '{}' while trying to close it. It will be reinitialized anyway.", e.getMessage());
}
}
try {
if (longTimeExecutionComputationManager != null) {
longTimeExecutionComputationManager.close();
}
} catch (Exception e) {
// Do nothing
if (throwException) {
throw e;
} else {
LOGGER.warn("longTimeExecutionComputationManager is not in a closable state. Had exception '{}' while trying to close it. It will be reinitialized anyway.", e.getMessage());
}
}
shortTimeExecutionComputationManager = config.createShortTimeExecutionComputationManager();
longTimeExecutionComputationManager = config.createLongTimeExecutionComputationManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import static org.mockito.MockitoAnnotations.openMocks;

Expand Down Expand Up @@ -36,7 +37,7 @@ void reinitComputationManagerNominal() {
appDataBeanUnderTest.longTimeExecutionComputationManager = longTimeExecutionComputationManager;
appDataBeanUnderTest.shortTimeExecutionComputationManager = shortTimeExecutionComputationManager;
// WHEN
appDataBeanUnderTest.reinitComputationManager();
appDataBeanUnderTest.reinitComputationManager(false);
// THEN
verify(longTimeExecutionComputationManager, times(1)).close();
verify(shortTimeExecutionComputationManager, times(1)).close();
Expand All @@ -51,21 +52,64 @@ void reinitComputationManagerNominal() {
}

@Test
void reinitComputationManagerManagerThrowsException() {
void reinitComputationManagerManagerThrowsException1() {
// GIVEN
appDataBeanUnderTest.longTimeExecutionComputationManager = longTimeExecutionComputationManager;
appDataBeanUnderTest.shortTimeExecutionComputationManager = shortTimeExecutionComputationManager;
doThrow(new Exception("SHORT")).when(shortTimeExecutionComputationManager).close();
doThrow(new Exception("LONG")).when(longTimeExecutionComputationManager).close();
// WHEN
appDataBeanUnderTest.reinitComputationManager();
appDataBeanUnderTest.reinitComputationManager(false);
// THEN
verify(longTimeExecutionComputationManager, times(1)).close();
verify(shortTimeExecutionComputationManager, times(1)).close();
try (
com.powsybl.computation.ComputationManager shortComputationManager = verify(config, times(1)).createShortTimeExecutionComputationManager();
com.powsybl.computation.ComputationManager longComputationManager = verify(config, times(1)).createLongTimeExecutionComputationManager()
){
) {
verifyNoMoreInteractions(config);
}
}

@Test
void reinitComputationManagerManagerThrowsException2() {
// GIVEN
appDataBeanUnderTest.longTimeExecutionComputationManager = longTimeExecutionComputationManager;
appDataBeanUnderTest.shortTimeExecutionComputationManager = shortTimeExecutionComputationManager;
doThrow(new Exception("SHORT")).when(shortTimeExecutionComputationManager).close();
doThrow(new Exception("LONG")).when(longTimeExecutionComputationManager).close();
// WHEN
Exception exception = assertThrows(Exception.class, () -> {
appDataBeanUnderTest.reinitComputationManager(true);
});
// THEN
verify(longTimeExecutionComputationManager, times(0)).close();
verify(shortTimeExecutionComputationManager, times(1)).close();
try (
com.powsybl.computation.ComputationManager shortComputationManager = verify(config, times(0)).createShortTimeExecutionComputationManager();
com.powsybl.computation.ComputationManager longComputationManager = verify(config, times(0)).createLongTimeExecutionComputationManager()
) {
verifyNoMoreInteractions(config);
}
}

@Test
void reinitComputationManagerManagerThrowsException3() {
// GIVEN
appDataBeanUnderTest.longTimeExecutionComputationManager = longTimeExecutionComputationManager;
appDataBeanUnderTest.shortTimeExecutionComputationManager = shortTimeExecutionComputationManager;
doThrow(new Exception("LONG")).when(longTimeExecutionComputationManager).close();
// WHEN
Exception exception = assertThrows(Exception.class, () -> {
appDataBeanUnderTest.reinitComputationManager(true);
});
// THEN
verify(longTimeExecutionComputationManager, times(1)).close();
verify(shortTimeExecutionComputationManager, times(1)).close();
try (
com.powsybl.computation.ComputationManager shortComputationManager = verify(config, times(0)).createShortTimeExecutionComputationManager();
com.powsybl.computation.ComputationManager longComputationManager = verify(config, times(0)).createLongTimeExecutionComputationManager()
) {
verifyNoMoreInteractions(config);
}
}
Expand All @@ -76,14 +120,14 @@ void reinitComputationManagerManagerNull() {
appDataBeanUnderTest.longTimeExecutionComputationManager = null;
appDataBeanUnderTest.shortTimeExecutionComputationManager = null;
// WHEN
appDataBeanUnderTest.reinitComputationManager();
appDataBeanUnderTest.reinitComputationManager(false);
// THEN
verify(longTimeExecutionComputationManager, times(0)).close();
verify(shortTimeExecutionComputationManager, times(0)).close();
try (
com.powsybl.computation.ComputationManager shortComputationManager = verify(config, times(1)).createShortTimeExecutionComputationManager();
com.powsybl.computation.ComputationManager longComputationManager = verify(config, times(1)).createLongTimeExecutionComputationManager()
){
) {
verifyNoMoreInteractions(config);
}
}
Expand Down

0 comments on commit 01001a9

Please sign in to comment.