|
29 | 29 | import software.amazon.awssdk.services.lambda.model.InvokeWithResponseStreamRequest; |
30 | 30 | import software.amazon.awssdk.services.lambda.model.InvokeWithResponseStreamResponseHandler; |
31 | 31 |
|
| 32 | +import java.io.ByteArrayOutputStream; |
32 | 33 | import java.util.List; |
33 | 34 | import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.CountDownLatch; |
| 36 | +import java.util.concurrent.ExecutorService; |
| 37 | +import java.util.concurrent.Executors; |
| 38 | +import java.util.concurrent.TimeUnit; |
34 | 39 |
|
| 40 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
35 | 41 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
36 | 42 | import static org.junit.jupiter.api.Assertions.assertTrue; |
37 | 43 | import static org.mockito.ArgumentMatchers.any; |
@@ -211,4 +217,102 @@ void testDocumentReconstruction_WithReconstructDisabled_ReturnsChunksSeparately( |
211 | 217 | // Then - without reconstruction, chunks remain separate |
212 | 218 | assertNotNull(result); |
213 | 219 | } |
| 220 | + |
| 221 | + @Test |
| 222 | + void testChunkWriting_WithConcurrentThreads_MaintainsDataIntegrity() throws InterruptedException { |
| 223 | + // This test verifies that ByteArrayOutputStream writes are thread-safe |
| 224 | + // Simulates the scenario where AWS SDK delivers chunks on different threads |
| 225 | + |
| 226 | + int numThreads = 10; |
| 227 | + int chunksPerThread = 10; |
| 228 | + int bytesPerChunk = 100; |
| 229 | + |
| 230 | + ByteArrayOutputStream testStream = new ByteArrayOutputStream(); |
| 231 | + ExecutorService executor = Executors.newFixedThreadPool(numThreads); |
| 232 | + |
| 233 | + CountDownLatch startLatch = new CountDownLatch(1); |
| 234 | + CountDownLatch doneLatch = new CountDownLatch(numThreads * chunksPerThread); |
| 235 | + |
| 236 | + // Track which chunks were written for verification |
| 237 | + java.util.Set<String> writtenChunks = java.util.concurrent.ConcurrentHashMap.newKeySet(); |
| 238 | + |
| 239 | + // Each thread writes multiple chunks concurrently |
| 240 | + for (int t = 0; t < numThreads; t++) { |
| 241 | + final int threadId = t; |
| 242 | + for (int c = 0; c < chunksPerThread; c++) { |
| 243 | + final int chunkId = c; |
| 244 | + executor.submit(() -> { |
| 245 | + try { |
| 246 | + startLatch.await(); // Synchronize start for maximum concurrency |
| 247 | + |
| 248 | + // Create a chunk with unique identifiable pattern |
| 249 | + byte[] chunk = new byte[bytesPerChunk]; |
| 250 | + // Fill chunk with a repeating pattern: [threadId, chunkId, threadId, chunkId, ...] |
| 251 | + for (int i = 0; i < bytesPerChunk; i++) { |
| 252 | + chunk[i] = (i % 2 == 0) ? (byte) threadId : (byte) chunkId; |
| 253 | + } |
| 254 | + |
| 255 | + // Track this chunk for later verification |
| 256 | + writtenChunks.add(threadId + "-" + chunkId); |
| 257 | + |
| 258 | + // Simulate the synchronized write that should happen in StreamingLambdaHandler |
| 259 | + synchronized (testStream) { |
| 260 | + testStream.write(chunk); |
| 261 | + } |
| 262 | + |
| 263 | + doneLatch.countDown(); |
| 264 | + } catch (Exception e) { |
| 265 | + // Fail the test if exception occurs |
| 266 | + throw new RuntimeException("Chunk write failed", e); |
| 267 | + } |
| 268 | + }); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + // Start all threads simultaneously |
| 273 | + startLatch.countDown(); |
| 274 | + |
| 275 | + // Wait for all writes to complete |
| 276 | + boolean completed = doneLatch.await(10, TimeUnit.SECONDS); |
| 277 | + assertTrue(completed, "All chunk writes should complete within timeout"); |
| 278 | + |
| 279 | + executor.shutdown(); |
| 280 | + boolean terminated = executor.awaitTermination(5, TimeUnit.SECONDS); |
| 281 | + assertTrue(terminated, "Executor should terminate"); |
| 282 | + |
| 283 | + // Verify 1: Total size should equal expected total bytes (no data loss) |
| 284 | + int expectedSize = numThreads * chunksPerThread * bytesPerChunk; |
| 285 | + assertEquals(expectedSize, testStream.size(), |
| 286 | + "Total bytes written should match expected size (no data loss)"); |
| 287 | + |
| 288 | + // Verify 2: All chunks were written (no missing chunks) |
| 289 | + assertEquals(numThreads * chunksPerThread, writtenChunks.size(), |
| 290 | + "All chunks should have been written"); |
| 291 | + |
| 292 | + // Verify 3: Chunk integrity - each chunk should be complete and not interleaved |
| 293 | + byte[] allBytes = testStream.toByteArray(); |
| 294 | + int chunkCount = allBytes.length / bytesPerChunk; |
| 295 | + |
| 296 | + for (int i = 0; i < chunkCount; i++) { |
| 297 | + int offset = i * bytesPerChunk; |
| 298 | + |
| 299 | + // Extract this chunk |
| 300 | + byte[] extractedChunk = new byte[bytesPerChunk]; |
| 301 | + System.arraycopy(allBytes, offset, extractedChunk, 0, bytesPerChunk); |
| 302 | + |
| 303 | + // Verify chunk has consistent pattern (not interleaved) |
| 304 | + // First byte should be threadId, alternating with chunkId |
| 305 | + byte threadId = extractedChunk[0]; |
| 306 | + byte chunkId = extractedChunk[1]; |
| 307 | + |
| 308 | + for (int j = 0; j < bytesPerChunk; j++) { |
| 309 | + byte expected = (j % 2 == 0) ? threadId : chunkId; |
| 310 | + assertEquals(expected, extractedChunk[j], |
| 311 | + String.format("Chunk %d byte %d has wrong value - chunk may be corrupted/interleaved", i, j)); |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + // If we reach here, all chunks maintained their integrity - no interleaving occurred |
| 316 | + // This proves the synchronized block prevents data corruption |
| 317 | + } |
214 | 318 | } |
0 commit comments