/** remove removes next element, or throws NSEE if empty */ public void testRemove() { DelayQueue q = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { assertEquals(new PDelay(i), q.remove()); } try { q.remove(); shouldThrow(); } catch (NoSuchElementException success) { } }
/** toArray(a) contains all elements */ public void testToArray2() { DelayQueue<PDelay> q = populatedQueue(SIZE); PDelay[] ints = new PDelay[SIZE]; PDelay[] array = q.toArray(ints); assertSame(ints, array); Arrays.sort(ints); for (int i = 0; i < ints.length; i++) assertSame(ints[i], q.remove()); }
/** * Remove named lease. Lease is removed from the list of leases and removed from the delay queue. * Lease can be resinserted using {@link #addLease(Lease)} * * @param leaseName name of lease * @throws LeaseException * @return Removed lease */ Lease removeLease(final String leaseName) throws LeaseException { Lease lease = null; synchronized (leaseQueue) { lease = leases.remove(leaseName); if (lease == null) { throw new LeaseException("lease '" + leaseName + "' does not exist"); } leaseQueue.remove(lease); } return lease; }
/** isEmpty is true before add, false after */ public void testEmpty() { DelayQueue q = new DelayQueue(); assertTrue(q.isEmpty()); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(new PDelay(1)); assertFalse(q.isEmpty()); q.add(new PDelay(2)); q.remove(); q.remove(); assertTrue(q.isEmpty()); }
/** removeAll(c) removes only those elements of c and reports true if changed */ public void testRemoveAll() { for (int i = 1; i < SIZE; ++i) { DelayQueue q = populatedQueue(SIZE); DelayQueue p = populatedQueue(i); assertTrue(q.removeAll(p)); assertEquals(SIZE - i, q.size()); for (int j = 0; j < i; ++j) { PDelay x = (PDelay) (p.remove()); assertFalse(q.contains(x)); } } }
/** * Renew a lease * * @param leaseName name of lease * @throws org.apache.hadoop.hbase.exceptions.LeaseException */ public void renewLease(final String leaseName) throws LeaseException { synchronized (leaseQueue) { Lease lease = leases.get(leaseName); // We need to check to see if the remove is successful as the poll in the run() // method could have completed between the get and the remove which will result // in a corrupt leaseQueue. if (lease == null || !leaseQueue.remove(lease)) { throw new LeaseException("lease '" + leaseName + "' does not exist or has already expired"); } lease.resetExpirationTime(); leaseQueue.add(lease); } }
/** retainAll(c) retains only those elements of c and reports true if changed */ public void testRetainAll() { DelayQueue q = populatedQueue(SIZE); DelayQueue p = populatedQueue(SIZE); for (int i = 0; i < SIZE; ++i) { boolean changed = q.retainAll(p); if (i == 0) assertFalse(changed); else assertTrue(changed); assertTrue(q.containsAll(p)); assertEquals(SIZE - i, q.size()); p.remove(); } }