/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2007, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.cache.lock;

import junit.framework.TestCase;
import org.jboss.cache.Fqn;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import EDU.oswego.cs.dl.util.concurrent.ReentrantWriterPreferenceReadWriteLock;

public class StripedLockTest extends TestCase
{
   private StripedLock stripedLock;

   protected void setUp()
   {
      stripedLock = new StripedLock();
   }

   public void testHashingDistribution()
   {
      // ensure even bucket distribution of lock stripes
      List fqns = createRandomFqns(1000);

      Map distribution = new HashMap();

      Iterator fqnsIterator = fqns.iterator();
      while (fqnsIterator.hasNext())
      {
         ReentrantWriterPreferenceReadWriteLock lock = stripedLock.getLock((Fqn) fqnsIterator.next());
         if (distribution.containsKey(lock))
         {
            int count = ((Integer) distribution.get(lock)).intValue() + 1;
            distribution.put(lock, new Integer(count));
         }
         else
         {
            distribution.put(lock, new Integer(1));
         }
      }

      System.out.println(distribution);

      // cannot be larger than the number of locks
      System.out.println("dist size: " + distribution.size());
      System.out.println("num shared locks: " + stripedLock.sharedLocks.length);
      assertTrue(distribution.size() <= stripedLock.sharedLocks.length);
      // assume at least a 2/3rd spread
      assertTrue(distribution.size() * 1.5 >= stripedLock.sharedLocks.length);
   }

   public void testAcquireReleaseLocks()
   {
      Fqn f = Fqn.fromString("/a/b/c");

      /* acquire RL*/
      stripedLock.acquireLock(f, false);
      /* release RL*/
      stripedLock.releaseLock(f);
      /* acquire WL */
      stripedLock.acquireLock(f, true);
      /* reacquire RL without giving up lock */
      stripedLock.acquireLock(f, false);
      /* release RL */
      stripedLock.releaseLock(f);
      /* release WL */
      stripedLock.releaseLock(f);
   }

   private List createRandomFqns(int number)
   {
      List f = new ArrayList(number);
      Random r = new Random();

      while (f.size() < number)
      {
         Fqn fqn = Fqn.fromString("/" + ((char) (65 + r.nextInt(26))) + "/" + ((char) (65 + r.nextInt(26))) + "/" + ((char) (65 + r.nextInt(26))));
         f.add(fqn);
      }

      System.out.println("Fqns: " + f);

      return f;
   }
}
