1 package org.jboss.cache.lock; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 import org.jboss.cache.CacheImpl; 7 import org.jboss.cache.Fqn; 8 import org.jboss.cache.NodeSPI; 9 import org.jboss.cache.config.Configuration; 10 import org.jboss.cache.transaction.DummyTransactionManager; 11 12 import javax.transaction.Transaction ; 13 14 18 public class AcquireAllTest extends TestCase 19 { 20 CacheImpl cache = null, cache2; 21 Transaction tx = null; 22 final Fqn FQN = Fqn.fromString("/myNode"); 23 final String KEY = "key"; 24 final String VALUE = "value"; 25 26 27 protected void setUp() throws Exception 28 { 29 super.setUp(); 30 } 31 32 protected void tearDown() throws Exception 33 { 34 super.tearDown(); 35 if (cache != null) 36 { 37 cache.stop(); 38 cache.destroy(); 39 cache = null; 40 } 41 if (tx != null) 42 { 43 tx.commit(); 44 tx = null; 45 } 46 } 47 48 49 public void testAcquireAll() throws Exception 50 { 51 NodeSPI root; 52 Object owner = Thread.currentThread(); 53 54 cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE); 55 cache.put("/a/b/c", null); 56 cache.put("/1/2/3", null); 57 58 root = cache.getRoot(); 59 NodeLock lock = root.getLock(); 60 61 lock.acquireAll(owner, 2000, NodeLock.LockType.READ); 62 lock.releaseAll(owner); 63 64 assertEquals(0, cache.getNumberOfLocksHeld()); 65 66 lock.acquireAll(owner, 2000, NodeLock.LockType.WRITE); 67 lock.releaseAll(owner); 68 69 assertEquals(0, cache.getNumberOfLocksHeld()); 70 } 71 72 73 public void testAcquireAllReplicated() throws Exception 74 { 75 NodeSPI root; 76 Object owner = Thread.currentThread(); 77 78 cache2 = createCache(Configuration.CacheMode.REPL_ASYNC, IsolationLevel.SERIALIZABLE); 79 cache2.put("/a/b/c", null); 80 cache2.put("/1/2/3", null); 81 82 cache = createCache(Configuration.CacheMode.REPL_ASYNC, IsolationLevel.SERIALIZABLE); 83 root = cache.getRoot(); 84 NodeLock lock = root.getLock(); 85 86 lock.acquireAll(owner, 2000, NodeLock.LockType.READ); 87 lock.releaseAll(owner); 88 89 assertEquals(0, cache.getNumberOfLocksHeld()); 90 91 lock.acquireAll(owner, 2000, NodeLock.LockType.WRITE); 92 lock.releaseAll(owner); 93 94 assertEquals(0, cache.getNumberOfLocksHeld()); 95 } 96 97 98 CacheImpl createCache(Configuration.CacheMode mode, IsolationLevel level) throws Exception 99 { 100 CacheImpl c = new CacheImpl(); 101 c.getConfiguration().setCacheMode(mode); 102 c.getConfiguration().setIsolationLevel(level); 103 c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 104 c.create(); 105 c.start(); 106 return c; 107 } 108 109 Transaction startTransaction() 110 { 111 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 112 try 113 { 114 mgr.begin(); 115 return mgr.getTransaction(); 116 } 117 catch (Throwable t) 118 { 119 return null; 120 } 121 } 122 123 124 public static Test suite() 125 { 126 return new TestSuite(AcquireAllTest.class); 127 } 128 129 public static void main(String [] args) 130 { 131 junit.textui.TestRunner.run(suite()); 132 } 133 134 } 135 | Popular Tags |