Skip to content
Snippets Groups Projects
Commit 0d11391d authored by Piotr Gawron's avatar Piotr Gawron
Browse files

race condition

parent 89e30469
No related branches found
No related tags found
3 merge requests!1567Merge 16.2.11 into master,!1566Merge 16.2.11 into 16.3,!1558race condition
Pipeline #68179 failed
minerva (16.2.11) stable; urgency=medium
* Bug fix: therewas a race condition in very rare situations when exporting
to CellDesigner (#1728)
-- Piotr Gawron <piotr.gawron@uni.lu> Wed, 01 Feb 2023 12:00:00 +0200
minerva (16.2.10) stable; urgency=medium
* Bug fix (API documentation): typos in API documentation corrected (#1806)
* Bug fix (API documentation): header in logout method change to keep
......
......@@ -74,6 +74,13 @@
<version>${springframework.version}</version>
</dependency>
<!-- spring module used for testing -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springframework.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -290,10 +290,11 @@ public enum ReactionLineData {
*/
public static ReactionLineData getByReactionType(final Class<? extends Reaction> clazz) {
if (map == null) {
map = new HashMap<Class<? extends Reaction>, ReactionLineData>();
Map<Class<? extends Reaction>, ReactionLineData> result = new HashMap<>();
for (final ReactionLineData rld : values()) {
map.put(rld.getReactionClass(), rld);
result.put(rld.getReactionClass(), rld);
}
map = result;
}
return map.get(clazz);
}
......@@ -309,10 +310,11 @@ public enum ReactionLineData {
*/
public static ReactionLineData getByCellDesignerString(final String type) {
if (cellDesignerMap == null) {
cellDesignerMap = new HashMap<String, ReactionLineData>();
Map<String, ReactionLineData> result = new HashMap<>();
for (final ReactionLineData rld : values()) {
cellDesignerMap.put(rld.getCellDesignerString(), rld);
result.put(rld.getCellDesignerString(), rld);
}
cellDesignerMap = result;
}
return cellDesignerMap.get(type);
}
......
package lcsb.mapviewer.converter.model.celldesigner.reaction;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;
import lcsb.mapviewer.converter.model.celldesigner.CellDesignerTestFunctions;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.reaction.type.UnknownTransitionReaction;
public class ReactionLineDataTest extends CellDesignerTestFunctions {
......@@ -43,4 +50,66 @@ public class ReactionLineDataTest extends CellDesignerTestFunctions {
ReactionLineData.TRANSPORT.createReaction(Mockito.mock(Reaction.class));
}
@Test
public void testGetByReactionTypeMultithreaded() throws InterruptedException {
ReflectionTestUtils.setField(ReactionLineData.class, "map", null);
MutableBoolean exceptionHappened = new MutableBoolean(false);
List<Thread> threads = new ArrayList<>();
for (int j = 0; j < 100; j++) {
Thread thread = new Thread() {
@Override
public void run() {
try {
assertNotNull(ReactionLineData.getByReactionType(UnknownTransitionReaction.class));
} catch (Throwable e) {
exceptionHappened.setTrue();
e.printStackTrace();
}
}
};
threads.add(thread);
}
for (Thread thread : threads) {
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
assertFalse(exceptionHappened.booleanValue());
}
@Test
public void testGetByCellDesignerStringMultithreaded() throws InterruptedException {
ReflectionTestUtils.setField(ReactionLineData.class, "cellDesignerMap", null);
MutableBoolean exceptionHappened = new MutableBoolean(false);
List<Thread> threads = new ArrayList<>();
for (int j = 0; j < 100; j++) {
Thread thread = new Thread() {
@Override
public void run() {
try {
assertNotNull(ReactionLineData.getByCellDesignerString("UNKNOWN_TRANSITION"));
} catch (Throwable e) {
exceptionHappened.setTrue();
e.printStackTrace();
}
}
};
threads.add(thread);
}
for (Thread thread : threads) {
thread.start();
}
for (Thread thread : threads) {
thread.join();
}
assertFalse(exceptionHappened.booleanValue());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment