|
34 | 34 | import java.util.Map; |
35 | 35 | import java.util.Objects; |
36 | 36 | import java.util.Optional; |
| 37 | +import java.util.concurrent.CountDownLatch; |
37 | 38 | import java.util.concurrent.ExecutorService; |
| 39 | +import java.util.concurrent.Executors; |
38 | 40 | import java.util.concurrent.SynchronousQueue; |
39 | 41 | import java.util.concurrent.ThreadPoolExecutor; |
40 | 42 | import java.util.concurrent.TimeUnit; |
@@ -820,6 +822,37 @@ public void testRejectedExecutionError() { |
820 | 822 | } |
821 | 823 | } |
822 | 824 |
|
| 825 | + @Test |
| 826 | + public void testThreadStarvationBeforeWorkflowThreadStarts() throws InterruptedException { |
| 827 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 828 | + CountDownLatch executorThreadOccupied = new CountDownLatch(1); |
| 829 | + CountDownLatch releaseExecutorThread = new CountDownLatch(1); |
| 830 | + executor.submit( |
| 831 | + () -> { |
| 832 | + executorThreadOccupied.countDown(); |
| 833 | + releaseExecutorThread.await(); |
| 834 | + return null; |
| 835 | + }); |
| 836 | + executorThreadOccupied.await(); |
| 837 | + |
| 838 | + DeterministicRunner runner = |
| 839 | + new DeterministicRunnerImpl( |
| 840 | + executor::submit, |
| 841 | + DummySyncWorkflowContext.newDummySyncWorkflowContext(), |
| 842 | + () -> fail("workflow code should not start while the executor thread is occupied")); |
| 843 | + |
| 844 | + try { |
| 845 | + PotentialDeadlockException e = |
| 846 | + Assert.assertThrows( |
| 847 | + PotentialDeadlockException.class, () -> runner.runUntilAllBlocked(10)); |
| 848 | + assertTrue(e.getMessage().contains("could not start executing within 10ms")); |
| 849 | + } finally { |
| 850 | + executor.shutdownNow(); |
| 851 | + releaseExecutorThread.countDown(); |
| 852 | + assertTrue(executor.awaitTermination(10, TimeUnit.SECONDS)); |
| 853 | + } |
| 854 | + } |
| 855 | + |
823 | 856 | @Test |
824 | 857 | public void testCloseBlockedUntilDone() throws InterruptedException { |
825 | 858 | final int THREAD_SLEEP_MS = 2000; |
|
0 commit comments