Skip to content

Commit

Permalink
fix : add synchronized
Browse files Browse the repository at this point in the history
Signed-off-by: Samir Romdhani <samir.romdhani@rte-france.com>
  • Loading branch information
samirromdhani committed Sep 23, 2024
1 parent fcbc3d3 commit 6d2afea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public static String toHex(long number, int length) {
* @param <T> type of the object
* @return copy of the object
*/
public static <T> T copySclElement(T object, Class<T> clazz) {
public static synchronized <T> T copySclElement(T object, Class<T> clazz) {
try {
if (jaxbContext == null) {
jaxbContext = JAXBContext.newInstance("org.lfenergy.compas.scl2007b4.model");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import org.lfenergy.compas.sct.commons.exception.ScdException;

import java.util.*;
import java.util.concurrent.*;
import java.util.stream.Stream;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.*;
import static org.lfenergy.compas.sct.commons.util.Utils.copySclElement;

Expand Down Expand Up @@ -482,6 +484,29 @@ void copySclElement_should_throwException() {
.hasMessage("org.lfenergy.compas.sct.commons.dto.FCDAInfo is not known to this context");
}

@Test
void copySclElement_should_succeed_when_syncRead() throws ExecutionException, InterruptedException {
// Given
TLN tln = new TLN();
tln.setLnType("T1");
tln.getLnClass().add(TLLN0Enum.LLN_0.value());
ExecutorService service = Executors.newFixedThreadPool(2);
Callable<TLN> copySclElementTlnCallable = () -> copySclElement(tln, TLN.class);
// When
List<Future<TLN>> result = service.invokeAll(List.of(copySclElementTlnCallable, copySclElementTlnCallable));
service.shutdown();
TLN[] tlns = new TLN[]{result.getFirst().get(), result.getLast().get()};
// Then
assertThat(tlns).hasSize(2);
assertThat(tlns[0])
.isNotSameAs(tlns[1])
.isNotSameAs(tln);
assertThat(tlns[0])
.usingRecursiveComparison()
.isEqualTo(tlns[1])
.isEqualTo(tln);
}

@Test
void extractFromP_should_return_value_for_given_type(){
// Given
Expand Down

0 comments on commit 6d2afea

Please sign in to comment.