1 7 8 package org.jboss.cache.pojo.rollback; 9 10 import junit.framework.TestCase; 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.pojo.*; 16 import org.jboss.cache.pojo.test.Person; 17 import org.jboss.cache.transaction.DummyTransactionManager; 18 19 import javax.naming.Context ; 20 import javax.naming.NamingException ; 21 import javax.naming.InitialContext ; 22 import javax.transaction.UserTransaction ; 23 import javax.transaction.SystemException ; 24 import javax.transaction.NotSupportedException ; 25 import javax.transaction.Transaction ; 26 import javax.transaction.RollbackException ; 27 import java.util.Properties ; 28 import java.util.List ; 29 import java.util.ArrayList ; 30 31 33 34 public class ReplicatedTxTest extends TestCase 35 { 36 Log log = LogFactory.getLog(org.jboss.cache.pojo.rollback.ReplicatedTxTest.class); 37 PojoCache cache, cache1; 38 final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory"; 39 DummyTransactionManager tx_mgr; 40 Throwable t1_ex, t2_ex; 41 long start = 0; 42 43 44 public ReplicatedTxTest(String name) 45 { 46 super(name); 47 } 48 49 protected void setUp() throws Exception 50 { 51 super.setUp(); 52 log.info("setUp() ...."); 53 String configFile = "META-INF/replSync-service.xml"; 54 boolean toStart = false; 55 cache = PojoCacheFactory.createCache(configFile, toStart); 56 cache.start(); 57 cache1 = PojoCacheFactory.createCache(configFile, toStart); 58 cache1.start(); 59 60 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY); 61 62 tx_mgr = DummyTransactionManager.getInstance(); 63 t1_ex = t2_ex = null; 64 } 65 66 protected void tearDown() throws Exception 67 { 68 super.tearDown(); 69 cache.stop(); 70 cache1.stop(); 71 72 DummyTransactionManager.destroy(); 73 } 74 75 77 UserTransaction getTransaction() throws SystemException , NotSupportedException , NamingException 78 { 79 Properties prop = new Properties (); 80 prop.put(Context.INITIAL_CONTEXT_FACTORY, 81 "org.jboss.cache.transaction.DummyContextFactory"); 82 return (UserTransaction ) new InitialContext (prop).lookup("UserTransaction"); 83 } 84 85 private Person createPerson(String id, String name, int age) 86 { 87 Person p = new Person(); 88 p.setName(name); 89 p.setAge(age); 90 return p; 91 } 92 93 public void testSimple() throws Exception 94 { 95 log.info("testSimple() ...."); 96 UserTransaction tx = getTransaction(); 97 tx.begin(); 98 Person p = createPerson("/person/test1", "Harald Gliebe", 32); 99 cache.attach("/person/test1", p); 100 101 tx.commit(); 102 tx.begin(); 103 p.setName("Benoit"); 104 tx.commit(); 105 Person p1 = (Person) cache1.find("/person/test1"); 106 assertEquals("Benoit", p.getName()); 107 assertEquals("Benoit", p1.getName()); 108 tx.begin(); 109 p1.setAge(61); 110 tx.commit(); 111 assertEquals(61, p.getAge()); 112 assertEquals(61, p1.getAge()); 113 } 114 115 118 public void testConcurrentPuts() throws Exception 119 { 120 Thread t1 = new Thread () 121 { 122 Transaction tx; 123 124 public void run() 125 { 126 try 127 { 128 Person p = createPerson("/person/test6", "p6", 50); 129 List <String > lang = new ArrayList <String >(); 130 lang.add("German"); 131 p.setLanguages(lang); 132 UserTransaction tx = getTransaction(); 133 tx.begin(); 134 cache.attach("/person/test6", p); 135 TestingUtil.sleepThread(17000); 136 tx.commit(); 137 } 138 catch (RollbackException rollback) 139 { 140 ; 141 } 142 catch (Exception ex) 143 { 144 t1_ex = ex; 145 } 146 } 147 }; 148 149 Thread t2 = new Thread () 150 { 151 Transaction tx; 152 153 public void run() 154 { 155 UserTransaction tx = null; 156 Person p = createPerson("/person/test6", "p6", 50); 157 try 158 { 159 TestingUtil.sleepThread(1000); List <String > lang = new ArrayList <String >(); 161 lang.add("German"); 162 p.setLanguages(lang); 163 tx = getTransaction(); 164 tx.begin(); 165 cache.attach("/person/test6", p); 166 tx.commit(); 167 } 168 catch (RollbackException rollback) 169 { 170 ; 171 } 172 catch (Exception ex) 173 { 174 try 175 { 176 tx.rollback(); 177 } catch (SystemException e) 178 { 179 e.printStackTrace(); 180 t2_ex = e; 181 } 182 } 183 184 cache.attach("/person/test6", p); 185 186 } 187 }; 188 189 t1.start(); 190 t2.start(); 191 192 t1.join(); 193 t2.join(); 194 195 if (t2_ex != null) 197 fail("Thread1 failed: " + t2_ex); 198 if (t1_ex != null) 199 fail("Thread2 failed: " + t1_ex); 200 201 int size = ((Person) cache.find("/person/test6")).getLanguages().size(); 202 assertEquals("number of languages", 1, size); 203 size = ((Person) cache1.find("/person/test6")).getLanguages().size(); 204 assertEquals("number of languages", 1, size); 205 } 206 207 210 public void testConcurrentPuts1() throws Exception 211 { 212 Thread t1 = new Thread () 213 { 214 Transaction tx; 215 216 public void run() 217 { 218 try 219 { 220 List <String > lang = ((Person) cache.find("/person/test6")).getLanguages(); 221 UserTransaction tx = getTransaction(); 222 tx.begin(); 223 lang.add("German"); 224 TestingUtil.sleepThread(17000); 225 tx.commit(); 226 } 227 catch (RollbackException rollback) 228 { 229 ; 230 } 231 catch (Exception ex) 232 { 233 t1_ex = ex; 234 } 235 } 236 }; 237 238 Thread t2 = new Thread () 239 { 240 Transaction tx; 241 242 public void run() 243 { 244 UserTransaction tx = null; 245 try 246 { 247 TestingUtil.sleepThread(1000); List <String > lang = ((Person) cache1.find("/person/test6")).getLanguages(); 249 tx = getTransaction(); 250 tx.begin(); 251 lang.add("English"); 252 tx.commit(); 253 } 254 catch (RollbackException rollback) 255 { 256 ; 257 } 258 catch (Exception ex) 259 { 260 try 261 { 262 tx.rollback(); 263 } catch (SystemException e) 264 { 265 e.printStackTrace(); 266 t2_ex = e; 267 } 268 } 269 } 270 }; 271 272 Person p = createPerson("/person/test6", "p6", 50); 273 cache.attach("/person/test6", p); 274 List <String > lang = new ArrayList <String >(); 275 lang.add("German"); 276 p.setLanguages(lang); 277 278 t1.start(); 279 t2.start(); 280 281 t1.join(); 282 t2.join(); 283 284 if (t2_ex != null) 286 fail("Thread1 failed: " + t2_ex); 287 if (t1_ex != null) 288 fail("Thread2 failed: " + t1_ex); 289 290 int size = ((Person) cache.find("/person/test6")).getLanguages().size(); 291 assertEquals("number of languages", 2, size); 292 size = ((Person) cache1.find("/person/test6")).getLanguages().size(); 293 assertEquals("number of languages", 2, size); 294 } 295 296 299 public void testConcurrentPuts2() throws Exception 300 { 301 Thread t1 = new Thread () 302 { 303 Transaction tx; 304 305 public void run() 306 { 307 try 308 { 309 List <String > lang = ((Person) cache.find("/person/test6")).getLanguages(); 310 UserTransaction tx = getTransaction(); 311 tx.begin(); 312 lang.add("German"); 313 TestingUtil.sleepThread(17000); 314 tx.commit(); 315 } 316 catch (RollbackException rollback) 317 { 318 ; 319 } 320 catch (Exception ex) 321 { 322 t1_ex = ex; 323 } 324 } 325 }; 326 327 Thread t2 = new Thread () 328 { 329 Transaction tx; 330 331 public void run() 332 { 333 UserTransaction tx = null; 334 try 335 { 336 TestingUtil.sleepThread(1000); List <String > lang = ((Person) cache.find("/person/test6")).getLanguages(); 338 tx = getTransaction(); 339 tx.begin(); 340 lang.add("English"); 341 tx.commit(); 342 } 343 catch (RollbackException rollback) 344 { 345 ; 346 } 347 catch (Exception ex) 348 { 349 try 350 { 351 tx.rollback(); 352 } catch (SystemException e) 353 { 354 e.printStackTrace(); 355 t2_ex = e; 356 } 357 } 358 } 359 }; 360 361 Person p = createPerson("/person/test6", "p6", 50); 362 cache.attach("/person/test6", p); 363 List <String > lang = new ArrayList <String >(); 364 lang.add("German"); 365 p.setLanguages(lang); 366 367 t1.start(); 368 t2.start(); 369 370 t1.join(); 371 t2.join(); 372 373 if (t2_ex != null) 375 fail("Thread1 failed: " + t2_ex); 376 if (t1_ex != null) 377 fail("Thread2 failed: " + t1_ex); 378 379 int size = ((Person) cache.find("/person/test6")).getLanguages().size(); 380 assertEquals("number of languages", 2, size); 381 size = ((Person) cache1.find("/person/test6")).getLanguages().size(); 382 assertEquals("number of languages", 2, size); 383 } 384 385 void log(String s) 386 { 387 long now; 388 if (start == 0) 389 start = System.currentTimeMillis(); 390 now = System.currentTimeMillis(); 391 392 System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s); 393 } 394 395 396 public static Test suite() throws Exception 397 { 398 return new TestSuite(org.jboss.cache.pojo.rollback.ReplicatedTxTest.class); 399 } 400 401 402 public static void main(String [] args) throws Exception 403 { 404 junit.textui.TestRunner.run(org.jboss.cache.pojo.rollback.ReplicatedTxTest.suite()); 405 } 406 407 } 408 | Popular Tags |