1 7 package org.jboss.cache.optimistic; 8 9 import junit.framework.Assert; 10 import org.jboss.cache.CacheImpl; 11 import org.jboss.cache.Fqn; 12 import org.jboss.cache.NodeSPI; 13 import org.jboss.cache.config.Configuration; 14 15 import javax.transaction.RollbackException ; 16 import javax.transaction.Transaction ; 17 import javax.transaction.TransactionManager ; 18 19 24 public class OptimisticVersioningTest extends AbstractOptimisticTestCase 25 { 26 CacheImpl cache1, cache2; 27 28 public OptimisticVersioningTest(String name) 29 { 30 super(name); 31 } 32 33 protected void setUp() throws Exception 34 { 35 cache1 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC); 36 cache2 = createReplicatedCache(Configuration.CacheMode.REPL_SYNC); 37 } 38 39 protected void tearDown() 40 { 41 super.tearDown(); 42 destroyCache(cache1); 43 destroyCache(cache2); 44 cache1 = null; 45 cache2 = null; 46 } 47 48 public void testVersionPropagation() throws Exception 49 { 50 Fqn fqn = Fqn.fromString("/a/b"); 51 String key = "key"; 52 53 cache1.put(fqn, key, "value"); 54 55 DataVersion v1 = ((NodeSPI) cache1.get(fqn)).getVersion(); 56 DataVersion v2 = ((NodeSPI) cache2.get(fqn)).getVersion(); 57 58 Assert.assertEquals("value", cache1.get(fqn, key)); 59 Assert.assertEquals("value", cache2.get(fqn, key)); 60 Assert.assertEquals("Version info should have propagated", v1, v2); 61 62 cache1.put(fqn, key, "value2"); 64 65 v1 = ((NodeSPI) cache1.get(fqn)).getVersion(); 66 v2 = ((NodeSPI) cache2.get(fqn)).getVersion(); 67 68 Assert.assertEquals("value2", cache1.get(fqn, key)); 69 Assert.assertEquals("value2", cache2.get(fqn, key)); 70 Assert.assertEquals("Version info should have propagated", v1, v2); 71 } 72 73 public void testTwoCachesUpdatingSimultaneously() throws Exception 74 { 75 TransactionManager mgr1 = cache1.getTransactionManager(); 76 TransactionManager mgr2 = cache2.getTransactionManager(); 77 Transaction tx1, tx2; 78 79 Fqn fqn = Fqn.fromString("/a/b"); 80 String key = "key"; 81 82 cache1.put(fqn, key, "value"); 83 84 DataVersion v1 = ((NodeSPI) cache1.get(fqn)).getVersion(); 85 DataVersion v2 = ((NodeSPI) cache2.get(fqn)).getVersion(); 86 87 Assert.assertEquals("value", cache1.get(fqn, key)); 88 Assert.assertEquals("value", cache2.get(fqn, key)); 89 Assert.assertEquals("Version info should have propagated", v1, v2); 90 91 mgr1.begin(); 93 cache1.put(fqn, key, "value2"); 94 tx1 = mgr1.suspend(); 95 96 mgr2.begin(); 98 cache2.put(fqn, key, "value3"); 99 tx2 = mgr2.suspend(); 100 101 mgr1.resume(tx1); 103 mgr1.commit(); 105 106 try 107 { 108 mgr2.resume(tx2); 109 mgr2.commit(); 110 Assert.assertTrue("Should have failed", false); 111 } 112 catch (RollbackException rbe) 113 { 114 Assert.assertTrue("Should have failed", true); 115 } 116 117 v1 = ((NodeSPI) cache1.get(fqn)).getVersion(); 119 v2 = ((NodeSPI) cache2.get(fqn)).getVersion(); 120 121 Assert.assertEquals("Version info should have propagated", v1, v2); 122 } 123 } 124 | Popular Tags |