/*
 * Copyright (C) 2013 Brett Wooldridge
 *
 * Licensed 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 com.zaxxer.hikari;

import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.zaxxer.hikari.pool.HikariPool;
import com.zaxxer.hikari.pool.PoolBagEntry;
import com.zaxxer.hikari.util.ConcurrentBag;
import com.zaxxer.hikari.util.Java8ConcurrentBag;

/**
 *
 * @author Brett Wooldridge
 */
public class TestConcurrentBag
{
   private static HikariDataSource ds;

   @BeforeClass
   public static void setup()
   {
      HikariConfig config = new HikariConfig();
      config.setMinimumIdle(1);
      config.setMaximumPoolSize(2);
      config.setInitializationFailFast(true);
      config.setConnectionTestQuery("VALUES 1");
      config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource");

      ds = new HikariDataSource(config);      
   }

   @AfterClass
   public static void teardown()
   {
      ds.close();
   }

   @Test
   public void testConcurrentBag() throws InterruptedException
   {
      ConcurrentBag<PoolBagEntry> bag = new Java8ConcurrentBag(null);
      Assert.assertEquals(0, bag.values(8).size());

      HikariPool pool = TestElf.getPool(ds);
      PoolBagEntry reserved = new PoolBagEntry(null, TestElf.getPool(ds));
      bag.add(reserved);
      bag.reserve(reserved);      // reserved

      PoolBagEntry inuse = new PoolBagEntry(null, pool);
      bag.add(inuse);
      bag.borrow(2L, TimeUnit.SECONDS); // in use
      
      PoolBagEntry notinuse = new PoolBagEntry(null, pool);
      bag.add(notinuse); // not in use
      
      bag.dumpState();

      try {
         bag.requite(reserved);
         Assert.fail();
      }
      catch (IllegalStateException e) {
         // pass
      }

      try {
         bag.remove(notinuse);
         Assert.fail();
      }
      catch (IllegalStateException e) {
         // pass
      }

      try {
         bag.unreserve(notinuse);
         Assert.fail();
      }
      catch (IllegalStateException e) {
         // pass
      }

      try {
         bag.remove(inuse);
         bag.remove(inuse);
         Assert.fail();
      }
      catch (IllegalStateException e) {
         // pass
      }

      bag.close();
      try {
         PoolBagEntry bagEntry = new PoolBagEntry(null, pool);
         bag.add(bagEntry);
         Assert.assertNotEquals(bagEntry, bag.borrow(100, TimeUnit.MILLISECONDS));
      }
      catch (IllegalStateException e) {
         // pass
      }

      Assert.assertNotNull(notinuse.toString());
   }
}

@@ -633,37 +633,162 @@
 ort 
-java.util.concurrent.TimeUnit
+static com.zaxxer.hikari.util.UtilityElf.IS_JAVA7;%0A%0Aimport java.util.concurrent.TimeUnit;%0Aimport java.util.concurrent.locks.AbstractQueuedLongSynchronizer
 ;%0A%0Ai
@@ -1025,16 +1025,67 @@
 entBag;%0A
+import com.zaxxer.hikari.util.IConcurrentBagEntry;%0A
 import c
@@ -3294,11 +3294,1432 @@
 ));%0A   %7D
+%0A%0A   @Test%0A   public void testConcurrentBag2() throws InterruptedException%0A   %7B%0A      ConcurrentBag%3CPoolBagEntry%3E bag = new FauxJava6ConcurrentBag();%0A      Assert.assertEquals(0, bag.values(IConcurrentBagEntry.STATE_IN_USE).size());%0A      Assert.assertEquals(0, bag.getCount(IConcurrentBagEntry.STATE_IN_USE));%0A   %7D%0A%0A   private static class FauxJava6ConcurrentBag extends ConcurrentBag%3CPoolBagEntry%3E%0A   %7B%0A      /**%0A       * @param listener%0A       */%0A      public FauxJava6ConcurrentBag() %7B%0A         super(null);%0A      %7D%0A%0A      @Override%0A      protected AbstractQueuedLongSynchronizer createQueuedSynchronizer()%0A      %7B%0A         return new Synchronizer();%0A      %7D%0A   %7D%0A%0A   /**%0A    * Our private synchronizer that handles notify/wait type semantics.%0A    */%0A   private static final class Synchronizer extends AbstractQueuedLongSynchronizer%0A   %7B%0A      private static final long serialVersionUID = 104753538004341218L;%0A%0A      @Override%0A      protected long tryAcquireShared(long seq)%0A      %7B%0A         return getState() %3E seq && !java67hasQueuedPredecessors() ? 1L : -1L;%0A      %7D%0A%0A      /** %7B@inheritDoc%7D */%0A      @Override%0A      protected boolean tryReleaseShared(long updateSeq)%0A      %7B%0A         setState(updateSeq);%0A%0A         return true;%0A      %7D%0A%0A      private boolean java67hasQueuedPredecessors()%0A      %7B%0A         if (IS_JAVA7) %7B%0A            return hasQueuedPredecessors();%0A         %7D%0A%0A         return false;%0A      %7D%0A   %7D
 %0A%7D%0A

Improve ConcurrentBag coverage.