1 7 package org.jboss.cache.tests; 8 9 import junit.framework.Test; 10 import junit.framework.TestCase; 11 import junit.framework.TestSuite; 12 import org.jboss.cache.Fqn; 13 import org.jboss.cache.TreeCache; 14 import org.jboss.cache.lock.IsolationLevel; 15 import org.jboss.cache.lock.TimeoutException; 16 import org.jboss.cache.transaction.DummyTransactionManager; 17 18 import javax.transaction.NotSupportedException ; 19 import javax.transaction.SystemException ; 20 import javax.transaction.Transaction ; 21 22 27 public class TreeCacheFunctionalTest extends TestCase { 28 TreeCache cache=null; 29 Transaction tx=null; 30 final Fqn FQN=Fqn.fromString("/myNode"); 31 final String KEY="key"; 32 final String VALUE="value"; 33 Exception ex; 34 35 36 protected void setUp() throws Exception { 37 super.setUp(); 38 ex=null; 39 } 40 41 protected void tearDown() throws Exception { 42 super.tearDown(); 43 if(cache != null) { 44 cache.stopService(); 45 cache.destroyService(); 46 cache=null; 47 } 48 if(ex != null) 49 throw ex; 50 } 51 52 53 public void testFailFastPut() throws Exception { 54 cache=new TreeCache(); 55 cache.setCacheMode(TreeCache.LOCAL); 56 cache.startService(); 57 cache.put(FQN, KEY, VALUE); 58 cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 0); 59 cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 100); 60 cache.putFailFast(Fqn.fromString("/1/2/3"), KEY, VALUE, 100); 61 cache.putFailFast(Fqn.fromString("/1/2/3"), KEY, VALUE, 0); 62 System.out.println("cache: " + cache.printLockInfo()); 63 } 64 65 66 public void testFailFastWith2Transactions() throws Exception , NotSupportedException { 67 cache=new TreeCache(); 68 cache.setCacheMode(TreeCache.LOCAL); 69 cache.setIsolationLevel(IsolationLevel.REPEATABLE_READ); 70 cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 71 cache.startService(); 72 tx=startTransaction(); 73 cache.put("/a/b/c", KEY, VALUE); 74 75 class Loser extends Thread { 76 public void run() { 77 Transaction trans=null; 78 try { 79 trans=startTransaction(); 80 cache.putFailFast(Fqn.fromString("/a/b/c"), KEY, VALUE, 500); 81 fail("this should fail as /a/b/c is held by another thread"); 82 } 83 catch(TimeoutException timeoutEx) { 84 System.out.println("got TimeoutException (as expected)"); 85 } 86 catch(Exception e) { 87 ex=e; 88 } 89 } 90 } 91 92 System.out.println("locks before loser: " + cache.printLockInfo()); 93 Loser loser=new Loser(); 94 loser.start(); 95 loser.join(); 96 tx.commit(); 97 } 98 99 100 Transaction startTransaction() throws SystemException , NotSupportedException { 101 DummyTransactionManager mgr=DummyTransactionManager.getInstance(); 102 mgr.begin(); 103 Transaction tmptx=mgr.getTransaction(); 104 return tmptx; 105 } 106 107 108 109 public static Test suite() { 110 return new TestSuite(TreeCacheFunctionalTest.class); 111 } 112 113 117 } 118 | Popular Tags |