### Question:
ClusterMetric { public int tryOccupyNext(ClusterFlowEvent event, int acquireCount, double threshold) { double latestQps = getAvg(ClusterFlowEvent.PASS); if (!canOccupy(event, acquireCount, latestQps, threshold)) { return 0; } metric.addOccupyPass(acquireCount); add(ClusterFlowEvent.WAITING, acquireCount); return 1000 / metric.getSampleCount(); } ClusterMetric(int sampleCount, int intervalInMs); void add(ClusterFlowEvent event, long count); long getCurrentCount(ClusterFlowEvent event); long getSum(ClusterFlowEvent event); double getAvg(ClusterFlowEvent event); int tryOccupyNext(ClusterFlowEvent event, int acquireCount, double threshold);  }### Answer:
@Test public void testTryOccupyNext() { setCurrentMillis(System.currentTimeMillis()); ClusterMetric metric = new ClusterMetric(5, 25); metric.add(ClusterFlowEvent.PASS, 1); metric.add(ClusterFlowEvent.PASS, 2); metric.add(ClusterFlowEvent.PASS, 1); metric.add(ClusterFlowEvent.BLOCK, 1); Assert.assertEquals(4, metric.getSum(ClusterFlowEvent.PASS)); Assert.assertEquals(1, metric.getSum(ClusterFlowEvent.BLOCK)); Assert.assertEquals(160, metric.getAvg(ClusterFlowEvent.PASS), 0.01); Assert.assertEquals(200, metric.tryOccupyNext(ClusterFlowEvent.PASS, 111, 900)); metric.add(ClusterFlowEvent.PASS, 1); metric.add(ClusterFlowEvent.PASS, 2); metric.add(ClusterFlowEvent.PASS, 1); Assert.assertEquals(200, metric.tryOccupyNext(ClusterFlowEvent.PASS, 222, 900)); metric.add(ClusterFlowEvent.PASS, 1); metric.add(ClusterFlowEvent.PASS, 2); metric.add(ClusterFlowEvent.PASS, 1); Assert.assertEquals(0, metric.tryOccupyNext(ClusterFlowEvent.PASS, 333, 900)); }