|  | 
| 3 | 3 | import org.junit.Test; | 
| 4 | 4 | import org.junit.jupiter.api.Assertions; | 
| 5 | 5 | 
 | 
|  | 6 | +import java.util.Random; | 
|  | 7 | +import java.util.concurrent.CountDownLatch; | 
| 6 | 8 | import java.util.concurrent.ExecutorService; | 
| 7 | 9 | import java.util.concurrent.Executors; | 
| 8 | 10 | 
 | 
| 9 |  | -public class TestCountDownLatch { | 
|  | 11 | +public class TestFakeCountDownLatch { | 
| 10 | 12 | 
 | 
| 11 | 13 |  @Test | 
| 12 |  | - public void testCountDown() throwsInterruptedException{ | 
| 13 |  | - int threadCount = 10, diff = 4; | 
| 14 |  | - CountDownLatch latchSingleThread = new CountDownLatch(threadCount); | 
|  | 14 | + public void singleThreadTest(){ | 
|  | 15 | + int threadCount = 10; | 
|  | 16 | + FakeCountDownLatch latchSingleThread = new FakeCountDownLatch(threadCount); | 
| 15 | 17 |  Assertions.assertEquals(threadCount, latchSingleThread.getCount()); | 
| 16 | 18 |  latchSingleThread.countDown(); | 
| 17 | 19 |  latchSingleThread.countDown(); | 
| 18 | 20 |  Assertions.assertEquals(threadCount-2, latchSingleThread.getCount()); | 
|  | 21 | + } | 
| 19 | 22 | 
 | 
|  | 23 | + @Test | 
|  | 24 | + public void testCountDown() throws InterruptedException { | 
|  | 25 | + int threadCount = 10, diff = 4; | 
|  | 26 | + | 
|  | 27 | + FakeCountDownLatch latch = new FakeCountDownLatch(threadCount); | 
|  | 28 | + CountDownLatch realLatch = new CountDownLatch(threadCount); | 
| 20 | 29 | 
 | 
| 21 |  | - CountDownLatch latch = new CountDownLatch(threadCount); | 
| 22 |  | - java.util.concurrent.CountDownLatch realLatch = new | 
| 23 |  | - java.util.concurrent.CountDownLatch(threadCount - diff); | 
| 24 | 30 |  ExecutorService service = Executors.newFixedThreadPool(threadCount); | 
| 25 | 31 |  for (int i = 0; i < threadCount; i++) { | 
| 26 | 32 |  service.submit(() -> { | 
| 27 | 33 |  try { | 
| 28 |  | - Thread.sleep(1000); | 
|  | 34 | + int rand = new Random().nextInt(); | 
|  | 35 | + if(rand < 0){ | 
|  | 36 | + rand = rand*-1; | 
|  | 37 | + } | 
|  | 38 | + rand = rand%1000; | 
|  | 39 | + Thread.sleep(rand); | 
| 29 | 40 |  } catch (InterruptedException e) { | 
| 30 | 41 |  e.printStackTrace(); | 
| 31 | 42 |  } | 
| 32 | 43 |  latch.countDown(); | 
| 33 | 44 |  realLatch.countDown(); | 
| 34 |  | - System.out.println(latch.getCount() + " - count " + threadCount); | 
| 35 | 45 |  }); | 
| 36 | 46 |  } | 
| 37 | 47 |  realLatch.await(); | 
| 38 |  | - latch.await(); | 
| 39 |  | - Assertions.assertEquals(diff, latch.getCount()); | 
|  | 48 | + Assertions.assertEquals(0, latch.getCount()); | 
| 40 | 49 |  } | 
| 41 | 50 | 
 | 
| 42 | 51 | 
 | 
| 43 | 52 |  @Test | 
| 44 | 53 |  public void testAwait() throws InterruptedException { | 
| 45 |  | - CountDownLatch latch = new CountDownLatch(3); | 
|  | 54 | + FakeCountDownLatch latch = new FakeCountDownLatch(3); | 
| 46 | 55 |  Thread t1 = new Thread(() -> { | 
| 47 | 56 |  try { | 
| 48 | 57 |  Thread.sleep(1000); | 
|  | 
0 commit comments