### Question:
RateLimiterController implements TrafficShapingController { @Override public boolean canPass(Node node, int acquireCount) { return canPass(node, acquireCount, false); } RateLimiterController(int timeOut, double count); @Override boolean canPass(Node node, int acquireCount); @Override boolean canPass(Node node, int acquireCount, boolean prioritized);  }### Answer:
@Test public void testPaceController_normal() throws InterruptedException { RateLimiterController paceController = new RateLimiterController(500, 10d); Node node = mock(Node.class); long start = TimeUtil.currentTimeMillis(); for (int i = 0; i < 6; i++) { assertTrue(paceController.canPass(node, 1)); } long end = TimeUtil.currentTimeMillis(); assertTrue((end - start) > 400); }
@Test public void testPaceController_timeout() throws InterruptedException { final RateLimiterController paceController = new RateLimiterController(500, 10d); final Node node = mock(Node.class); final AtomicInteger passcount = new AtomicInteger(); final AtomicInteger blockcount = new AtomicInteger(); final CountDownLatch countDown = new CountDownLatch(1); final AtomicInteger done = new AtomicInteger(); for (int i = 0; i < 10; i++) { Thread thread = new Thread(new Runnable() { @Override public void run() { boolean pass = paceController.canPass(node, 1); if (pass) { passcount.incrementAndGet(); } else { blockcount.incrementAndGet(); } done.incrementAndGet(); if (done.get() >= 10) { countDown.countDown(); } } }, "Thread " + i); thread.start(); } countDown.await(); System.out.println("pass:" + passcount.get()); System.out.println("block" + blockcount.get()); System.out.println("done" + done.get()); assertTrue(blockcount.get() > 0); }
@Test public void testPaceController_zeroattack() throws InterruptedException { RateLimiterController paceController = new RateLimiterController(500, 0d); Node node = mock(Node.class); for (int i = 0; i < 2; i++) { assertFalse(paceController.canPass(node, 1)); assertTrue(paceController.canPass(node, 0)); } }
@Test public void testPaceController_timeout() throws InterruptedException { final RateLimiterController paceController = new RateLimiterController(500, 10d); final Node node = mock(Node.class); final AtomicInteger passcount = new AtomicInteger(); final AtomicInteger blockcount = new AtomicInteger(); final CountDownLatch countDown = new CountDownLatch(1); final AtomicInteger done = new AtomicInteger(); for (int i = 0; i < 10; i++) { Thread thread = new Thread(new Runnable() { @Override public void run() { boolean pass = paceController.canPass(node, 1); if (pass == true) { passcount.incrementAndGet(); } else { blockcount.incrementAndGet(); } done.incrementAndGet(); if (done.get() >= 10) { countDown.countDown(); } } }, "Thread " + i); thread.start(); } countDown.await(); System.out.println("pass:" + passcount.get()); System.out.println("block" + blockcount.get()); System.out.println("done" + done.get()); assertTrue(blockcount.get() > 0); }