common.core: make BufferedBlockingQueue validate inputs
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 8 Nov 2016 01:55:36 +0000 (20:55 -0500)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Mon, 14 Nov 2016 15:07:30 +0000 (10:07 -0500)
Also tests that inputs are validated

Change-Id: I6cc39a0851e0ccf8ada0dd473586c613387ffc68
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/47398
Reviewed-by: Hudson CI
Reviewed-by: Genevieve Bastien <gbastien+lttng@versatic.net>
Tested-by: Genevieve Bastien <gbastien+lttng@versatic.net>
common/org.eclipse.tracecompass.common.core.tests/src/org/eclipse/tracecompass/common/core/tests/collect/BufferedBlockingQueueTest.java
common/org.eclipse.tracecompass.common.core/src/org/eclipse/tracecompass/common/core/collect/BufferedBlockingQueue.java

index 01c67fa25bb22d4778f098351f6c8005c75fd31a..593bd0e4168b1c52bf0f49a98e6663930c260534 100644 (file)
@@ -15,6 +15,7 @@ package org.eclipse.tracecompass.common.core.tests.collect;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.fail;
 
 import java.util.Collection;
@@ -68,6 +69,60 @@ public class BufferedBlockingQueueTest {
         charQueue = new BufferedBlockingQueue<>(15, 15);
     }
 
+    /**
+     * Test with chunkSize = 1 and buffer queueSize = 1
+     */
+    public void testValidConstructor1() {
+        assertNotNull(new BufferedBlockingQueue<>(1, 1));
+    }
+
+    /**
+     * Test with chunkSize = 0 and buffer queueSize = 0
+     * <p>
+     * Should fail with an {@link IllegalArgumentException}
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testInvalidConstructor1() {
+        assertNotNull(new BufferedBlockingQueue<>(0, 0));
+    }
+
+    /**
+     * Test with chunkSize = 0 and buffer queueSize = 1
+     * <p>
+     * Should fail with an {@link IllegalArgumentException}
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testInvalidConstructor2() {
+        assertNotNull(new BufferedBlockingQueue<>(1, 0));
+    }
+
+    /**
+     * Test with chunkSize = 1 and buffer queueSize = 0
+     */
+    public void testInvalidConstructor3() {
+        assertNotNull(new BufferedBlockingQueue<>(0, 1));
+    }
+
+    /**
+     * Test with chunkSize = 1 and buffer queueSize =-1
+     * <p>
+     * Should fail with an {@link IllegalArgumentException}
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testInvalidConstructor4() {
+        assertNotNull(new BufferedBlockingQueue<>(-1, 1));
+    }
+
+    /**
+     * Test with chunkSize = -1 and buffer queueSize = 1
+     * <p>
+     * Should fail with an {@link IllegalArgumentException}
+     */
+    @Test(expected = IllegalArgumentException.class)
+    public void testInvalidConstructor5() {
+        assertNotNull(new BufferedBlockingQueue<>(1, -1));
+    }
+
     /**
      * Test inserting one element and removing it.
      */
index 21533fbaabb4570018e891897f101c3e45350b6f..ec00f9eef272348375e6c8e2c007cf8de46a9fe7 100644 (file)
@@ -73,11 +73,19 @@ public class BufferedBlockingQueue<T> implements Iterable<T> {
      *
      * @param queueSize
      *            The size of the actual blocking queue. This is the number of
-     *            *chunks* that will go in the queue.
+     *            additional *chunks* that will go in the queue. The value must
+     *            be larger or equal to 0.
      * @param chunkSize
-     *            The size of an individual chunk.
+     *            The size of an individual chunk. The value must be larger than
+     *            0.
      */
     public BufferedBlockingQueue(int queueSize, int chunkSize) {
+        if (queueSize < 0) {
+            throw new IllegalArgumentException("queueSize must be >= 0"); //$NON-NLS-1$
+        }
+        if (chunkSize <= 0) {
+            throw new IllegalArgumentException("chunkSize must be > 0"); //$NON-NLS-1$
+        }
         /* Add one to the queue size for the output buffer */
         fInnerQueue = new LinkedBlockingDeque<>(queueSize + 1);
         fChunkSize = chunkSize;
This page took 0.02914 seconds and 5 git commands to generate.