1 7 package org.jboss.cache.optimistic; 8 9 import org.jboss.cache.CacheImpl; 10 import org.jboss.cache.GlobalTransaction; 11 import org.jboss.cache.OptimisticTransactionEntry; 12 import org.jboss.cache.TransactionTable; 13 import org.jboss.cache.loader.SamplePojo; 14 import org.jboss.cache.marshall.MethodCall; 15 import org.jboss.cache.marshall.MethodCallFactory; 16 import org.jboss.cache.marshall.MethodDeclarations; 17 import org.jboss.cache.transaction.DummyTransactionManager; 18 19 import javax.transaction.Transaction ; 20 import java.util.List ; 21 22 public class TxInterceptorTest extends AbstractOptimisticTestCase 23 { 24 public TxInterceptorTest(String name) 25 { 26 super(name); 27 } 28 29 public void testNoTransaction() throws Exception 30 { 31 32 CacheImpl cache = createCache(); 33 MockInterceptor dummy = new MockInterceptor(); 34 dummy.setCache(cache); 35 36 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 37 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 38 assertNull(mgr.getTransaction()); 39 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 40 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 41 42 SamplePojo pojo = new SamplePojo(21, "test"); 43 44 cache.put("/one/two", "key1", pojo); 45 assertNull(mgr.getTransaction()); 46 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 47 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 48 49 51 List calls = dummy.getAllCalled(); 52 53 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 54 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 55 57 } 58 59 public void testLocalTransactionExists() throws Exception 60 { 61 62 CacheImpl cache = createCache(); 63 MockInterceptor dummy = new MockInterceptor(); 64 dummy.setCache(cache); 65 66 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 67 68 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 69 70 mgr.begin(); 72 Transaction tx = mgr.getTransaction(); 73 74 SamplePojo pojo = new SamplePojo(21, "test"); 75 76 assertNotNull(mgr.getTransaction()); 77 TransactionTable txTable = cache.getTransactionTable(); 78 assertNull(txTable.get(tx)); 79 80 cache.put("/one/two", "key1", pojo); 81 82 assertNotNull(mgr.getTransaction()); 83 mgr.commit(); 84 85 assertNull(mgr.getTransaction()); 86 87 List calls = dummy.getAllCalled(); 88 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 89 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 90 91 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 92 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 93 cache.stop(); 94 95 } 96 97 public void testRollbackTransaction() throws Exception 98 { 99 100 CacheImpl cache = createCache(); 101 MockInterceptor dummy = new MockInterceptor(); 102 dummy.setCache(cache); 103 104 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 105 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 106 107 mgr.begin(); 109 110 SamplePojo pojo = new SamplePojo(21, "test"); 111 112 cache.put("/one/two", "key1", pojo); 113 114 assertNotNull(mgr.getTransaction()); 115 mgr.rollback(); 116 117 assertNull(mgr.getTransaction()); 118 119 List calls = dummy.getAllCalled(); 120 assertEquals(1, calls.size()); 121 assertEquals(MethodDeclarations.rollbackMethod, calls.get(0)); 122 123 124 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 125 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 126 cache.stop(); 127 128 } 129 130 public void testEmptyLocalTransaction() throws Exception 131 { 132 CacheImpl cache = createCache(); 133 MockInterceptor dummy = new MockInterceptor(); 134 dummy.setCache(cache); 135 136 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 137 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 138 139 mgr.begin(); 141 142 assertNotNull(mgr.getTransaction()); 143 mgr.commit(); 144 145 assertNull(mgr.getTransaction()); 146 147 List calls = dummy.getAllCalled(); 148 assertEquals(0, calls.size()); 149 150 assertNull(mgr.getTransaction()); 151 152 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 153 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 154 155 cache.stop(); 156 } 157 158 public void testEmptyRollbackLocalTransaction() throws Exception 159 { 160 161 CacheImpl cache = createCache(); 162 MockInterceptor dummy = new MockInterceptor(); 163 dummy.setCache(cache); 164 165 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 166 167 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 168 169 mgr.begin(); 171 172 assertNotNull(mgr.getTransaction()); 173 mgr.rollback(); 174 175 assertNull(mgr.getTransaction()); 176 177 List calls = dummy.getAllCalled(); 178 assertEquals(0, calls.size()); 179 180 assertNull(mgr.getTransaction()); 181 182 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 183 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 184 cache.stop(); 185 186 } 187 188 public void testLocalRollbackAftercommitTransaction() throws Exception 189 { 190 191 CacheImpl cache = createCache(); 192 MockInterceptor dummy = new MockInterceptor(); 193 dummy.setCache(cache); 194 195 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 196 197 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 198 199 mgr.begin(); 201 202 SamplePojo pojo = new SamplePojo(21, "test"); 203 204 cache.put("/one/two", "key1", pojo); 205 206 assertNotNull(mgr.getTransaction()); 207 mgr.commit(); 208 209 assertNull(mgr.getTransaction()); 210 211 List calls = dummy.getAllCalled(); 212 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 213 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 214 boolean failed = false; 215 try 216 { 217 mgr.rollback(); 218 fail(); 219 } 220 catch (Exception e) 221 { 222 e.printStackTrace(); 223 failed = true; 224 assertTrue(true); 225 } 226 assertTrue(failed); 227 assertNull(mgr.getTransaction()); 228 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 229 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 230 cache.stop(); 231 } 232 233 234 public void testgtxTransactionExists() throws Exception 235 { 236 CacheImpl cache = createCache(); 237 MockInterceptor dummy = new MockInterceptor(); 238 dummy.setCache(cache); 239 240 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 241 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 242 243 mgr.begin(); 245 Transaction tx = mgr.getTransaction(); 246 247 cache.getCurrentTransaction(tx); 249 250 SamplePojo pojo = new SamplePojo(21, "test"); 251 252 cache.put("/one/two", "key1", pojo); 253 254 assertNotNull(mgr.getTransaction()); 255 mgr.commit(); 256 257 assertNull(mgr.getTransaction()); 258 259 List calls = dummy.getAllCalled(); 260 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 261 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 262 263 264 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 265 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 266 267 cache.stop(); 268 } 269 270 271 public void testRemotePrepareTransaction() throws Exception 272 { 273 274 CacheImpl cache = createCache(); 275 MockInterceptor dummy = new MockInterceptor(); 276 dummy.setCache(cache); 277 278 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 279 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 280 281 mgr.begin(); 283 Transaction tx = mgr.getTransaction(); 284 285 286 SamplePojo pojo = new SamplePojo(21, "test"); 287 288 cache.put("/one/two", "key1", pojo); 289 290 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 291 TransactionTable table = cache.getTransactionTable(); 292 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 293 assertNotNull(mgr.getTransaction()); 294 mgr.commit(); 295 296 List calls = dummy.getAllCalled(); 298 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 299 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 300 assertNull(mgr.getTransaction()); 301 302 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 303 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 304 305 306 GlobalTransaction remoteGtx = new GlobalTransaction(); 307 308 remoteGtx.setAddress(new DummyAddress()); 309 MethodCall meth = entry.getModifications().get(0); 311 312 meth.getArgs()[0] = remoteGtx; 313 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE); 315 try 316 { 317 cache._replicate(prepareMethod); 318 } 319 catch (Throwable t) 320 { 321 fail(); 322 } 323 324 assertNull(mgr.getTransaction()); 326 327 assertNotNull(table.get(remoteGtx)); 329 assertNotNull(table.getLocalTransaction(remoteGtx)); 330 331 calls = dummy.getAllCalled(); 333 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 334 335 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions()); 337 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions()); 338 cache.stop(); 339 } 340 341 public void testRemotePrepareSuspendTransaction() throws Exception 342 { 343 344 CacheImpl cache = createCache(); 345 MockInterceptor dummy = new MockInterceptor(); 346 dummy.setCache(cache); 347 348 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 349 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 350 351 mgr.begin(); 353 Transaction tx = mgr.getTransaction(); 354 355 356 SamplePojo pojo = new SamplePojo(21, "test"); 357 358 cache.put("/one/two", "key1", pojo); 359 360 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 361 TransactionTable table = cache.getTransactionTable(); 362 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 363 assertEquals(tx, mgr.getTransaction()); 364 365 367 GlobalTransaction remoteGtx = new GlobalTransaction(); 368 369 remoteGtx.setAddress(new DummyAddress()); 370 MethodCall meth = entry.getModifications().get(0); 372 373 meth.getArgs()[0] = remoteGtx; 374 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE); 376 try 377 { 378 cache._replicate(prepareMethod); 379 } 380 catch (Throwable t) 381 { 382 t.printStackTrace(); 383 fail(); 384 } 385 386 assertEquals(tx, mgr.getTransaction()); 388 389 assertNotNull(table.get(remoteGtx)); 391 assertNotNull(table.getLocalTransaction(remoteGtx)); 392 393 394 List calls = dummy.getAllCalled(); 395 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 396 397 assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions()); 399 assertEquals(2, cache.getTransactionTable().getNumLocalTransactions()); 400 401 mgr.commit(); 403 404 calls = dummy.getAllCalled(); 406 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(1)); 407 assertEquals(MethodDeclarations.commitMethod, calls.get(2)); 408 409 411 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions()); 412 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions()); 413 414 assertNotNull(table.get(remoteGtx)); 415 assertNotNull(table.getLocalTransaction(remoteGtx)); 416 417 assertNull(table.get(gtx)); 418 assertNull(table.getLocalTransaction(gtx)); 419 assertEquals(null, mgr.getTransaction()); 421 cache.stop(); 422 } 423 424 public void testRemoteCommitSuspendTransaction() throws Exception 425 { 426 427 CacheImpl cache = createCache(); 428 MockInterceptor dummy = new MockInterceptor(); 429 dummy.setCache(cache); 430 431 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 432 433 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 434 435 mgr.begin(); 437 Transaction tx = mgr.getTransaction(); 438 439 cache.getCurrentTransaction(tx); 441 442 SamplePojo pojo = new SamplePojo(21, "test"); 443 444 cache.put("/one/two", "key1", pojo); 445 446 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 447 TransactionTable table = cache.getTransactionTable(); 448 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 449 assertEquals(tx, mgr.getTransaction()); 450 451 453 GlobalTransaction remoteGtx = new GlobalTransaction(); 454 455 remoteGtx.setAddress(new DummyAddress()); 456 MethodCall meth = entry.getModifications().get(0); 458 459 meth.getArgs()[0] = remoteGtx; 460 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE); 462 try 463 { 464 cache._replicate(prepareMethod); 465 } 466 catch (Throwable t) 467 { 468 fail(); 469 } 470 471 assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions()); 472 assertEquals(2, cache.getTransactionTable().getNumLocalTransactions()); 473 474 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, remoteGtx); 476 try 477 { 478 cache._replicate(commitMethod); 479 } 480 catch (Throwable t) 481 { 482 t.printStackTrace(); 483 fail(); 484 } 485 486 assertEquals(tx, mgr.getTransaction()); 488 489 assertNull(table.get(remoteGtx)); 491 assertNull(table.getLocalTransaction(remoteGtx)); 492 493 List calls = dummy.getAllCalled(); 494 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 495 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 496 497 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions()); 498 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions()); 499 500 mgr.commit(); 502 503 calls = dummy.getAllCalled(); 504 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 505 assertEquals(MethodDeclarations.commitMethod, calls.get(3)); 506 507 508 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 509 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 510 511 assertEquals(null, mgr.getTransaction()); 512 cache.stop(); 513 } 514 515 public void testRemoteRollbackSuspendTransaction() throws Exception 516 { 517 518 CacheImpl cache = createCache(); 519 MockInterceptor dummy = new MockInterceptor(); 520 dummy.setCache(cache); 521 522 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 523 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 524 525 mgr.begin(); 527 Transaction tx = mgr.getTransaction(); 528 529 cache.getCurrentTransaction(tx); 531 532 SamplePojo pojo = new SamplePojo(21, "test"); 533 534 cache.put("/one/two", "key1", pojo); 535 536 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 537 TransactionTable table = cache.getTransactionTable(); 538 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 539 assertEquals(tx, mgr.getTransaction()); 540 541 543 GlobalTransaction remoteGtx = new GlobalTransaction(); 544 545 remoteGtx.setAddress(new DummyAddress()); 546 MethodCall meth = entry.getModifications().get(0); 548 549 meth.getArgs()[0] = remoteGtx; 550 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE); 552 try 553 { 554 cache._replicate(prepareMethod); 555 } 556 catch (Throwable t) 557 { 558 fail(); 559 } 560 561 assertEquals(2, cache.getTransactionTable().getNumGlobalTransactions()); 562 assertEquals(2, cache.getTransactionTable().getNumLocalTransactions()); 563 564 MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, remoteGtx); 566 try 567 { 568 cache._replicate(rollbackMethod); 569 } 570 catch (Throwable t) 571 { 572 fail(); 573 } 574 575 assertEquals(tx, mgr.getTransaction()); 577 578 assertNull(table.get(remoteGtx)); 580 assertNull(table.getLocalTransaction(remoteGtx)); 581 582 List calls = dummy.getAllCalled(); 583 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 584 assertEquals(MethodDeclarations.rollbackMethod, calls.get(1)); 585 586 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions()); 587 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions()); 588 589 mgr.commit(); 591 592 calls = dummy.getAllCalled(); 593 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 594 assertEquals(MethodDeclarations.commitMethod, calls.get(3)); 595 596 597 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 598 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 599 600 assertEquals(null, mgr.getTransaction()); 601 cache.stop(); 602 } 603 604 public void testRemoteCommitTransaction() throws Exception 605 { 606 607 CacheImpl cache = createCache(); 608 MockInterceptor dummy = new MockInterceptor(); 609 dummy.setCache(cache); 610 611 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 612 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 613 614 mgr.begin(); 616 Transaction tx = mgr.getTransaction(); 617 618 cache.getCurrentTransaction(tx); 620 621 SamplePojo pojo = new SamplePojo(21, "test"); 622 623 cache.put("/one/two", "key1", pojo); 624 625 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 626 TransactionTable table = cache.getTransactionTable(); 627 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 628 assertNotNull(mgr.getTransaction()); 629 mgr.commit(); 630 631 List calls = dummy.getAllCalled(); 633 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 634 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 635 636 assertNull(mgr.getTransaction()); 637 638 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 639 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 640 641 642 GlobalTransaction remoteGtx = new GlobalTransaction(); 643 644 remoteGtx.setAddress(new DummyAddress()); 645 646 MethodCall meth = entry.getModifications().get(0); 648 649 meth.getArgs()[0] = remoteGtx; 650 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE); 652 try 653 { 654 cache._replicate(prepareMethod); 655 } 656 catch (Throwable t) 657 { 658 fail(); 659 } 660 661 assertNull(mgr.getTransaction()); 663 assertEquals(1, cache.getTransactionTable().getNumGlobalTransactions()); 664 assertEquals(1, cache.getTransactionTable().getNumLocalTransactions()); 665 666 assertNotNull(table.get(remoteGtx)); 668 assertNotNull(table.getLocalTransaction(remoteGtx)); 669 assertEquals(1, table.get(remoteGtx).getModifications().size()); 671 672 calls = dummy.getAllCalled(); 673 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 674 675 assertNull(mgr.getTransaction()); 676 MethodCall commitMethod = MethodCallFactory.create(MethodDeclarations.commitMethod, remoteGtx, Boolean.TRUE); 678 try 679 { 680 cache._replicate(commitMethod); 681 } 682 catch (Throwable t) 683 { 684 fail(); 685 } 686 687 assertNull(table.get(remoteGtx)); 688 assertNull(table.getLocalTransaction(remoteGtx)); 689 690 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 691 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 692 assertNull(mgr.getTransaction()); 693 cache.stop(); 694 695 } 696 697 public void testRemoteRollbackTransaction() throws Exception 698 { 699 700 CacheImpl cache = createCache(); 701 MockInterceptor dummy = new MockInterceptor(); 702 dummy.setCache(cache); 703 704 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, true)); 705 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 706 707 mgr.begin(); 709 Transaction tx = mgr.getTransaction(); 710 711 cache.getCurrentTransaction(tx); 713 714 SamplePojo pojo = new SamplePojo(21, "test"); 715 716 cache.put("/one/two", "key1", pojo); 717 718 GlobalTransaction gtx = cache.getCurrentTransaction(tx); 719 TransactionTable table = cache.getTransactionTable(); 720 OptimisticTransactionEntry entry = (OptimisticTransactionEntry) table.get(gtx); 721 assertNotNull(mgr.getTransaction()); 722 mgr.commit(); 723 724 List calls = dummy.getAllCalled(); 726 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 727 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 728 729 GlobalTransaction remoteGtx = new GlobalTransaction(); 730 731 remoteGtx.setAddress(new DummyAddress()); 732 733 MethodCall meth = entry.getModifications().get(0); 735 736 meth.getArgs()[0] = remoteGtx; 737 MethodCall prepareMethod = MethodCallFactory.create(MethodDeclarations.optimisticPrepareMethod, remoteGtx, injectDataVersion(entry.getModifications()), null, remoteGtx.getAddress(), Boolean.FALSE); 739 try 740 { 741 cache._replicate(prepareMethod); 742 } 743 catch (Throwable t) 744 { 745 fail(); 746 } 747 748 assertNull(mgr.getTransaction()); 750 751 assertNotNull(table.get(remoteGtx)); 753 assertNotNull(table.getLocalTransaction(remoteGtx)); 754 assertEquals(1, table.get(remoteGtx).getModifications().size()); 756 757 calls = dummy.getAllCalled(); 758 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 759 760 MethodCall rollbackMethod = MethodCallFactory.create(MethodDeclarations.rollbackMethod, remoteGtx); 762 try 763 { 764 cache._replicate(rollbackMethod); 765 } 766 catch (Throwable t) 767 { 768 fail(); 769 } 770 771 calls = dummy.getAllCalled(); 772 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 773 assertEquals(MethodDeclarations.rollbackMethod, calls.get(3)); 774 775 assertNull(table.get(remoteGtx)); 776 assertNull(table.getLocalTransaction(remoteGtx)); 777 778 assertEquals(0, cache.getTransactionTable().getNumGlobalTransactions()); 779 assertEquals(0, cache.getTransactionTable().getNumLocalTransactions()); 780 cache.stop(); 781 782 } 783 784 785 public void testSequentialTransactionExists() throws Exception 786 { 787 788 CacheImpl cache = createCache(); 789 MockInterceptor dummy = new MockInterceptor(); 790 dummy.setCache(cache); 791 792 cache.setInterceptorChain(getAlteredInterceptorChain(dummy, cache, false)); 793 794 DummyTransactionManager mgr = DummyTransactionManager.getInstance(); 795 796 mgr.begin(); 797 Transaction tx = mgr.getTransaction(); 798 SamplePojo pojo = new SamplePojo(21, "test"); 799 800 cache.put("/one/two", "key1", pojo); 801 802 assertNotNull(mgr.getTransaction()); 803 mgr.commit(); 804 805 806 mgr.begin(); 807 Transaction tx1 = mgr.getTransaction(); 808 assertNotNull(tx1); 809 assertNotSame(tx, tx1); 810 cache.put("/one/two", "key1", pojo); 811 mgr.commit(); 812 813 814 List calls = dummy.getAllCalled(); 815 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(0)); 816 assertEquals(MethodDeclarations.commitMethod, calls.get(1)); 817 818 assertEquals(MethodDeclarations.optimisticPrepareMethod, calls.get(2)); 819 assertEquals(MethodDeclarations.commitMethod, calls.get(3)); 820 cache.stop(); 821 } 822 823 } 824 | Popular Tags |