1 25 26 package org.objectweb.perseus.concurrency; 27 28 import junit.framework.TestCase; 29 import org.objectweb.perseus.persistence.PInteger; 30 import org.objectweb.perseus.persistence.PIntegerState; 31 import org.objectweb.perseus.persistence.TPMTest; 32 import org.objectweb.perseus.cache.replacement.lib.LRUReplacementManager; 33 import org.objectweb.perseus.concurrency.api.ConcurrencyException; 34 import org.objectweb.perseus.persistence.api.PersistenceException; 35 import org.objectweb.perseus.persistence.api.TransactionalWorkingSet; 36 import org.objectweb.perseus.persistence.concurrency.POptimisticConcurrencyManager; 37 38 public class TestOptimistic extends TestCase { 39 40 PInteger o1, o2; 41 Object oid1, oid2; 42 43 TransactionalWorkingSet ctxt1, ctxt2; 44 TPMTest tpm; 45 46 public TestOptimistic(String s) throws Exception { 47 super(s); 48 } 49 50 protected void setUp() throws Exception { 51 52 tpm = new TPMTest("TPM", new POptimisticConcurrencyManager(), new LRUReplacementManager()); 53 ctxt1 = (TransactionalWorkingSet) tpm.createWS("1"); 54 ctxt2 = (TransactionalWorkingSet) tpm.createWS("2"); 55 o1 = new PInteger(1); 56 o2 = new PInteger(2); 57 58 tpm.begin(ctxt1); 59 oid1 = tpm.export(ctxt1, o1); 60 oid2 = tpm.export(ctxt1, o2); 61 tpm.prepare(ctxt1); 62 tpm.commit(ctxt1); 63 64 } 65 66 public void test1 () throws ConcurrencyException, PersistenceException { 68 tpm.begin(ctxt1); 69 tpm.readIntention(ctxt1, oid1); 70 assertTrue(tpm.prepare(ctxt1)); 71 tpm.commit(ctxt1); 72 } 73 74 public void test2 () throws ConcurrencyException, PersistenceException { 76 tpm.begin(ctxt1); 77 tpm.readIntention(ctxt1, oid1); 78 tpm.writeIntention(ctxt1, oid1); 79 assertTrue(tpm.prepare(ctxt1)); 80 tpm.commit(ctxt1); 81 } 82 83 public void test3 () throws ConcurrencyException, PersistenceException { 85 tpm.begin(ctxt1); 86 tpm.begin(ctxt2); 87 tpm.readIntention(ctxt1, oid1); 88 tpm.readIntention(ctxt2, oid1); 89 assertTrue(tpm.prepare(ctxt1)); 90 tpm.commit(ctxt1); 91 assertTrue(tpm.prepare(ctxt2)); 92 tpm.commit(ctxt2); 93 } 94 95 public void test4 () throws ConcurrencyException, PersistenceException { 97 PIntegerState s1, s2; 98 tpm.begin(ctxt1); 99 tpm.begin(ctxt2); 100 s1 = (PIntegerState) tpm.readIntention(ctxt1, oid1); 101 s2 = (PIntegerState) tpm.readIntention(ctxt2, oid1); 102 s2 = (PIntegerState) tpm.writeIntention(ctxt2, oid1); 103 s2.i = 2; 104 105 s1 = (PIntegerState) tpm.readIntention(ctxt1, oid1); 107 assertTrue(s1.i == 1); 108 assertTrue(tpm.prepare(ctxt2)); 109 tpm.commit(ctxt2); 110 111 s1 = (PIntegerState) tpm.readIntention(ctxt1, oid1); 113 assertTrue(s1.i == 1); 114 tpm.rollback(ctxt1); 116 118 tpm.begin(ctxt1); 120 s1 = (PIntegerState) tpm.readIntention(ctxt1, oid1); 121 assertTrue(s1.i == 2); 122 assertTrue(tpm.prepare(ctxt1)); 123 tpm.commit(ctxt1); 124 } 125 126 public void test5 () throws ConcurrencyException, PersistenceException { 128 tpm.begin(ctxt1); 129 tpm.begin(ctxt2); 130 tpm.readIntention(ctxt1, oid1); 131 tpm.writeIntention(ctxt1, oid1); 132 tpm.readIntention(ctxt2, oid1); 133 assertTrue(tpm.prepare(ctxt1)); 134 tpm.commit(ctxt1); 135 tpm.rollback(ctxt2); 136 } 137 138 public void test6 () throws ConcurrencyException, PersistenceException { 140 tpm.begin(ctxt1); 141 tpm.begin(ctxt2); 142 tpm.readIntention(ctxt1, oid1); 143 tpm.writeIntention(ctxt1, oid1); 144 tpm.readIntention(ctxt2, oid1); 145 tpm.writeIntention(ctxt2, oid1); 146 assertTrue(tpm.prepare(ctxt1)); 147 tpm.commit(ctxt1); 148 tpm.rollback(ctxt2); 149 } 150 151 public void test7 () throws ConcurrencyException, PersistenceException { 154 tpm.begin(ctxt1); 155 tpm.begin(ctxt2); 156 tpm.readIntention(ctxt1, oid1); 157 tpm.readIntention(ctxt2, oid2); 158 tpm.readIntention(ctxt1, oid1); 159 tpm.writeIntention(ctxt1, oid1); 160 tpm.readIntention(ctxt2, oid2); 161 tpm.writeIntention(ctxt2, oid2); 162 assertTrue(tpm.prepare(ctxt1)); 163 tpm.commit(ctxt1); 164 assertTrue(tpm.prepare(ctxt2)); 165 tpm.commit(ctxt2); 166 } 167 168 public void test9 () throws ConcurrencyException, PersistenceException { 171 tpm.begin(ctxt1); 172 tpm.begin(ctxt2); 173 tpm.readIntention(ctxt1, oid1); 174 tpm.readIntention(ctxt2, oid2); 175 tpm.readIntention(ctxt1, oid2); 176 tpm.writeIntention(ctxt1, oid2); 177 tpm.readIntention(ctxt2, oid1); 178 tpm.writeIntention(ctxt2, oid1); 179 assertTrue(tpm.prepare(ctxt1)); 180 tpm.commit(ctxt1); 181 tpm.rollback(ctxt2); 182 } 183 } 184 | Popular Tags |