Skip to content

Commit

Permalink
fix : add synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
samirromdhani committed Sep 20, 2024
1 parent fcbc3d3 commit 5ce1d50
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,6 +18,7 @@
import org.lfenergy.compas.sct.commons.exception.ScdException;

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

import static org.assertj.core.api.Assertions.*;
Expand Down Expand Up @@ -482,6 +483,30 @@ void copySclElement_should_throwException() {
.hasMessage("org.lfenergy.compas.sct.commons.dto.FCDAInfo is not known to this context");
}

@Test
void copySclElement_should_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 = new Callable<TLN>() {
@Override
public TLN call() {
return copySclElement(tln, TLN.class);
}
};
// When
List<Future<TLN>> result = service.invokeAll(Arrays.asList(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(tln);
assertThat(tlns[1]).usingRecursiveComparison().isEqualTo(tln);
}

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

0 comments on commit 5ce1d50

Please sign in to comment.