|
31 | 31 | import org.slf4j.LoggerFactory; |
32 | 32 |
|
33 | 33 | import java.util.concurrent.Callable; |
| 34 | +import java.util.concurrent.CountDownLatch; |
34 | 35 | import java.util.concurrent.ExecutorService; |
35 | 36 | import java.util.concurrent.TimeUnit; |
36 | 37 |
|
37 | 38 | import static org.junit.Assert.assertEquals; |
38 | | -import static org.junit.Assert.assertFalse; |
39 | 39 |
|
40 | 40 | /** |
41 | 41 | * Basic test for S3A's blocking executor service. |
@@ -92,11 +92,12 @@ public void testSubmitRunnable() throws Exception { |
92 | 92 | */ |
93 | 93 | protected void verifyQueueSize(ExecutorService executorService, |
94 | 94 | int expectedQueueSize) { |
95 | | - StopWatch stopWatch = new StopWatch().start(); |
| 95 | + CountDownLatch latch = new CountDownLatch(1); |
96 | 96 | for (int i = 0; i < expectedQueueSize; i++) { |
97 | | - executorService.submit(sleeper); |
98 | | - assertDidntBlock(stopWatch); |
| 97 | + executorService.submit(new LatchedSleeper(latch)); |
99 | 98 | } |
| 99 | + StopWatch stopWatch = new StopWatch().start(); |
| 100 | + latch.countDown(); |
100 | 101 | executorService.submit(sleeper); |
101 | 102 | assertDidBlock(stopWatch); |
102 | 103 | } |
@@ -124,15 +125,6 @@ public void testChainedQueue() throws Throwable { |
124 | 125 |
|
125 | 126 | // Helper functions, etc. |
126 | 127 |
|
127 | | - private void assertDidntBlock(StopWatch sw) { |
128 | | - try { |
129 | | - assertFalse("Non-blocking call took too long.", |
130 | | - sw.now(TimeUnit.MILLISECONDS) > BLOCKING_THRESHOLD_MSEC); |
131 | | - } finally { |
132 | | - sw.reset().start(); |
133 | | - } |
134 | | - } |
135 | | - |
136 | 128 | private void assertDidBlock(StopWatch sw) { |
137 | 129 | try { |
138 | 130 | if (sw.now(TimeUnit.MILLISECONDS) < BLOCKING_THRESHOLD_MSEC) { |
@@ -164,6 +156,25 @@ public Integer call() throws Exception { |
164 | 156 | } |
165 | 157 | }; |
166 | 158 |
|
| 159 | + private class LatchedSleeper implements Runnable { |
| 160 | + private final CountDownLatch latch; |
| 161 | + |
| 162 | + LatchedSleeper(CountDownLatch latch) { |
| 163 | + this.latch = latch; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void run() { |
| 168 | + try { |
| 169 | + latch.await(); |
| 170 | + Thread.sleep(TASK_SLEEP_MSEC); |
| 171 | + } catch (InterruptedException e) { |
| 172 | + LOG.info("Thread {} interrupted.", Thread.currentThread().getName()); |
| 173 | + Thread.currentThread().interrupt(); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
167 | 178 | /** |
168 | 179 | * Helper function to create thread pool under test. |
169 | 180 | */ |
|
0 commit comments