|
7 | 7 | import com.fasterxml.jackson.databind.JsonNode;
|
8 | 8 | import com.fasterxml.jackson.databind.ObjectMapper;
|
9 | 9 | import java.util.Date;
|
| 10 | +import java.util.concurrent.CountDownLatch; |
10 | 11 | import java.util.concurrent.ExecutorService;
|
11 | 12 | import java.util.concurrent.Executors;
|
12 | 13 | import java.util.concurrent.TimeUnit;
|
13 | 14 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 15 | +import java.util.concurrent.atomic.AtomicInteger; |
14 | 16 | import org.junit.jupiter.api.Test;
|
15 | 17 |
|
16 | 18 | public class UtilsTest {
|
@@ -90,4 +92,90 @@ public void testParseUtcISODateNode() throws JsonProcessingException {
|
90 | 92 | assertNull(parsedDate);
|
91 | 93 | assertNull(parseUtcISODateNode(null));
|
92 | 94 | }
|
| 95 | + |
| 96 | + @Test |
| 97 | + public void testDateParsingThreadSafety() throws InterruptedException { |
| 98 | + final AtomicBoolean collisionDetected = new AtomicBoolean(false); |
| 99 | + final AtomicInteger unexpectedExceptions = new AtomicInteger(0); |
| 100 | + final AtomicInteger incorrectParseResults = new AtomicInteger(0); |
| 101 | + |
| 102 | + int numThreads = 20; // Spawn 20 threads |
| 103 | + int iterationsPerThread = 100; // Each thread will parse 100 dates |
| 104 | + ExecutorService pool = Executors.newFixedThreadPool(numThreads); |
| 105 | + |
| 106 | + CountDownLatch startLatch = new CountDownLatch(1); |
| 107 | + CountDownLatch finishLatch = new CountDownLatch(numThreads); |
| 108 | + |
| 109 | + // Expected date: 2024-05-01T16:13:26.651Z -> 1714580006651L |
| 110 | + final String testDateString = "\"2024-05-01T16:13:26.651Z\""; |
| 111 | + final long expectedTimestamp = 1714580006651L; |
| 112 | + |
| 113 | + try { |
| 114 | + for (int i = 0; i < numThreads; i++) { |
| 115 | + pool.execute( |
| 116 | + () -> { |
| 117 | + try { |
| 118 | + // Wait for all threads to start simultaneously |
| 119 | + startLatch.await(); |
| 120 | + |
| 121 | + ObjectMapper mapper = new ObjectMapper(); |
| 122 | + |
| 123 | + for (int j = 0; j < iterationsPerThread; j++) { |
| 124 | + try { |
| 125 | + JsonNode jsonNode = mapper.readTree(testDateString); |
| 126 | + Date parsedDate = parseUtcISODateNode(jsonNode); |
| 127 | + |
| 128 | + if (parsedDate == null || parsedDate.getTime() != expectedTimestamp) { |
| 129 | + incorrectParseResults.incrementAndGet(); |
| 130 | + collisionDetected.set(true); |
| 131 | + } |
| 132 | + |
| 133 | + // Also test the reverse operation |
| 134 | + Date originalDate = new Date(expectedTimestamp); |
| 135 | + String formattedDate = getISODate(originalDate); |
| 136 | + if (!formattedDate.equals("2024-05-01T16:13:26.651Z")) { |
| 137 | + incorrectParseResults.incrementAndGet(); |
| 138 | + collisionDetected.set(true); |
| 139 | + } |
| 140 | + |
| 141 | + } catch (Exception e) { |
| 142 | + unexpectedExceptions.incrementAndGet(); |
| 143 | + } |
| 144 | + } |
| 145 | + } catch (InterruptedException e) { |
| 146 | + Thread.currentThread().interrupt(); |
| 147 | + } finally { |
| 148 | + finishLatch.countDown(); |
| 149 | + } |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + // Start all threads simultaneously to maximize contention |
| 154 | + startLatch.countDown(); |
| 155 | + |
| 156 | + // Wait for all threads to complete |
| 157 | + assertTrue( |
| 158 | + finishLatch.await(30, TimeUnit.SECONDS), "Test threads did not complete within timeout"); |
| 159 | + |
| 160 | + } finally { |
| 161 | + pool.shutdown(); |
| 162 | + assertTrue( |
| 163 | + pool.awaitTermination(5, TimeUnit.SECONDS), |
| 164 | + "Thread pool did not shutdown within timeout"); |
| 165 | + } |
| 166 | + |
| 167 | + // Print diagnostic information |
| 168 | + System.out.println("Unexpected exceptions: " + unexpectedExceptions.get()); |
| 169 | + System.out.println("Incorrect parse results: " + incorrectParseResults.get()); |
| 170 | + System.out.println("Total operations: " + (numThreads * iterationsPerThread * 2)); |
| 171 | + |
| 172 | + String failureMessage = |
| 173 | + "SimpleDateFormat thread-safety issue detected! " |
| 174 | + + "Exceptions: " |
| 175 | + + unexpectedExceptions.get() |
| 176 | + + ", Incorrect results: " |
| 177 | + + incorrectParseResults.get(); |
| 178 | + assertFalse(collisionDetected.get(), failureMessage); |
| 179 | + assertEquals(0, unexpectedExceptions.get(), failureMessage); |
| 180 | + } |
93 | 181 | }
|
0 commit comments