Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 222   Methods: 9
NCLOC: 156   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
BreakDeadMemberLocksTest.java 75% 96% 100% 93.8%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    * Copyright 2005, JBoss Inc., and individual contributors as indicated
 4    * by the @authors tag. See the copyright.txt in the distribution for a
 5    * full listing of individual contributors.
 6    *
 7    * This is free software; you can redistribute it and/or modify it
 8    * under the terms of the GNU Lesser General Public License as
 9    * published by the Free Software Foundation; either version 2.1 of
 10    * the License, or (at your option) any later version.
 11    *
 12    * This software is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15    * Lesser General Public License for more details.
 16    *
 17    * You should have received a copy of the GNU Lesser General Public
 18    * License along with this software; if not, write to the Free
 19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21    */
 22   
 23    package org.jboss.cache.lock;
 24   
 25    import junit.framework.TestCase;
 26    import org.jboss.cache.CacheImpl;
 27    import org.jboss.cache.DefaultCacheFactory;
 28    import org.jboss.cache.config.Configuration.CacheMode;
 29    import org.jboss.cache.factories.UnitTestCacheFactory;
 30    import org.jboss.cache.misc.TestingUtil;
 31   
 32    import javax.transaction.Synchronization;
 33    import javax.transaction.Transaction;
 34    import javax.transaction.TransactionManager;
 35    import java.util.HashMap;
 36    import java.util.Map;
 37    import java.util.Set;
 38   
 39    /**
 40    * Tests the breaking of locks held by dead members.
 41    *
 42    * @author <a href="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
 43    * @version $Revision$
 44    */
 45    public class BreakDeadMemberLocksTest extends TestCase
 46    {
 47   
 48    private Map caches;
 49   
 50  1 protected void setUp() throws Exception
 51    {
 52  1 super.setUp();
 53   
 54  1 caches = new HashMap();
 55    }
 56   
 57  1 public void testBreakDeadMemberLocks() throws Exception
 58    {
 59  1 CacheImpl cacheA = createCache("A");
 60   
 61  1 cacheA.put("/1/A", "1", "A");
 62  1 cacheA.put("/1/A", "2", "A");
 63  1 cacheA.put("/2/A", "1", "A");
 64  1 cacheA.put("/2/A", "2", "A");
 65  1 cacheA.put("/1/A/I", "1", "A");
 66  1 cacheA.put("/1/A/I", "2", "A");
 67   
 68  1 CacheImpl cacheB = createCache("B");
 69   
 70    // Pause to give caches time to see each other
 71  1 TestingUtil.blockUntilViewsReceived(new CacheImpl[]{cacheA, cacheB}, 60000);
 72   
 73  1 final TransactionManager tm = cacheB.getTransactionManager();
 74  1 tm.begin();
 75  1 final Transaction tx = tm.getTransaction();
 76   
 77  1 cacheB.put("/1/A", "1", "B");
 78  1 cacheB.put("/1/A", "2", "B");
 79  1 cacheB.put("/2/A", "1", "B");
 80  1 cacheB.put("/2/A", "2", "B");
 81  1 cacheB.put("/1/A/I", "1", "B");
 82  1 cacheB.put("/1/A/I", "2", "B");
 83  1 cacheB.put("/EXISTS", "KEY", "B");
 84   
 85  1 Object monitor = new Object();
 86  1 HangSync sync = new HangSync(monitor);
 87  1 tx.registerSynchronization(sync);
 88   
 89  1 Thread t = new Thread()
 90    {
 91  1 public void run()
 92    {
 93  1 try
 94    {
 95  1 tm.resume(tx);
 96  1 tx.commit();
 97    }
 98    catch (Exception e) {}
 99    }
 100    };
 101   
 102  1 synchronized (monitor)
 103    {
 104  1 t.start();
 105   
 106  1 while (!sync.hung)
 107    {
 108  1 monitor.wait(500);
 109    }
 110    }
 111   
 112  1 tm.suspend();
 113   
 114    // Confirm that B's tx replicated
 115  1 assertTrue(cacheA.exists("/EXISTS"));
 116   
 117  1 cacheB.stop();
 118  1 cacheB.destroy();
 119   
 120  1 while (cacheA.getMembers().size() > 1)
 121    {
 122  1 try
 123    {
 124  1 Thread.sleep(100);
 125    }
 126    catch (InterruptedException e) {}
 127    }
 128   
 129  1 assertEquals("A", cacheA.get("/1/A", "1"));
 130  1 assertEquals("A", cacheA.get("/1/A", "2"));
 131  1 assertEquals("A", cacheA.get("/2/A", "1"));
 132  1 assertEquals("A", cacheA.get("/2/A", "2"));
 133  1 assertEquals("A", cacheA.get("/1/A/I", "1"));
 134  1 assertEquals("A", cacheA.get("/1/A/I", "2"));
 135   
 136  1 if (t.isAlive())
 137    {
 138  1 t.interrupt();
 139    }
 140    }
 141   
 142  2 protected CacheImpl createCache(String cacheID) throws Exception
 143    {
 144  2 if (caches.get(cacheID) != null)
 145    {
 146  0 throw new IllegalStateException(cacheID + " already created");
 147    }
 148   
 149  2 CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 150  2 cache.setConfiguration(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC));
 151   
 152  2 cache.create();
 153  2 cache.start();
 154   
 155  2 caches.put(cacheID, cache);
 156   
 157  2 return cache;
 158    }
 159   
 160  1 protected void tearDown() throws Exception
 161    {
 162  1 super.tearDown();
 163   
 164  1 Set keys = caches.keySet();
 165  1 String[] cacheIDs = new String[keys.size()];
 166  1 cacheIDs = (String[]) keys.toArray(cacheIDs);
 167  1 for (int i = 0; i < cacheIDs.length; i++)
 168    {
 169  2 stopCache(cacheIDs[i]);
 170    }
 171    }
 172   
 173  2 protected void stopCache(String id)
 174    {
 175  2 CacheImpl cache = (CacheImpl) caches.get(id);
 176  2 if (cache != null)
 177    {
 178  2 try
 179    {
 180  2 cache.stop();
 181  2 cache.destroy();
 182  2 caches.remove(id);
 183    }
 184    catch (Exception e)
 185    {
 186  0 System.out.println("Exception stopping cache " + e.getMessage());
 187  0 e.printStackTrace(System.out);
 188    }
 189    }
 190    }
 191   
 192    class HangSync implements Synchronization
 193    {
 194    private boolean hung = false;
 195    private Object monitor;
 196   
 197  1 HangSync(Object monitor)
 198    {
 199  1 this.monitor = monitor;
 200    }
 201   
 202  1 public void afterCompletion(int arg0)
 203    {
 204    }
 205   
 206  1 public void beforeCompletion()
 207    {
 208  1 hung = true;
 209  1 synchronized (monitor)
 210    {
 211  1 monitor.notifyAll();
 212    }
 213  1 try
 214    {
 215  1 Thread.sleep(30000);
 216    }
 217    catch (InterruptedException e) {}
 218    }
 219   
 220   
 221    }
 222    }