8a723497c78489b7a0b7be12158663bf783b6f3e
java/com.sap.sse.test/src/com/sap/sse/test/TestRepeatingTaskCancelsUponException.java
| ... | ... | @@ -2,29 +2,39 @@ package com.sap.sse.test; |
| 2 | 2 | |
| 3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | 4 | |
| 5 | +import java.util.concurrent.BrokenBarrierException; |
|
| 6 | +import java.util.concurrent.CyclicBarrier; |
|
| 5 | 7 | import java.util.concurrent.TimeUnit; |
| 6 | 8 | |
| 7 | 9 | import org.junit.jupiter.api.Test; |
| 10 | +import org.junit.jupiter.api.Timeout; |
|
| 8 | 11 | |
| 9 | 12 | import com.sap.sse.util.ThreadPoolUtil; |
| 10 | 13 | |
| 14 | +@Timeout(value=10, unit=TimeUnit.SECONDS) |
|
| 11 | 15 | public class TestRepeatingTaskCancelsUponException { |
| 12 | 16 | int counter; |
| 13 | 17 | volatile int repetitions; |
| 18 | + final CyclicBarrier barrier = new CyclicBarrier(2); |
|
| 14 | 19 | |
| 15 | 20 | @Test |
| 16 | - public void testTerminationUponException() throws InterruptedException { |
|
| 21 | + public void testTerminationUponException() throws InterruptedException, BrokenBarrierException { |
|
| 17 | 22 | counter = 0; |
| 18 | 23 | repetitions = 100; |
| 19 | 24 | final long intervalInMillis = 10; |
| 20 | 25 | ThreadPoolUtil.INSTANCE.getDefaultBackgroundTaskThreadPoolExecutor().scheduleAtFixedRate(this::run, |
| 21 | 26 | intervalInMillis, intervalInMillis, TimeUnit.MILLISECONDS); |
| 22 | - Thread.sleep(intervalInMillis*repetitions+500); |
|
| 27 | + barrier.await(); |
|
| 23 | 28 | assertEquals(repetitions, counter); |
| 24 | 29 | } |
| 25 | 30 | |
| 26 | 31 | private void run() { |
| 27 | 32 | if (++counter == repetitions) { |
| 33 | + try { |
|
| 34 | + barrier.await(); |
|
| 35 | + } catch (InterruptedException | BrokenBarrierException e) { |
|
| 36 | + throw new RuntimeException(e); |
|
| 37 | + } |
|
| 28 | 38 | throw new RuntimeException("Terminating"); |
| 29 | 39 | } |
| 30 | 40 | } |