@Test public void testTimeouts() throws InterruptedException { TestTicker ticker = new TestTicker(); Timeouts timeouts = new Timeouts(ticker, 64, 128); try { long now = ticker.currentTimeMillis(); long in1000 = now + 1000L; for (int i = 0; i < 1000; i++) timeouts.add(5000 + i, in1000); timeouts.add(42L, now); long in50 = now + 50; timeouts.add(142L, in50); timeouts.add(143L, in50); timeouts.add(144L, in50); final List<Long> ll = new ArrayList<>(); timeouts.removeExpired(new Timeouts.TimeoutHandler() { public void expired(long hashEntryAdr) { ll.add(hashEntryAdr); } }); assertEquals(ll.size(), 1); assertEquals(ll.get(0), Long.valueOf(42L)); ll.clear(); timeouts.removeExpired(new Timeouts.TimeoutHandler() { public void expired(long hashEntryAdr) { ll.add(hashEntryAdr); } }); assertEquals(ll.size(), 0); ticker.addMillis(100L); ll.clear(); timeouts.removeExpired(new Timeouts.TimeoutHandler() { public void expired(long hashEntryAdr) { ll.add(hashEntryAdr); } }); timeouts.remove(143L, in50); assertEquals(ll.size(), 3); assertTrue(ll.containsAll(Arrays.asList(142L, 144L))); ll.clear(); timeouts.removeExpired(new Timeouts.TimeoutHandler() { public void expired(long hashEntryAdr) { ll.add(hashEntryAdr); } }); assertEquals(ll.size(), 0); ticker.addMillis(1000L); ll.clear(); timeouts.removeExpired(new Timeouts.TimeoutHandler() { public void expired(long hashEntryAdr) { ll.add(hashEntryAdr); } }); assertEquals(ll.size(), 1000); } finally { timeouts.release(); } }
Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; }
Timeouts { Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; } }
Timeouts { Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; } Timeouts(Ticker ticker, int slots, long precision); }
Timeouts { Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; } Timeouts(Ticker ticker, int slots, long precision);  }
Timeouts { Timeouts(Ticker ticker, int slots, long precision) { if (slots == 0) slots = 64; if (precision == 0) precision = 128; if (slots < 1) throw new IllegalArgumentException("timeouts-slots <= 0"); if (precision < 1) throw new IllegalArgumentException("precision <= 0"); this.ticker = ticker; this.slotCount = Ints.checkedCast(Util.roundUpToPowerOf2(Math.max(slots, 16), 1 << 30)); int slotShift = 64 - Util.bitNum(slotCount) - 1; this.slotBitmask = ((long) slotCount - 1) << slotShift; this.slots = new Slot[slotCount]; for (int i = 0; i < slotCount; i++) this.slots[i] = new Slot(); precision = Util.roundUpToPowerOf2(Math.max(precision, 1), 1 << 30); precisionShift = 64 - Util.bitNum(precision) - 1; } Timeouts(Ticker ticker, int slots, long precision);   }