CircularBlockingDequeTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00017 package org.ros.concurrent;
00018 
00019 import static org.junit.Assert.assertEquals;
00020 import static org.junit.Assert.assertFalse;
00021 import static org.junit.Assert.assertTrue;
00022 import static org.junit.Assert.fail;
00023 
00024 import org.junit.Before;
00025 import org.junit.Test;
00026 
00027 import java.util.Iterator;
00028 import java.util.NoSuchElementException;
00029 import java.util.concurrent.CountDownLatch;
00030 import java.util.concurrent.ExecutorService;
00031 import java.util.concurrent.Executors;
00032 import java.util.concurrent.TimeUnit;
00033 
00037 public class CircularBlockingDequeTest {
00038 
00039   private ExecutorService executorService;
00040 
00041   @Before
00042   public void before() {
00043     executorService = Executors.newCachedThreadPool();
00044   }
00045 
00046   @Test
00047   public void testAddAndTake() throws InterruptedException {
00048     CircularBlockingDeque<String> deque = new CircularBlockingDeque<String>(10);
00049     String expectedString1 = "Hello, world!";
00050     String expectedString2 = "Goodbye, world!";
00051     deque.addLast(expectedString1);
00052     deque.addLast(expectedString2);
00053     assertEquals(expectedString1, deque.takeFirst());
00054     assertEquals(expectedString2, deque.takeFirst());
00055   }
00056 
00057   @Test
00058   public void testAddFirstAndTakeLast() throws InterruptedException {
00059     CircularBlockingDeque<String> deque = new CircularBlockingDeque<String>(10);
00060     String expectedString1 = "Hello, world!";
00061     String expectedString2 = "Goodbye, world!";
00062     deque.addLast(expectedString1);
00063     deque.addLast(expectedString2);
00064     assertEquals(expectedString1, deque.peekFirst());
00065     assertEquals(expectedString2, deque.takeLast());
00066     deque.addFirst(expectedString2);
00067     assertEquals(expectedString1, deque.peekLast());
00068     assertEquals(expectedString2, deque.takeFirst());
00069   }
00070 
00071   @Test
00072   public void testOverwrite() throws InterruptedException {
00073     CircularBlockingDeque<String> deque = new CircularBlockingDeque<String>(2);
00074     String expectedString = "Hello, world!";
00075     deque.addLast("overwritten");
00076     deque.addLast(expectedString);
00077     deque.addLast("foo");
00078     assertEquals(expectedString, deque.takeFirst());
00079   }
00080 
00081   @Test
00082   public void testIterator() throws InterruptedException {
00083     // We keep the queue short and throw in an unused element so that the deque
00084     // wraps around the backing array.
00085     CircularBlockingDeque<String> deque = new CircularBlockingDeque<String>(2);
00086     deque.addLast("unused");
00087     String expectedString1 = "Hello, world!";
00088     String expectedString2 = "Goodbye, world!";
00089     deque.addLast(expectedString1);
00090     deque.addLast(expectedString2);
00091     Iterator<String> iterator = deque.iterator();
00092     assertEquals(expectedString1, iterator.next());
00093     assertEquals(expectedString2, iterator.next());
00094     assertFalse(iterator.hasNext());
00095     try {
00096       iterator.next();
00097       fail();
00098     } catch (NoSuchElementException e) {
00099       // next() should throw an exception if there is no next element.
00100     }
00101     deque.takeFirst();
00102     iterator = deque.iterator();
00103     assertEquals(expectedString2, iterator.next());
00104     assertFalse(iterator.hasNext());
00105   }
00106 
00107   @Test
00108   public void testBlockingTake() throws InterruptedException {
00109     final CircularBlockingDeque<String> deque = new CircularBlockingDeque<String>(1);
00110     final String expectedString = "Hello, world!";
00111     final CountDownLatch latch = new CountDownLatch(1);
00112     executorService.execute(new Runnable() {
00113       @Override
00114       public void run() {
00115         try {
00116           assertEquals(expectedString, deque.takeFirst());
00117         } catch (InterruptedException e) {
00118           fail();
00119         }
00120         latch.countDown();
00121       }
00122     });
00123     // Sleep to ensure we're waiting on take().
00124     Thread.sleep(5);
00125     deque.addLast(expectedString);
00126     assertTrue(latch.await(1, TimeUnit.SECONDS));
00127   }
00128 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49