### Question:
LimitedFertilityBlockingQueue extends LinkedBlockingQueue<E> { @Override public E take() throws InterruptedException { return take(myFactory); } LimitedFertilityBlockingQueue(int maxChildren, Factory<? extends E> factory);  LimitedFertilityBlockingQueue(int capacity, int maxChildren, Factory<? extends E> factory); int getCount(); int getMaxChildren(); @Override E poll(); E poll(Factory<? extends E> factory); E poll(Factory<? extends E> factory, @Nullable Consumer<? super E> adapter); E poll(Factory<? extends E> factory, long timeout, TimeUnit unit); E poll(Factory<? extends E> factory, @Nullable Consumer<? super E> adapter, long timeout, TimeUnit unit); @Override E poll(long timeout, TimeUnit unit); @Override E take(); E take(Factory<? extends E> factory); E take(Factory<? extends E> factory, @Nullable Consumer<? super E> adapter);  }### Answer:
@Test public void testTake() throws InterruptedException, ExecutionException { LimitedFertilityBlockingQueue.Factory<Integer> factory = new FactoryImplementation(); final LimitedFertilityBlockingQueue<Integer> queue = new LimitedFertilityBlockingQueue<>(10, factory); for (int index = 0; index < 10; ++index) { Assert.assertEquals(Integer.valueOf(index), queue.take()); } Callable<Integer> task = new Callable<Integer>() { @Override public Integer call() throws InterruptedException { return queue.take(); } }; Future<Integer> future = Executors.newSingleThreadExecutor().submit(task); try { future.get(100L, TimeUnit.MILLISECONDS); Assert.fail(SHOULD_HAVE_TIMED_OUT); } catch (TimeoutException e) { } Assert.assertTrue(queue.add(Integer.valueOf(11))); Assert.assertEquals(Integer.valueOf(11), future.get()); }
@Test public void testTakeFactory() throws InterruptedException, ExecutionException { final LimitedFertilityBlockingQueue.Factory<Integer> factory = new FactoryImplementation(); final LimitedFertilityBlockingQueue<Integer> queue = new LimitedFertilityBlockingQueue<>(10, null); for (int index = 0; index < 10; ++index) { Assert.assertEquals(Integer.valueOf(index), queue.take(factory)); } Callable<Integer> task = new Callable<Integer>() { @Override public Integer call() throws InterruptedException { return queue.take(factory); } }; Future<Integer> future = Executors.newSingleThreadExecutor().submit(task); try { future.get(100L, TimeUnit.MILLISECONDS); Assert.fail(SHOULD_HAVE_TIMED_OUT); } catch (TimeoutException e) { } Assert.assertTrue(queue.add(Integer.valueOf(11))); Assert.assertEquals(Integer.valueOf(11), future.get()); }
@Test public void testTakeFactoryConsumer() throws InterruptedException, ExecutionException { final LimitedFertilityBlockingQueue.Factory<Integer> factory = new FactoryImplementation(); final ConsumerImplementation consumer = new ConsumerImplementation(); final LimitedFertilityBlockingQueue<Integer> queue = new LimitedFertilityBlockingQueue<>(10, null); for (int index = 0; index < 10; ++index) { Assert.assertEquals(Integer.valueOf(index), queue.take(factory, consumer)); } Assert.assertTrue(consumer.getValues().isEmpty()); Callable<Integer> task = new Callable<Integer>() { @Override public Integer call() throws InterruptedException { return queue.take(factory, consumer); } }; Future<Integer> future = Executors.newSingleThreadExecutor().submit(task); try { future.get(100L, TimeUnit.MILLISECONDS); Assert.fail(SHOULD_HAVE_TIMED_OUT); } catch (TimeoutException e) { } Assert.assertTrue(queue.add(Integer.valueOf(11))); Assert.assertEquals(Integer.valueOf(11), future.get()); Assert.assertEquals(1, consumer.getValues().size()); Assert.assertEquals(Integer.valueOf(11), consumer.getValues().get(0)); }