/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt 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 java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.transaction.Synchronization;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;

import org.jboss.cache.PropertyConfigurator;
import org.jboss.cache.TreeCache;
import org.jboss.cache.TreeCacheMBean;
import org.jboss.cache.misc.TestingUtil;
import org.jgroups.Address;
import org.jgroups.MembershipListener;
import org.jgroups.View;

import junit.framework.TestCase;

/**
 * Tests the breaking of locks held by dead members.
 * 
 * @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
 * @version $Revision$
 */
public class BreakDeadMemberLocksTest extends TestCase
{

   private Map caches;
   
   protected void setUp() throws Exception
   {
      super.setUp();
      
      caches = new HashMap();
   }
   
   public void testBreakDeadMemberLocks() throws Exception
   {
      TreeCacheMBean cacheA = createCache("A");
      
      cacheA.put("/1/A", "1", "A");
      cacheA.put("/1/A", "2", "A");
      cacheA.put("/2/A", "1", "A");
      cacheA.put("/2/A", "2", "A");
      cacheA.put("/1/A/I", "1", "A");
      cacheA.put("/1/A/I", "2", "A");
      
      TreeCacheMBean cacheB = createCache("B");
      
      // Pause to give caches time to see each other
      TestingUtil.blockUntilViewsReceived(new TreeCacheMBean[] { cacheA, cacheB }, 60000);
      
      final TransactionManager tm = cacheB.getTransactionManager();
      tm.begin();
      final Transaction tx = tm.getTransaction();
      
      cacheB.put("/1/A", "1", "B");
      cacheB.put("/1/A", "2", "B");
      cacheB.put("/2/A", "1", "B");
      cacheB.put("/2/A", "2", "B");
      cacheB.put("/1/A/I", "1", "B");
      cacheB.put("/1/A/I", "2", "B");
      cacheB.put("/EXISTS", "KEY", "B");
      
      Object monitor = new Object();
      HangSync sync = new HangSync(monitor);
      tx.registerSynchronization(sync);
      
      Thread t = new Thread() {
        public void run() {
           try
           {
              tm.resume(tx);
              tx.commit();
           }
           catch (Exception e) {}
        }
      };
      
      synchronized (monitor)
      {
         t.start();
      
         while (!sync.hung)
            monitor.wait(500);
      }
      
      tm.suspend();
      
      // Confirm that B's tx replicated
      assertTrue(cacheA.exists("/EXISTS"));
      
      cacheB.stopService();
      cacheB.destroyService();
      
      while (cacheA.getMembers().size() > 1)
      {
         try
         {
            Thread.sleep(100);
         }
         catch (InterruptedException e) {}
      }
      
      assertEquals("A", cacheA.get("/1/A", "1"));
      assertEquals("A", cacheA.get("/1/A", "2"));
      assertEquals("A", cacheA.get("/2/A", "1"));
      assertEquals("A", cacheA.get("/2/A", "2"));
      assertEquals("A", cacheA.get("/1/A/I", "1"));
      assertEquals("A", cacheA.get("/1/A/I", "2"));
      
      if (t.isAlive())
      {
         t.interrupt();
      }
   }
   
   protected TreeCacheMBean createCache(String cacheID) throws Exception
   {
      if (caches.get(cacheID) != null)
      throw new IllegalStateException(cacheID + " already created");
      
      TreeCacheMBean tree=new TreeCache();
      PropertyConfigurator config=new PropertyConfigurator();
      config.configure(tree, "META-INF/replSync-service.xml");
      
      tree.createService();
      tree.startService();
      
      caches.put(cacheID, tree);
      
      return tree;
   }
   
   protected void tearDown() throws Exception
   {
      super.tearDown();
      
      Set keys = caches.keySet();
      String[] cacheIDs = new String[keys.size()];
      cacheIDs = (String[]) keys.toArray(cacheIDs);
      for (int i = 0; i < cacheIDs.length; i++)
      {
         stopCache(cacheIDs[i]);
      }
   }
   
   protected void stopCache(String id)
   {
      TreeCache cache = (TreeCache) caches.get(id);
      if (cache != null)
      {
         try {
            cache.stopService();
            cache.destroyService();
            caches.remove(id);
         }
         catch (Exception e) {
            System.out.println("Exception stopping cache " + e.getMessage());
            e.printStackTrace(System.out);
         }
      }
   }

   class HangSync implements Synchronization
   {
      private boolean hung = false;
      private Object monitor;
      
      HangSync(Object monitor)
      {
         this.monitor = monitor;
      }

      public void afterCompletion(int arg0) {}

      public void beforeCompletion()
      {
         hung = true;
         synchronized (monitor)
         {
            monitor.notifyAll();
         }
         try
         {
            Thread.sleep(30000);
         }
         catch(InterruptedException e) {}
      }
      
      
   }
}
