|
70 | 70 | import java.util.List; |
71 | 71 | import java.util.Map; |
72 | 72 | import java.util.concurrent.ExecutionException; |
| 73 | +import java.util.concurrent.atomic.AtomicInteger; |
73 | 74 | import org.easymock.EasyMock; |
74 | 75 | import org.junit.After; |
75 | 76 | import org.junit.Before; |
@@ -1448,5 +1449,43 @@ public void run() { |
1448 | 1449 | flushWaiter.join(1000); |
1449 | 1450 | assertFalse(flushWaiter.isAlive()); |
1450 | 1451 | } |
| 1452 | + |
| 1453 | + @Test |
| 1454 | + public void testFlushStress() throws InterruptedException { |
| 1455 | + SettableApiFuture<WriteLogEntriesResponse> mockRpcResponse = SettableApiFuture.create(); |
| 1456 | + mockRpcResponse.set(null); |
| 1457 | + replay(rpcFactoryMock); |
| 1458 | + logging = options.getService(); |
| 1459 | + WriteLogEntriesRequest request = WriteLogEntriesRequest.newBuilder() |
| 1460 | + .addAllEntries(Iterables.transform(ImmutableList.of(LOG_ENTRY1), |
| 1461 | + LogEntry.toPbFunction(PROJECT))) |
| 1462 | + .build(); |
| 1463 | + |
| 1464 | + Thread[] threads = new Thread[100]; |
| 1465 | + EasyMock.expect(loggingRpcMock.write(request)).andReturn(mockRpcResponse).times(threads.length); |
| 1466 | + EasyMock.replay(loggingRpcMock); |
| 1467 | + |
| 1468 | + // log and flush concurrently in many threads to trigger a ConcurrentModificationException |
| 1469 | + final AtomicInteger exceptions = new AtomicInteger(0); |
| 1470 | + for (int i = 0; i < threads.length; i++) { |
| 1471 | + threads[i] = new Thread() { |
| 1472 | + @Override |
| 1473 | + public void run() { |
| 1474 | + try { |
| 1475 | + logging.write(ImmutableList.of(LOG_ENTRY1)); |
| 1476 | + logging.flush(); |
| 1477 | + } catch (Exception ex) { |
| 1478 | + exceptions.incrementAndGet(); |
| 1479 | + } |
| 1480 | + } |
| 1481 | + }; |
| 1482 | + threads[i].start(); |
| 1483 | + } |
| 1484 | + for (int i = 0; i < threads.length; i++) { |
| 1485 | + threads[i].join(); |
| 1486 | + } |
| 1487 | + assertSame(0, exceptions.get()); |
| 1488 | + } |
| 1489 | + |
1451 | 1490 | } |
1452 | 1491 |
|
0 commit comments