You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by su...@apache.org on 2010/03/15 04:30:55 UTC
svn commit: r923044 - in
/synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons:
./ executors/ executors/queues/
Author: supun
Date: Mon Mar 15 03:30:55 2010
New Revision: 923044
URL: http://svn.apache.org/viewvc?rev=923044&view=rev
Log:
adding unit tests to the priority executors
Added:
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/DummyTask.java
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueAbstractTest.java
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueStressTest.java
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueTest.java
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/queues/
synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/queues/FixedQueueTest.java
Added: synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/DummyTask.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/DummyTask.java?rev=923044&view=auto
==============================================================================
--- synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/DummyTask.java (added)
+++ synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/DummyTask.java Mon Mar 15 03:30:55 2010
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.commons.executors;
+
+public class DummyTask implements Importance, Runnable {
+ private int priority = 0;
+
+ private int mark = 0;
+
+ public DummyTask(int priority) {
+ this.priority = priority;
+ }
+
+ public int getPriority() {
+ return priority;
+ }
+
+ public void setPriority(int p) {
+ this.priority = p;
+ }
+
+ public void setProperty(String name, Object value) {}
+
+ public Object getProperty(String name) {
+ return null;
+ }
+
+ public int getMark() {
+ return mark;
+ }
+
+ public void setMark(int mark) {
+ this.mark = mark;
+ }
+
+ public void run() {
+
+ }
+}
Added: synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueAbstractTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueAbstractTest.java?rev=923044&view=auto
==============================================================================
--- synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueAbstractTest.java (added)
+++ synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueAbstractTest.java Mon Mar 15 03:30:55 2010
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.commons.executors;
+
+import org.apache.synapse.commons.executors.queues.UnboundedQueue;
+import org.apache.synapse.commons.executors.queues.FixedSizeQueue;
+
+import java.util.List;
+import java.util.ArrayList;
+
+import junit.framework.TestCase;
+
+/**
+ * Provde common functionality for Concurrent and Single threaded testing of the
+ * MultiPriorityBlockingQueue.
+ */
+public abstract class MultiPriorityBlockingQueueAbstractTest extends TestCase {
+ protected MultiPriorityBlockingQueue<DummyTask> createUnboundedQueue(int noQueues, int[] priorities) {
+ List<InternalQueue<DummyTask>> internalQueueList = new ArrayList<InternalQueue<DummyTask>>();
+
+ for (int i = 0; i < noQueues; i++) {
+ InternalQueue<DummyTask> intQueue = new UnboundedQueue<DummyTask>(priorities[i]);
+
+ internalQueueList.add(intQueue);
+ }
+
+ return new MultiPriorityBlockingQueue(internalQueueList, true, new PRRNextQueueAlgorithm());
+ }
+
+ protected MultiPriorityBlockingQueue<DummyTask> createFixedQueue(
+ int noQueues, int[] sizes, int[] priorities) {
+ List<InternalQueue<DummyTask>> internalQueueList = new ArrayList<InternalQueue<DummyTask>>();
+
+ for (int i = 0; i < noQueues; i++) {
+ InternalQueue<DummyTask> intQueue = new FixedSizeQueue<DummyTask>(priorities[i], sizes[i]);
+
+ internalQueueList.add(intQueue);
+ }
+
+ return new MultiPriorityBlockingQueue(
+ internalQueueList, true, new PRRNextQueueAlgorithm());
+ }
+}
Added: synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueStressTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueStressTest.java?rev=923044&view=auto
==============================================================================
--- synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueStressTest.java (added)
+++ synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueStressTest.java Mon Mar 15 03:30:55 2010
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.commons.executors;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Stree Test the MultiPriorityBlockingQueue. This test uses threads to insert and remove
+ * items concurrently.
+ */
+public class MultiPriorityBlockingQueueStressTest extends MultiPriorityBlockingQueueAbstractTest {
+ private static final int CONCURRENT_ITEMS = 10000;
+
+ private final int[] sizes = {CONCURRENT_ITEMS, CONCURRENT_ITEMS, CONCURRENT_ITEMS};
+ private final int[] priorities = {1, 10, 100};
+ private final int QUEUES = 3;
+
+ private MultiPriorityBlockingQueue fixedQueue;
+ private MultiPriorityBlockingQueue unboundedQueue;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ fixedQueue = createFixedQueue(QUEUES, sizes, priorities);
+
+ unboundedQueue = createUnboundedQueue(QUEUES, priorities);
+ }
+
+ public void testOffer() {
+ List<InsertItemThread> list = startConcurrentOffer(fixedQueue, QUEUES, priorities, sizes);
+
+ for (InsertItemThread t : list) {
+ while (!t.isFinished()) {}
+ }
+
+ assertEquals(QUEUES * CONCURRENT_ITEMS, fixedQueue.size());
+
+ fixedQueue.clear();
+
+ assertEquals(0, fixedQueue.size());
+ }
+
+ public void testUnboundedQueueOffer() {
+ List<InsertItemThread> list = startConcurrentOffer(unboundedQueue, 3, priorities, sizes);
+
+ for (InsertItemThread t : list) {
+ while (!t.isFinished()) {}
+ }
+
+ assertEquals(QUEUES * CONCURRENT_ITEMS, unboundedQueue.size());
+
+ unboundedQueue.clear();
+
+ assertEquals(0, unboundedQueue.size());
+ }
+
+ public void testTake() {
+ List<InsertItemThread> offers = startConcurrentOffer(fixedQueue, QUEUES, priorities, sizes);
+ List<RemoveItemsThread> takes = startConcurrentTake(fixedQueue, QUEUES, sizes);
+
+ for (InsertItemThread t : offers) {
+ while (!t.isFinished()) {}
+ }
+
+ for (RemoveItemsThread t : takes) {
+ while (!t.isFinished()) {}
+ }
+
+ assertEquals(0, fixedQueue.size());
+ }
+
+ public void testUnbounderQueueTake() {
+ List<InsertItemThread> offers = startConcurrentOffer(unboundedQueue, QUEUES, priorities, sizes);
+ List<RemoveItemsThread> takes = startConcurrentTake(unboundedQueue, QUEUES, sizes);
+
+ for (InsertItemThread t : offers) {
+ while (!t.isFinished()) {}
+ }
+
+ for (RemoveItemsThread t : takes) {
+ while (!t.isFinished()) {}
+ }
+
+ assertEquals(0, unboundedQueue.size());
+ }
+
+ public void testRemainingCapacity() {
+ List<InsertItemThread> offers = startConcurrentOffer(fixedQueue, QUEUES, priorities, sizes);
+
+ int[] s1 = {CONCURRENT_ITEMS - 1000, CONCURRENT_ITEMS - 1000, CONCURRENT_ITEMS - 1000};
+ List<RemoveItemsThread> takes = startConcurrentTake(fixedQueue, QUEUES, s1);
+
+ for (InsertItemThread t : offers) {
+ while (!t.isFinished()) {}
+ }
+
+ for (RemoveItemsThread t : takes) {
+ while (!t.isFinished()) {}
+ }
+
+ assertEquals(CONCURRENT_ITEMS * QUEUES - 3000, fixedQueue.remainingCapacity());
+
+ int[] s2 = {1000, 1000, 1000};
+ takes = startConcurrentTake(fixedQueue, QUEUES, s2);
+
+ for (RemoveItemsThread t : takes) {
+ while (!t.isFinished()) {}
+ }
+
+ assertEquals(CONCURRENT_ITEMS * QUEUES, fixedQueue.remainingCapacity());
+ }
+
+ private List<InsertItemThread> startConcurrentOffer(
+ MultiPriorityBlockingQueue queue, int threads, int []priorities, int sizes[]) {
+ List<InsertItemThread> threadList = new ArrayList<InsertItemThread>();
+ for (int i = 0; i < threads; i++) {
+ InsertItemThread t = new InsertItemThread(queue, priorities[i], sizes[i]);
+ t.start();
+ threadList.add(t);
+ }
+
+ return threadList;
+ }
+
+ private List<RemoveItemsThread> startConcurrentTake(MultiPriorityBlockingQueue queue,
+ int threads, int []sizes) {
+ List<RemoveItemsThread> threadList = new ArrayList<RemoveItemsThread>();
+ for (int i = 0; i < threads; i++) {
+ RemoveItemsThread t = new RemoveItemsThread(queue, sizes[i]);
+ t.start();
+ threadList.add(t);
+ }
+ return threadList;
+ }
+
+ public static class InsertItemThread extends Thread {
+ private MultiPriorityBlockingQueue queue = null;
+
+ private int max = 0;
+ private int priority = 0;
+
+ private AtomicBoolean finished = new AtomicBoolean(false);
+
+ public InsertItemThread(MultiPriorityBlockingQueue queue, int priority, int max) {
+ this.queue = queue;
+ this.max = max;
+ this.priority = priority;
+
+ }
+
+ public void run() {
+ for (int i = 0; i < max; i++) {
+ boolean offer = queue.offer(new DummyTask(priority));
+ assertTrue("Offer should be successful", offer);
+ }
+ finished.getAndSet(true);
+ }
+
+ public boolean isFinished() {
+ return finished.get();
+ }
+ }
+
+ public static class RemoveItemsThread extends Thread {
+ private MultiPriorityBlockingQueue queue = null;
+
+ private int max = 0;
+
+ private AtomicBoolean finished = new AtomicBoolean(false);
+
+ public RemoveItemsThread(MultiPriorityBlockingQueue queue, int max) {
+ this.queue = queue;
+ this.max = max;
+ }
+
+ public void run() {
+ for (int i = 0; i < max; i++) {
+ try {
+ Object o = queue.take();
+ assertNotNull("Take should return a valid object", o);
+ } catch (InterruptedException e) {
+ assertFalse("This exception cannot occur: " + e.getMessage(), true);
+ }
+ }
+ finished.getAndSet(true);
+ }
+
+ public boolean isFinished() {
+ return finished.get();
+ }
+ }
+}
Added: synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueTest.java?rev=923044&view=auto
==============================================================================
--- synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueTest.java (added)
+++ synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/MultiPriorityBlockingQueueTest.java Mon Mar 15 03:30:55 2010
@@ -0,0 +1,251 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.commons.executors;
+
+/**
+ * Test the MultiPriorityBlockingQueue operations in a single threaded enviorenment.
+ */
+public class MultiPriorityBlockingQueueTest extends MultiPriorityBlockingQueueAbstractTest {
+
+ private static final int ITEMS = 100;
+
+ private MultiPriorityBlockingQueue fixedQueue;
+
+ private MultiPriorityBlockingQueue unboundedQueue;
+
+ private final int[] priorities = {10, 1};
+ private final int[] sizes = {ITEMS, ITEMS};
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ fixedQueue = createFixedQueue(2, sizes, priorities);
+ unboundedQueue = createUnboundedQueue(2, priorities);
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ fixedQueue.clear();
+ unboundedQueue.clear();
+ }
+
+ public void testOffer() {
+ performOffer(fixedQueue, sizes, priorities);
+
+ assertEquals(ITEMS * 2, fixedQueue.size());
+
+ fixedQueue.clear();
+
+ assertEquals(0, fixedQueue.size());
+ }
+
+ public void testUnboundedQueueOffer() {
+ performOffer(unboundedQueue, sizes, priorities);
+
+ assertEquals(ITEMS * 2, unboundedQueue.size());
+
+ unboundedQueue.clear();
+
+ assertEquals(0, unboundedQueue.size());
+ }
+
+ public void testTake() {
+ performOffer(fixedQueue, sizes, priorities);
+ performTake(fixedQueue, ITEMS * 2);
+
+ assertEquals(0, fixedQueue.size());
+ }
+
+ public void testUnboundedQueueTake() {
+ performOffer(unboundedQueue, sizes, priorities);
+ performTake(unboundedQueue, ITEMS * 2);
+
+ assertEquals(0, unboundedQueue.size());
+ }
+
+ public void testAdd() {
+ performOffer(fixedQueue, sizes, priorities);
+
+ boolean exceptionOccured = false;
+
+ try {
+ fixedQueue.add(new DummyTask(10));
+ } catch (IllegalStateException e) {
+ exceptionOccured = true;
+ }
+
+ assertTrue("Exception should occur", exceptionOccured);
+
+ fixedQueue.clear();
+ assertEquals(0, fixedQueue.size());
+
+ for (int i = 0; i < ITEMS; i++) {
+ fixedQueue.add(new DummyTask(1));
+ fixedQueue.add(new DummyTask(10));
+ }
+
+ assertEquals(ITEMS * 2, fixedQueue.size());
+
+ fixedQueue.clear();
+ assertEquals(0, fixedQueue.size());
+ }
+
+ public void testPut() throws InterruptedException {
+ try {
+ performPut(fixedQueue, sizes, priorities);
+ } catch (InterruptedException e) {
+ assertTrue("This exception shouldn't occur", false);
+ }
+
+ assertEquals(ITEMS * 2, fixedQueue.size());
+
+ Thread t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ fixedQueue.put(new DummyTask(10));
+
+ assertTrue("Above put should block and this line shouldn't execute", false);
+ } catch (InterruptedException e) {
+ assertTrue("Interrupted", true);
+ }
+ }
+ });
+ t.start();
+
+ waitForThreadState(t, Thread.State.WAITING);
+
+ assertTrue(t.getState() == Thread.State.WAITING);
+ t.interrupt();
+
+ t = new Thread(new Runnable() {
+ public void run() {
+ try {
+ fixedQueue.put(new DummyTask(10));
+
+ assertTrue("This should execute since we are taking one " +
+ "element out from the queue", true);
+ } catch (InterruptedException e) {
+ assertTrue("Interruption shouldn't occur", false);
+ }
+ }
+ });
+ t.start();
+
+ waitForThreadState(t, Thread.State.WAITING);
+ // after thread is blocked in put we are going to take an element
+ fixedQueue.take();
+
+ waitForThreadState(t, Thread.State.TERMINATED);
+
+ assertEquals(ITEMS * 2, fixedQueue.size());
+
+ fixedQueue.clear();
+ assertEquals(0, fixedQueue.size());
+ }
+
+ public void testContains() {
+ int [] puts = {ITEMS - 1, ITEMS -1};
+
+ performOffer(fixedQueue, puts, priorities);
+
+ DummyTask task = new DummyTask(10);
+ fixedQueue.offer(task);
+
+ assertTrue(fixedQueue.contains(task));
+ fixedQueue.clear();
+
+
+ performOffer(unboundedQueue, puts, priorities);
+
+ task = new DummyTask(10);
+ unboundedQueue.offer(task);
+
+ assertTrue(unboundedQueue.contains(task));
+
+ fixedQueue.clear();
+ assertEquals(0, fixedQueue.size());
+ }
+
+ public void testRemove() {
+ int [] puts = {5, 5};
+
+ performOffer(fixedQueue, puts, priorities);
+
+ DummyTask task = new DummyTask(10);
+ fixedQueue.offer(task);
+
+ assertTrue(fixedQueue.remove(task));
+ fixedQueue.clear();
+
+
+ performOffer(unboundedQueue, puts, priorities);
+
+ task = new DummyTask(10);
+ unboundedQueue.offer(task);
+
+ assertTrue(unboundedQueue.remove(task));
+
+ unboundedQueue.clear();
+ assertEquals(0, fixedQueue.size());
+ }
+
+ private void waitForThreadState(Thread t, Thread.State state) {
+ int count = 0;
+ while (t.getState() != state && count < 10){
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException ignored) { }
+
+ count++;
+ }
+ }
+
+ private void performPut(MultiPriorityBlockingQueue queue,
+ int []items, int []priorites) throws InterruptedException {
+ for (int priority = 0; priority < priorites.length; priority++) {
+ for (int i = 0; i < items[priority]; i++) {
+ queue.put(new DummyTask(priorites[priority]));
+ }
+ }
+ }
+
+ private void performOffer(MultiPriorityBlockingQueue queue,
+ int []items, int []priorites) {
+ for (int priority = 0; priority < priorites.length; priority++) {
+ for (int i = 0; i < items[priority]; i++) {
+ queue.offer(new DummyTask(priorites[priority]));
+ }
+ }
+ }
+
+ private void performTake(MultiPriorityBlockingQueue queue, int items) {
+ for (int i = 0; i < items; i++) {
+ try {
+ Object o = queue.take();
+ assertNotNull("Object cannot be null", o);
+ } catch (InterruptedException cannotOccur) {
+ assertFalse("Cannot execute this", true);
+ }
+ }
+ }
+}
Added: synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/queues/FixedQueueTest.java
URL: http://svn.apache.org/viewvc/synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/queues/FixedQueueTest.java?rev=923044&view=auto
==============================================================================
--- synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/queues/FixedQueueTest.java (added)
+++ synapse/trunk/java/modules/commons/src/test/java/org/apache/synapse/commons/executors/queues/FixedQueueTest.java Mon Mar 15 03:30:55 2010
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.synapse.commons.executors.queues;
+
+import junit.framework.TestCase;
+import org.apache.synapse.commons.executors.DummyTask;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.NoSuchElementException;
+
+
+public class FixedQueueTest extends TestCase {
+
+ private FixedSizeQueue<DummyTask> queue;
+
+ private final int SIZE = 10;
+ private final int PRIORITY = 10;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ queue = new FixedSizeQueue<DummyTask>(PRIORITY, SIZE);
+ }
+
+ public void testOffer() {
+ queue.offer(new DummyTask(PRIORITY));
+
+ assertEquals(queue.size(), 1);
+
+ for (int i = 0; i < SIZE; i++) {
+ queue.offer(new DummyTask(SIZE));
+ }
+
+ assertEquals(queue.size(), SIZE);
+
+ queue.clear();
+
+ assertEquals(queue.size(), 0);
+ }
+
+ public void testPeek() {
+ DummyTask task = new DummyTask(PRIORITY);
+ queue.offer(task);
+
+ assertEquals(queue.peek(), task);
+
+ for (int i = 0; i < SIZE/2; i++) {
+ queue.offer(new DummyTask(SIZE));
+ }
+
+ queue.offer(task);
+
+ assertEquals(queue.peek(), task);
+ queue.clear();
+
+ assertTrue(null == queue.peek());
+ }
+
+ public void testAdd() {
+ DummyTask task = new DummyTask(PRIORITY);
+ queue.add(task);
+
+ assertEquals(queue.peek(), task);
+
+ for (int i = 0; i < SIZE/2; i++) {
+ queue.add(new DummyTask(SIZE));
+ }
+
+ queue.add(task);
+
+ assertEquals(queue.peek(), task);
+
+ boolean exceptionOccurred = false;
+ try {
+ for (int i = 0; i < SIZE; i++) {
+ queue.add(new DummyTask(SIZE));
+ }
+ } catch (IllegalStateException e) {
+ exceptionOccurred = true;
+ }
+
+ assertTrue(exceptionOccurred);
+ queue.clear();
+ }
+
+ public void testRemove() {
+ DummyTask task = new DummyTask(PRIORITY);
+ queue.offer(task);
+
+ assertEquals(queue.peek(), task);
+ assertEquals(queue.remove(), task);
+ assertEquals(queue.size(), 0);
+
+ for (int i = 0; i < SIZE; i++) {
+ queue.offer(new DummyTask(SIZE));
+ }
+
+ queue.remove();
+
+ assertEquals(queue.size(), SIZE - 1);
+
+ queue.clear();
+ }
+
+ public void testRemoveObject() {
+ DummyTask task = new DummyTask(PRIORITY);
+ queue.offer(task);
+
+ assertEquals(queue.peek(), task);
+ assertTrue(queue.remove(task));
+ assertEquals(queue.size(), 0);
+
+ List<DummyTask> tasks = new ArrayList<DummyTask>();
+ for (int i = 0; i < SIZE; i++) {
+ DummyTask t = new DummyTask(10);
+ tasks.add(t);
+ queue.offer(t);
+ }
+
+ assertEquals(queue.size(), SIZE);
+
+ queue.remove(tasks.get(6));
+ queue.remove(tasks.get(7));
+ queue.remove(tasks.get(4));
+ queue.remove(tasks.get(5));
+
+ assertEquals(queue.size(), SIZE - 4);
+
+ queue.offer(tasks.get(4));
+ queue.offer(tasks.get(5));
+ queue.offer(tasks.get(6));
+ queue.offer(tasks.get(7));
+
+ assertEquals(queue.size(), SIZE);
+
+ queue.clear();
+ }
+
+ public void testElement() {
+ DummyTask task = new DummyTask(PRIORITY);
+ queue.offer(task);
+
+ assertEquals(queue.element(), task);
+
+ for (int i = 0; i < SIZE/2; i++) {
+ queue.offer(new DummyTask(SIZE));
+ }
+
+ queue.offer(task);
+
+ assertEquals(queue.element(), task);
+ queue.clear();
+
+ boolean exceptionOccurred = false;
+ try {
+ queue.element();
+ } catch (NoSuchElementException e) {
+ exceptionOccurred = true;
+ }
+
+ assertTrue(exceptionOccurred);
+ }
+}