1 7 package org.jboss.cache.invalidation; 8 9 import junit.framework.Assert; 10 import junit.framework.Test; 11 import junit.framework.TestCase; 12 import junit.framework.TestSuite; 13 import junit.textui.TestRunner; 14 import org.apache.commons.logging.Log; 15 import org.apache.commons.logging.LogFactory; 16 import org.jboss.cache.CacheImpl; 17 import org.jboss.cache.Fqn; 18 import org.jboss.cache.config.CacheLoaderConfig; 19 import org.jboss.cache.config.Configuration; 20 import org.jboss.cache.factories.XmlConfigurationParser; 21 import org.jboss.cache.misc.TestingUtil; 22 import org.jboss.cache.xml.XmlHelper; 23 import org.w3c.dom.Element ; 24 25 import javax.transaction.RollbackException ; 26 import javax.transaction.Transaction ; 27 import javax.transaction.TransactionManager ; 28 import java.io.File ; 29 30 35 public class InvalidationInterceptorTest extends TestCase 36 { 37 38 private static Log log = LogFactory.getLog(InvalidationInterceptorTest.class); 39 40 public void testPessimisticNonTransactional() throws Exception 41 { 42 CacheImpl cache1 = createCache(false); 43 CacheImpl cache2 = createCache(false); 44 45 Fqn fqn = Fqn.fromString("/a/b"); 46 cache1.put(fqn, "key", "value"); 47 48 Assert.assertEquals("value", cache1.get(fqn, "key")); 50 Assert.assertNull("Should NOT have replicated!", cache2.get(fqn)); 51 52 log.info("***** Node not replicated, as expected."); 53 54 cache2.put(fqn, "key", "value"); 56 Assert.assertNull("Should be null", cache1.get(fqn)); 57 Assert.assertEquals("value", cache2.get(fqn, "key")); 58 59 cache1.put(fqn, "key2", "value2"); 61 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 62 Assert.assertNull("Should have been invalidated!", cache2.get(fqn)); 63 64 cache1.stop(); 66 cache2.stop(); 67 cache1 = null; 68 cache2 = null; 69 } 70 71 public void testUnnecessaryEvictions() throws Exception 72 { 73 CacheImpl cache1 = createCache(false); 74 CacheImpl cache2 = createCache(false); 75 76 Fqn fqn1 = Fqn.fromString("/a/b/c"); 77 Fqn fqn2 = Fqn.fromString("/a/b/d"); 78 79 cache1.put(fqn1, "hello", "world"); 80 81 assertEquals("world", cache1.get(fqn1, "hello")); 82 assertNull(cache2.get(fqn1, "hello")); 83 84 cache2.put(fqn2, "hello", "world"); 85 assertEquals("world", cache1.get(fqn1, "hello")); 86 assertNull(cache2.get(fqn1, "hello")); 87 assertEquals("world", cache2.get(fqn2, "hello")); 88 assertNull(cache1.get(fqn2, "hello")); 89 90 cache2.put(fqn1, "hello", "world"); 91 assertEquals("world", cache2.get(fqn1, "hello")); 92 assertEquals("world", cache2.get(fqn2, "hello")); 93 assertNull(cache1.get(fqn1, "hello")); 94 assertNull(cache1.get(fqn2, "hello")); 95 96 cache1.stop(); 97 cache2.stop(); 98 cache1 = null; 99 cache2 = null; 100 101 } 102 103 104 public void testPessimisticNonTransactionalAsync() throws Exception 105 { 106 CacheImpl cache1 = createUnstartedCache(false); 107 CacheImpl cache2 = createUnstartedCache(false); 108 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.INVALIDATION_ASYNC); 109 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.INVALIDATION_ASYNC); 110 cache1.start(); 111 cache2.start(); 112 113 Fqn fqn = Fqn.fromString("/a/b"); 114 cache1.put(fqn, "key", "value"); 115 TestingUtil.sleepThread(500); Assert.assertEquals("value", cache1.get(fqn, "key")); 118 Assert.assertNull("Should NOT have replicated!", cache2.get(fqn)); 119 120 log.info("***** Node not replicated, as expected."); 121 122 cache2.put(fqn, "key", "value"); 124 TestingUtil.sleepThread(500); Assert.assertNull("Should be null", cache1.get(fqn)); 126 Assert.assertEquals("value", cache2.get(fqn, "key")); 127 128 cache1.put(fqn, "key2", "value2"); 130 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 131 TestingUtil.sleepThread(500); Assert.assertNull("Should have been invalidated!", cache2.get(fqn)); 133 134 cache1.stop(); 136 cache2.stop(); 137 cache1 = null; 138 cache2 = null; 139 } 140 141 142 public void testPessimisticTransactional() throws Exception 143 { 144 CacheImpl cache1 = createCache(false); 145 CacheImpl cache2 = createCache(false); 146 147 Fqn fqn = Fqn.fromString("/a/b"); 148 cache1.put(fqn, "key", "value"); 149 150 Assert.assertEquals("value", cache1.get(fqn, "key")); 152 Assert.assertNull("Should NOT have replicated!", cache2.get(fqn)); 153 154 log.info("***** Node not replicated, as expected."); 155 156 TransactionManager txm = cache2.getTransactionManager(); 159 Assert.assertEquals("value", cache1.get(fqn, "key")); 160 161 txm.begin(); 162 cache2.put(fqn, "key", "value"); 163 Assert.assertEquals("value", cache2.get(fqn, "key")); 164 txm.commit(); 165 166 Assert.assertNull("Should be null", cache1.get(fqn)); 167 Assert.assertEquals("value", cache2.get(fqn, "key")); 168 169 txm = cache1.getTransactionManager(); 171 Assert.assertEquals("value", cache2.get(fqn, "key")); 172 173 txm.begin(); 174 cache1.put(fqn, "key2", "value2"); 175 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 176 txm.commit(); 177 178 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 179 Assert.assertNull("Should have been invalidated!", cache2.get(fqn)); 180 181 txm = cache2.getTransactionManager(); 183 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 184 185 txm.begin(); 186 cache2.put(fqn, "key", "value"); 187 Assert.assertEquals("value", cache2.get(fqn, "key")); 188 txm.rollback(); 189 190 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 191 Assert.assertNull("Should not have committed", cache2.get(fqn)); 192 193 cache1.stop(); 195 cache2.stop(); 196 cache1 = null; 197 cache2 = null; 198 199 } 200 201 202 public void testOptSyncUnableToEvict() throws Exception 203 { 204 CacheImpl cache1 = createCache(true); 205 CacheImpl cache2 = createCache(true); 206 207 Fqn fqn = Fqn.fromString("/a/b"); 208 209 cache2.put(fqn, "key", "value"); 210 Assert.assertEquals("value", cache2.get(fqn, "key")); 211 Assert.assertNull(cache1.get(fqn)); 212 213 TransactionManager mgr1 = cache1.getTransactionManager(); 215 TransactionManager mgr2 = cache2.getTransactionManager(); 216 217 mgr1.begin(); 218 cache1.put(fqn, "key2", "value2"); 219 Transaction tx1 = mgr1.suspend(); 220 mgr2.begin(); 221 cache2.put(fqn, "key3", "value3"); 222 Transaction tx2 = mgr2.suspend(); 223 mgr1.resume(tx1); 224 try 226 { 227 mgr1.commit(); 228 Assert.assertTrue("Ought to have succeeded!", true); 229 } 230 catch (RollbackException roll) 231 { 232 Assert.assertTrue("Ought to have succeeded!", false); 233 } 234 235 mgr2.resume(tx2); 236 try 237 { 238 mgr2.commit(); 239 Assert.assertTrue("Ought to have failed!", false); 240 } 241 catch (RollbackException roll) 242 { 243 Assert.assertTrue("Ought to have failed!", true); 244 } 245 cache1.stop(); 247 cache2.stop(); 248 cache1 = null; 249 cache2 = null; 250 } 251 252 public void testPessTxSyncUnableToEvict() throws Exception 253 { 254 CacheImpl cache1 = createCache(false); 255 CacheImpl cache2 = createCache(false); 256 257 Fqn fqn = Fqn.fromString("/a/b"); 258 259 cache1.put("/a/b", "key", "value"); 260 Assert.assertEquals("value", cache1.get(fqn, "key")); 261 Assert.assertNull(cache2.get(fqn)); 262 263 TransactionManager mgr1 = cache1.getTransactionManager(); 265 TransactionManager mgr2 = cache2.getTransactionManager(); 266 267 mgr1.begin(); 268 cache1.put(fqn, "key2", "value2"); 269 Transaction tx1 = mgr1.suspend(); 270 mgr2.begin(); 271 cache2.put(fqn, "key3", "value3"); 272 Transaction tx2 = mgr2.suspend(); 273 mgr1.resume(tx1); 274 try 276 { 277 mgr1.commit(); 278 Assert.assertTrue("Ought to have failed!", false); 279 } 280 catch (RollbackException roll) 281 { 282 Assert.assertTrue("Ought to have failed!", true); 283 } 284 285 mgr2.resume(tx2); 286 try 287 { 288 mgr2.commit(); 289 Assert.assertTrue("Ought to have succeeded!", true); 290 } 291 catch (RollbackException roll) 292 { 293 Assert.assertTrue("Ought to have succeeded!", false); 294 } 295 cache1.stop(); 297 cache2.stop(); 298 cache1 = null; 299 cache2 = null; 300 } 301 302 public void testPessTxAsyncUnableToEvict() throws Exception 303 { 304 CacheImpl cache1 = createUnstartedCache(false); 305 CacheImpl cache2 = createUnstartedCache(false); 306 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.INVALIDATION_ASYNC); 307 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.INVALIDATION_ASYNC); 308 cache1.start(); 309 cache2.start(); 310 311 Fqn fqn = Fqn.fromString("/a/b"); 312 313 cache1.put("/a/b", "key", "value"); 314 Assert.assertEquals("value", cache1.get(fqn, "key")); 315 Assert.assertNull(cache2.get(fqn)); 316 317 TransactionManager mgr1 = cache1.getTransactionManager(); 319 TransactionManager mgr2 = cache2.getTransactionManager(); 320 321 mgr1.begin(); 322 cache1.put(fqn, "key2", "value2"); 323 Transaction tx1 = mgr1.suspend(); 324 mgr2.begin(); 325 cache2.put(fqn, "key3", "value3"); 326 Transaction tx2 = mgr2.suspend(); 327 mgr1.resume(tx1); 328 try 330 { 331 mgr1.commit(); 332 Assert.assertTrue("Ought to have succeeded!", true); 333 } 334 catch (RollbackException roll) 335 { 336 Assert.assertTrue("Ought to have succeeded!", false); 337 } 338 339 mgr2.resume(tx2); 340 try 341 { 342 mgr2.commit(); 343 Assert.assertTrue("Ought to have succeeded!", true); 344 } 345 catch (RollbackException roll) 346 { 347 Assert.assertTrue("Ought to have succeeded!", false); 348 } 349 cache1.stop(); 351 cache2.stop(); 352 cache1 = null; 353 cache2 = null; 354 } 355 356 357 public void testOptimistic() throws Exception 358 { 359 CacheImpl cache1 = createCache(true); 360 CacheImpl cache2 = createCache(true); 361 362 Fqn fqn = Fqn.fromString("/a/b"); 363 cache1.put(fqn, "key", "value"); 364 365 Assert.assertEquals("value", cache1.get(fqn, "key")); 367 Assert.assertNull("Should NOT have replicated!", cache2.get(fqn)); 368 369 log.info("***** Node not replicated, as expected."); 370 371 cache2.put(fqn, "key", "value"); 373 Assert.assertNull("Should be null", cache1.get(fqn)); 374 Assert.assertEquals("value", cache2.get(fqn, "key")); 375 376 cache1.put(fqn, "key2", "value2"); 378 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 379 Assert.assertNull("Should have been invalidated!", cache2.get(fqn)); 380 381 TransactionManager txm = cache2.getTransactionManager(); 383 384 txm.begin(); 385 cache2.put(fqn, "key", "value"); 386 Assert.assertEquals("value", cache2.get(fqn, "key")); 387 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 388 txm.commit(); 389 390 Assert.assertNull("Should be null", cache1.get(fqn)); 391 Assert.assertEquals("value", cache2.get(fqn, "key")); 392 393 txm = cache1.getTransactionManager(); 395 396 txm.begin(); 397 cache1.put(fqn, "key2", "value2"); 398 Assert.assertEquals("value", cache2.get(fqn, "key")); 399 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 400 txm.commit(); 401 402 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 403 Assert.assertNull("Should have been invalidated!", cache2.get(fqn)); 404 405 txm = cache2.getTransactionManager(); 407 408 txm.begin(); 409 cache2.put(fqn, "key", "value"); 410 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 411 Assert.assertEquals("value", cache2.get(fqn, "key")); 412 txm.rollback(); 413 414 Assert.assertEquals("value2", cache1.get(fqn, "key2")); 415 Assert.assertNull("Should not have committed", cache2.get(fqn)); 416 417 cache1.stop(); 419 cache2.stop(); 420 cache1 = null; 421 cache2 = null; 422 423 } 424 425 public void testPessimisticNonTransactionalWithCacheLoader() throws Exception 426 { 427 CacheImpl[] caches = createCachesWithSharedCL(false); 428 429 Fqn fqn = Fqn.fromString("/a/b"); 430 caches[0].put(fqn, "key", "value"); 431 432 Assert.assertEquals("value", caches[0].get(fqn, "key")); 433 Assert.assertEquals("value", caches[1].get(fqn, "key")); 434 435 caches[1].put(fqn, "key", "value"); 437 Assert.assertEquals("value", caches[1].get(fqn, "key")); 438 Assert.assertEquals("value", caches[0].get(fqn, "key")); 439 440 caches[0].put(fqn, "key2", "value2"); 442 Assert.assertEquals("value2", caches[0].get(fqn, "key2")); 443 Assert.assertEquals("value2", caches[1].get(fqn, "key2")); 444 Assert.assertEquals("value", caches[0].get(fqn, "key")); 445 Assert.assertEquals("value", caches[1].get(fqn, "key")); 446 447 caches[0].remove(fqn); 449 caches[1].remove(fqn); 450 caches[0].stop(); 451 caches[1].stop(); 452 caches[0] = null; 453 caches[1] = null; 454 } 455 456 public void testPessimisticTransactionalWithCacheLoader() throws Exception 457 { 458 CacheImpl[] caches = createCachesWithSharedCL(false); 459 460 Fqn fqn = Fqn.fromString("/a/b"); 461 TransactionManager mgr = caches[0].getTransactionManager(); 462 Assert.assertNull("Should be null", caches[0].get(fqn, "key")); 463 Assert.assertNull("Should be null", caches[1].get(fqn, "key")); 464 mgr.begin(); 465 caches[0].put(fqn, "key", "value"); 466 Assert.assertEquals("value", caches[0].get(fqn, "key")); 467 mgr.commit(); 468 Assert.assertEquals("value", caches[1].get(fqn, "key")); 469 Assert.assertEquals("value", caches[0].get(fqn, "key")); 470 471 mgr.begin(); 472 caches[0].put(fqn, "key2", "value2"); 473 Assert.assertEquals("value2", caches[0].get(fqn, "key2")); 474 mgr.rollback(); 475 Assert.assertEquals("value", caches[1].get(fqn, "key")); 476 Assert.assertEquals("value", caches[0].get(fqn, "key")); 477 Assert.assertNull("Should be null", caches[0].get(fqn, "key2")); 478 Assert.assertNull("Should be null", caches[1].get(fqn, "key2")); 479 480 caches[0].remove(fqn); 482 caches[1].remove(fqn); 483 caches[0].stop(); 484 caches[1].stop(); 485 caches[0] = null; 486 caches[1] = null; 487 } 488 489 public void testOptimisticWithCacheLoader() throws Exception 490 { 491 CacheImpl[] caches = createCachesWithSharedCL(true); 492 493 Fqn fqn = Fqn.fromString("/a/b"); 494 TransactionManager mgr = caches[0].getTransactionManager(); 495 Assert.assertNull("Should be null", caches[0].get(fqn, "key")); 496 Assert.assertNull("Should be null", caches[1].get(fqn, "key")); 497 mgr.begin(); 498 caches[0].put(fqn, "key", "value"); 499 Assert.assertEquals("value", caches[0].get(fqn, "key")); 500 Assert.assertNull("Should be null", caches[1].get(fqn, "key")); 501 mgr.commit(); 502 Assert.assertEquals("value", caches[1].get(fqn, "key")); 503 Assert.assertEquals("value", caches[0].get(fqn, "key")); 504 505 mgr.begin(); 506 caches[0].put(fqn, "key2", "value2"); 507 Assert.assertEquals("value2", caches[0].get(fqn, "key2")); 508 Assert.assertNull("Should be null", caches[1].get(fqn, "key2")); 509 mgr.rollback(); 510 Assert.assertEquals("value", caches[1].get(fqn, "key")); 511 Assert.assertEquals("value", caches[0].get(fqn, "key")); 512 Assert.assertNull("Should be null", caches[0].get(fqn, "key2")); 513 Assert.assertNull("Should be null", caches[1].get(fqn, "key2")); 514 515 caches[0].remove(fqn); 517 caches[1].remove(fqn); 518 caches[0].stop(); 519 caches[1].stop(); 520 caches[0] = null; 521 caches[1] = null; 522 } 523 524 public void testInvalidationWithRegionBasedMarshalling() throws Exception 525 { 526 doRegionBasedTest(false); 527 } 528 529 public void testInvalidationWithRegionBasedMarshallingOptimistic() throws Exception 530 { 531 doRegionBasedTest(true); 532 } 533 534 protected void doRegionBasedTest(boolean optimistic) throws Exception 535 { 536 CacheImpl[] caches = new CacheImpl[2]; 537 caches[0] = createUnstartedCache(false); 538 caches[1] = createUnstartedCache(false); 539 540 caches[0].getConfiguration().setUseRegionBasedMarshalling(true); 541 caches[1].getConfiguration().setUseRegionBasedMarshalling(true); 542 543 if (optimistic) 544 { 545 caches[0].getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 546 caches[1].getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 547 } 548 549 caches[0].start(); 550 caches[1].start(); 551 552 TestingUtil.blockUntilViewsReceived(caches, 5000); 553 554 Fqn fqn = Fqn.fromString("/a/b"); 555 556 assertNull("Should be null", caches[0].get(fqn)); 557 assertNull("Should be null", caches[1].get(fqn)); 558 559 caches[0].put(fqn, "key", "value"); 560 assertEquals("expecting value", "value", caches[0].get(fqn, "key")); 561 assertNull("Should be null", caches[1].get(fqn)); 562 563 caches[1].put(fqn, "key", "value2"); 565 assertEquals("expecting value2", "value2", caches[1].get(fqn, "key")); 566 assertNull("Should be null", caches[0].get(fqn)); 567 568 caches[0].remove(fqn); 570 caches[1].remove(fqn); 571 caches[0].stop(); 572 caches[1].stop(); 573 caches[0] = null; 574 caches[1] = null; 575 } 576 577 protected CacheImpl createUnstartedCache(boolean optimistic) throws Exception 578 { 579 CacheImpl cache = new CacheImpl(); 580 cache.getConfiguration().setClusterName("MyCluster"); 581 cache.getConfiguration().setInitialStateRetrievalTimeout(3000); 582 cache.getConfiguration().setCacheMode(Configuration.CacheMode.INVALIDATION_SYNC); 583 if (optimistic) cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 584 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 585 return cache; 586 } 587 588 protected CacheImpl createCache(boolean optimistic) throws Exception 589 { 590 CacheImpl cache = createUnstartedCache(optimistic); 591 cache.start(); 592 return cache; 593 } 594 595 protected CacheImpl[] createCachesWithSharedCL(boolean optimistic) throws Exception 596 { 597 CacheImpl[] caches = new CacheImpl[2]; 598 caches[0] = createUnstartedCache(optimistic); 599 caches[1] = createUnstartedCache(optimistic); 600 601 caches[0].getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig()); 602 caches[1].getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig()); 603 604 caches[0].start(); 605 caches[1].start(); 606 return caches; 607 } 608 609 610 protected CacheLoaderConfig getCacheLoaderConfig() throws Exception 611 { 612 String xml = " <config>\n" + 613 " \n" + 614 " <passivation>false</passivation>\n" + 615 " <preload></preload>\n" + 616 "\n" + 617 " <cacheloader>\n" + 618 " <class>org.jboss.cache.loader.FileCacheLoader</class>\n" + 619 " <properties>\n" + 620 " location=" + getTempDir() + "\n" + 621 " </properties>\n" + 622 " <async>false</async>\n" + 623 " <fetchPersistentState>false</fetchPersistentState>\n" + 624 " <ignoreModifications>false</ignoreModifications>\n" + 625 " </cacheloader>\n" + 626 " \n" + 627 " </config>"; 628 Element element = XmlHelper.stringToElement(xml); 629 return XmlConfigurationParser.parseCacheLoaderConfig(element); 630 } 631 632 protected String getTempDir() 633 { 634 String name = "cacheloader"; 635 String tempDir = System.getProperty("java.io.tmpdir", "/tmp") + File.separator + "JBossCache-InvalidationInterceptorTestCase-WorkingDir" + File.separator + name; 636 File tempDirFile = new File (tempDir); 637 if (!tempDirFile.exists()) 638 { 639 tempDirFile.mkdirs(); 640 } 641 return tempDir; 642 } 643 644 public static void main(String [] args) 645 { 646 TestRunner.run(suite()); 647 } 648 649 public static Test suite() 650 { 651 return new TestSuite(InvalidationInterceptorTest.class); 652 } 653 } 654 | Popular Tags |