|
20 | 20 | import static org.junit.jupiter.api.Assertions.assertNull; |
21 | 21 | import static org.junit.jupiter.api.Assertions.assertSame; |
22 | 22 |
|
| 23 | +import java.util.ArrayList; |
23 | 24 | import java.util.Arrays; |
24 | 25 | import java.util.Collections; |
25 | 26 | import java.util.HashSet; |
| 27 | +import java.util.List; |
26 | 28 | import java.util.Set; |
| 29 | +import java.util.concurrent.ExecutorService; |
| 30 | +import java.util.concurrent.Executors; |
| 31 | +import java.util.concurrent.Future; |
| 32 | +import java.util.concurrent.TimeUnit; |
27 | 33 |
|
| 34 | +import org.apache.commons.jxpath.DynamicPropertyHandler; |
28 | 35 | import org.junit.jupiter.api.Test; |
29 | 36 |
|
30 | 37 | class ValueUtilsTest { |
@@ -87,4 +94,47 @@ void testGetValueFromSetNegativeIndex() { |
87 | 94 | void testGetValueFromSetTooSmall() { |
88 | 95 | assertNull(ValueUtils.getValue(Collections.EMPTY_SET, 2)); |
89 | 96 | } |
| 97 | + |
| 98 | + @Test |
| 99 | + void testGetDynamicPropertyHandlerConcurrently() throws InterruptedException { |
| 100 | + // This test is to ensure that the dynamic property handler can be accessed concurrently |
| 101 | + // without throwing any exceptions. It does not assert any specific behavior, but rather |
| 102 | + // ensures that no exceptions are thrown during concurrent access. |
| 103 | + int nThreads = 200; // Number of threads to simulate concurrent access |
| 104 | + List<Future<?>> futures = new ArrayList<>(); |
| 105 | + ExecutorService threadPool = Executors.newFixedThreadPool(nThreads); |
| 106 | + for (int i = 0; i < nThreads; i++) { |
| 107 | + futures.add(threadPool.submit(() -> ValueUtils.getDynamicPropertyHandler(DummyHandler.class))); |
| 108 | + } |
| 109 | + |
| 110 | + threadPool.shutdown(); |
| 111 | + threadPool.awaitTermination(1, TimeUnit.SECONDS); |
| 112 | + |
| 113 | + for (Future<?> future : futures) { |
| 114 | + try { |
| 115 | + future.get(); // This will throw an exception if any thread encountered an issue |
| 116 | + } catch (Exception e) { |
| 117 | + // If an exception is thrown, the test fails |
| 118 | + throw new AssertionError("Exception thrown during concurrent access", e); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static class DummyHandler implements DynamicPropertyHandler { |
| 124 | + |
| 125 | + @Override |
| 126 | + public Object getProperty(Object object, String propertyName) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public String[] getPropertyNames(Object object) { |
| 132 | + return new String[0]; |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void setProperty(Object object, String propertyName, Object value) { |
| 137 | + |
| 138 | + } |
| 139 | + } |
90 | 140 | } |
0 commit comments