/** * Take an existing Reservation that we know is legit and randomly decide to * either queue it for a later update or delete transaction * @param r */ protected void requeueReservation(Reservation r) { int idx = rng.nextInt(100); if (idx > 20) return; // Queue this motha trucka up for a deletin' or an updatin' CacheType ctype = null; if (rng.nextBoolean()) { ctype = CacheType.PENDING_DELETES; } else { ctype = CacheType.PENDING_UPDATES; } assert(ctype != null); Buffer<Reservation> cache = CACHE_RESERVATIONS.get(ctype); assert(cache != null); cache.add(r); if (debug.val) LOG.debug(String.format("Queued %s for %s [cacheSize=%d]\nFlightId: %d\nCustomerId: %d", r, ctype, cache.size(), r.flight_id, r.customer_id)); }
/** * Tests {@link BlockingBuffer#get()} in combination with {@link BlockingBuffer#add()} using multiple read threads. * <p/> * Two read threads should block on an empty buffer until one object * is added then both threads should complete. */ public void testBlockedGetWithAdd() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); // run methods will get and compare -- must wait for add Thread thread1 = new ReadThread(blockingBuffer, obj); Thread thread2 = new ReadThread(blockingBuffer, obj); thread1.start(); thread2.start(); // give hungry read threads ample time to hang delay(); // notifyAll should allow both read threads to complete blockingBuffer.add(obj); // allow notified threads to complete delay(); // There should not be any threads waiting. if (thread1.isAlive() || thread2.isAlive()) fail("Live thread(s) when both should be dead."); }
/** * Tests {@link BlockingBuffer#get()} in combination with {@link BlockingBuffer#addAll()} using multiple read threads. * <p/> * Two read threads should block on an empty buffer until a * singleton is added then both threads should complete. */ public void testBlockedGetWithAddAll() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); // run methods will get and compare -- must wait for addAll Thread thread1 = new ReadThread(blockingBuffer, obj); Thread thread2 = new ReadThread(blockingBuffer, obj); thread1.start(); thread2.start(); // give hungry read threads ample time to hang delay(); // notifyAll should allow both read threads to complete blockingBuffer.addAll(Collections.singleton(obj)); // allow notified threads to complete delay(); // There should not be any threads waiting. if (thread1.isAlive() || thread2.isAlive()) fail("Live thread(s) when both should be dead."); }
/** * Tests interrupted {@link BlockingBuffer#get()}. */ public void testInterruptedGet() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); // spawn a read thread to wait on the empty buffer ArrayList exceptionList = new ArrayList(); Thread thread = new ReadThread(blockingBuffer, obj, exceptionList); thread.start(); // Interrupting the thread should cause it to throw BufferUnderflowException thread.interrupt(); // Chill, so thread can throw and add message to exceptionList delay(); assertTrue("Thread interrupt should have led to underflow", exceptionList.contains("BufferUnderFlow")); if (thread.isAlive()) { fail("Read thread has hung."); } }
/** * Tests interrupted remove. */ public void testInterruptedRemove() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); // spawn a read thread to wait on the empty buffer ArrayList exceptionList = new ArrayList(); Thread thread = new ReadThread(blockingBuffer, obj, exceptionList, "remove"); thread.start(); // Interrupting the thread should cause it to throw BufferUnderflowException thread.interrupt(); // Chill, so thread can throw and add message to exceptionList delay(); assertTrue("Thread interrupt should have led to underflow", exceptionList.contains("BufferUnderFlow")); if (thread.isAlive()) { fail("Read thread has hung."); } }
/** * Tests that the removal operation actually removes the first element. */ public void testCircularFifoBufferCircular() { List list = new ArrayList(); list.add("A"); list.add("B"); list.add("C"); Buffer buf = new CircularFifoBuffer(list); assertEquals(true, buf.contains("A")); assertEquals(true, buf.contains("B")); assertEquals(true, buf.contains("C")); buf.add("D"); assertEquals(false, buf.contains("A")); assertEquals(true, buf.contains("B")); assertEquals(true, buf.contains("C")); assertEquals(true, buf.contains("D")); assertEquals("B", buf.get()); assertEquals("B", buf.remove()); assertEquals("C", buf.remove()); assertEquals("D", buf.remove()); }
protected final void clearCache() { for (BitSet seats : CACHE_BOOKED_SEATS.values()) { seats.clear(); } // FOR for (Buffer<Reservation> queue : CACHE_RESERVATIONS.values()) { queue.clear(); } // FOR for (Set<Long> f_ids : CACHE_CUSTOMER_BOOKED_FLIGHTS.values()) { synchronized (f_ids) { f_ids.clear(); } // SYNCH } // FOR }
public void testTransformedBuffer() { Buffer buffer = TransformedBuffer.decorate(new ArrayStack(), TestTransformedCollection.STRING_TO_INTEGER_TRANSFORMER); assertEquals(0, buffer.size()); Object[] els = new Object[]{"1", "3", "5", "7", "2", "4", "6"}; for (int i = 0; i < els.length; i++) { buffer.add(els[i]); assertEquals(i + 1, buffer.size()); assertEquals(true, buffer.contains(new Integer((String) els[i]))); assertEquals(false, buffer.contains(els[i])); } assertEquals(false, buffer.remove(els[0])); assertEquals(true, buffer.remove(new Integer((String) els[0]))); }
/** * Tests {@link BlockingBuffer#get()} in combination with {@link BlockingBuffer#add()}. */ public void testGetWithAdd() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); new DelayedAdd(blockingBuffer, obj).start(); // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer . assertSame(obj, blockingBuffer.get()); }
/** * Tests {@link BlockingBuffer#get()} in combination with {@link BlockingBuffer#addAll()}. */ public void testGetWithAddAll() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); new DelayedAddAll(blockingBuffer, obj).start(); // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer . assertSame(obj, blockingBuffer.get()); }
/** * Tests {@link BlockingBuffer#remove()} in combination with {@link BlockingBuffer#add()}. */ public void testRemoveWithAdd() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); new DelayedAdd(blockingBuffer, obj).start(); // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer . assertSame(obj, blockingBuffer.remove()); }
/** * Tests {@link BlockingBuffer#remove()} in combination with {@link BlockingBuffer#addAll()}. */ public void testRemoveWithAddAll() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); new DelayedAddAll(blockingBuffer, obj).start(); // verify does not throw BufferUnderflowException; should block until other thread has added to the buffer . assertSame(obj, blockingBuffer.remove()); }
/** * Tests {@link BlockingBuffer#remove()} in combination with {@link BlockingBuffer#add()} using multiple read threads. * <p/> * Two read threads should block on an empty buffer until one * object is added then one thread should complete. The remaining * thread should complete after the addition of a second object. */ public void testBlockedRemoveWithAdd() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); // run methods will remove and compare -- must wait for add Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove"); Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove"); thread1.start(); thread2.start(); // give hungry read threads ample time to hang delay(); blockingBuffer.add(obj); // allow notified threads to complete delay(); // There should be one thread waiting. assertTrue("There is one thread waiting", thread1.isAlive() ^ thread2.isAlive()); blockingBuffer.add(obj); // allow notified thread to complete delay(); // There should not be any threads waiting. if (thread1.isAlive() || thread2.isAlive()) fail("Live thread(s) when both should be dead."); }
/** * Tests {@link BlockingBuffer#remove()} in combination with {@link BlockingBuffer#addAll()} using multiple read threads. * <p/> * Two read threads should block on an empty buffer until a * singleton collection is added then one thread should * complete. The remaining thread should complete after the * addition of a second singleton. */ public void testBlockedRemoveWithAddAll1() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj = new Object(); // run methods will remove and compare -- must wait for addAll Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove"); Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove"); thread1.start(); thread2.start(); // give hungry read threads ample time to hang delay(); blockingBuffer.addAll(Collections.singleton(obj)); // allow notified threads to complete delay(); // There should be one thread waiting. assertTrue("There is one thread waiting", thread1.isAlive() ^ thread2.isAlive()); blockingBuffer.addAll(Collections.singleton(obj)); // allow notified thread to complete delay(); // There should not be any threads waiting. if (thread1.isAlive() || thread2.isAlive()) fail("Live thread(s) when both should be dead."); }
/** * Tests {@link BlockingBuffer#remove()} in combination with {@link BlockingBuffer#addAll()} using multiple read threads. * <p/> * Two read threads should block on an empty buffer until a * collection with two distinct objects is added then both * threads should complete. Each thread should have read a * different object. */ public void testBlockedRemoveWithAddAll2() { Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer()); Object obj1 = new Object(); Object obj2 = new Object(); Set objs = Collections.synchronizedSet(new HashSet()); objs.add(obj1); objs.add(obj2); // run methods will remove and compare -- must wait for addAll Thread thread1 = new ReadThread(blockingBuffer, objs, "remove"); Thread thread2 = new ReadThread(blockingBuffer, objs, "remove"); thread1.start(); thread2.start(); // give hungry read threads ample time to hang delay(); blockingBuffer.addAll(objs); // allow notified threads to complete delay(); assertEquals("Both objects were removed", 0, objs.size()); // There should not be any threads waiting. if (thread1.isAlive() || thread2.isAlive()) fail("Live thread(s) when both should be dead."); }
ReadThread(Buffer buffer, Object obj, ArrayList exceptionList, String action) { super(); this.buffer = buffer; this.obj = obj; this.exceptionList = exceptionList; this.action = action; }
public void testBufferRemove() { resetEmpty(); Buffer buffer = (Buffer) collection; try { buffer.remove(); fail(); } catch (UnsupportedOperationException ex) { } }
public void testGet() { Buffer buffer = makeTestBuffer(); try { Object o = buffer.get(); fail("Expecting BufferUnderflowException"); } catch (BufferUnderflowException ex) { // expected } buffer.add("one"); buffer.add("two"); buffer.add("three"); assertEquals("Buffer get", buffer.get(), "three"); }
public void testRemove() { Buffer buffer = makeTestBuffer(); buffer.add("one"); assertEquals("Buffer get", buffer.remove(), "one"); try { buffer.remove(); fail("Expecting BufferUnderflowException"); } catch (BufferUnderflowException ex) { // expected } }
/** * Extracts the weak components from a graph. * @param graph the graph whose weak components are to be extracted * @return the list of weak components */ public Set<Set<V>> transform(Graph<V,E> graph) { Set<Set<V>> clusterSet = new HashSet<Set<V>>(); HashSet<V> unvisitedVertices = new HashSet<V>(graph.getVertices()); while (!unvisitedVertices.isEmpty()) { Set<V> cluster = new HashSet<V>(); V root = unvisitedVertices.iterator().next(); unvisitedVertices.remove(root); cluster.add(root); Buffer<V> queue = new UnboundedFifoBuffer<V>(); queue.add(root); while (!queue.isEmpty()) { V currentVertex = queue.remove(); Collection<V> neighbors = graph.getNeighbors(currentVertex); for(V neighbor : neighbors) { if (unvisitedVertices.contains(neighbor)) { queue.add(neighbor); unvisitedVertices.remove(neighbor); cluster.add(neighbor); } } } clusterSet.add(cluster); } return clusterSet; }
DelayedAdd(Buffer buffer, Object obj) { super(); this.buffer = buffer; this.obj = obj; }
DelayedAddAll(Buffer buffer, Object obj) { super(); this.buffer = buffer; this.obj = obj; }
ReadThread(Buffer buffer, Object obj) { super(); this.buffer = buffer; this.obj = obj; }
ReadThread(Buffer buffer, Object obj, ArrayList exceptionList) { super(); this.buffer = buffer; this.obj = obj; this.exceptionList = exceptionList; }
ReadThread(Buffer buffer, Set objs, String action) { super(); this.buffer = buffer; this.objs = objs; this.action = action; }
public Collection makeFullCollection() { Buffer buffer = new UnboundedFifoBuffer(); buffer.addAll(Arrays.asList(getFullElements())); return UnmodifiableBuffer.decorate(buffer); }
protected Buffer decorateBuffer(Buffer buffer, Predicate predicate) { return PredicatedBuffer.decorate(buffer, predicate); }
public Buffer makeTestBuffer() { return decorateBuffer(new ArrayStack(), testPredicate); }
public Collection makeFullCollection() { Buffer buffer = new UnboundedFifoBuffer(); buffer.addAll(Arrays.asList(getFullElements())); return SynchronizedBuffer.decorate(buffer); }
/** * Factory method to create an unmodifiable buffer. * <p/> * If the buffer passed in is already unmodifiable, it is returned. * * @param buffer the buffer to decorate, must not be null * @return an unmodifiable Buffer * @throws IllegalArgumentException if buffer is null */ public static <E> Buffer<E> decorate(Buffer<E> buffer) { if (buffer instanceof Unmodifiable) { return buffer; } return new UnmodifiableBuffer<E>(buffer); }
/** * Factory method to create a blocking buffer. * * @param buffer the buffer to decorate, must not be null * @return a new blocking Buffer * @throws IllegalArgumentException if buffer is null */ public static <E> Buffer<E> decorate(Buffer<E> buffer) { return new BlockingBuffer<E>(buffer); }
/** * Constructor that wraps (not copies). * * @param buffer the buffer to decorate, must not be null * @throws IllegalArgumentException if the buffer is null */ protected BlockingBuffer(Buffer<E> buffer) { super(buffer); }
/** * Factory method to create a predicated (validating) buffer. * <p/> * If there are any elements already in the buffer being decorated, they * are validated. * * @param buffer the buffer to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @return a new predicated Buffer * @throws IllegalArgumentException if buffer or predicate is null * @throws IllegalArgumentException if the buffer contains invalid elements */ public static <E> Buffer<E> decorate(Buffer<E> buffer, Predicate<? super E> predicate) { return new PredicatedBuffer(buffer, predicate); }
/** * Constructor that wraps (not copies). * <p/> * If there are any elements already in the collection being decorated, they * are validated. * * @param buffer the buffer to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @throws IllegalArgumentException if buffer or predicate is null * @throws IllegalArgumentException if the buffer contains invalid elements */ protected PredicatedBuffer(Buffer<E> buffer, Predicate<? super E> predicate) { super(buffer, predicate); }
/** * Gets the buffer being decorated. * * @return the decorated buffer */ protected Buffer<E> getBuffer() { return (Buffer<E>) getCollection(); }
/** * Factory method to create a synchronized buffer. * * @param buffer the buffer to decorate, must not be null * @return a new synchronized Buffer * @throws IllegalArgumentException if buffer is null */ public static <E> Buffer<E> decorate(Buffer<E> buffer) { return new SynchronizedBuffer(buffer); }
/** * Constructor that wraps (not copies). * * @param buffer the buffer to decorate, must not be null * @throws IllegalArgumentException if the buffer is null */ protected SynchronizedBuffer(Buffer<E> buffer) { super(buffer); }