Search in sources :

Example 1 with TestRegion

use of org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess.TestRegion in project infinispan by infinispan.

the class AbstractGeneralDataRegionTest method testEvict.

@Test
public void testEvict() throws Exception {
    Assume.assumeFalse("Per-key eviction is not supported in 5.3", org.hibernate.Version.getVersionString().startsWith("5.3"));
    withSessionFactoriesAndRegions(2, ((sessionFactories, regions) -> {
        InfinispanBaseRegion localRegion = regions.get(0);
        TestRegion testLocalRegion = TEST_SESSION_ACCESS.fromRegion(localRegion);
        InfinispanBaseRegion remoteRegion = regions.get(1);
        TestRegion testRemoteRegion = TEST_SESSION_ACCESS.fromRegion(remoteRegion);
        Object localSession = sessionFactories.get(0).openSession();
        Object remoteSession = sessionFactories.get(1).openSession();
        AdvancedCache<?, ?> localCache = localRegion.getCache();
        AdvancedCache<?, ?> remoteCache = remoteRegion.getCache();
        try {
            assertNull("local is clean", testLocalRegion.get(localSession, KEY));
            assertNull("remote is clean", testRemoteRegion.get(remoteSession, KEY));
            // If this node is backup owner, it will see the update once as originator and then when getting the value from primary
            boolean isLocalNodeBackupOwner = extractCacheTopology(localCache).getDistribution(KEY).writeOwners().indexOf(localCache.getCacheManager().getAddress()) > 0;
            CountDownLatch insertLatch = new CountDownLatch(isLocalNodeBackupOwner ? 3 : 2);
            ExpectingInterceptor.get(localCache).when((ctx, cmd) -> cmd instanceof PutKeyValueCommand).countDown(insertLatch);
            ExpectingInterceptor.get(remoteCache).when((ctx, cmd) -> cmd instanceof PutKeyValueCommand).countDown(insertLatch);
            Transaction tx = ((SharedSessionContract) localSession).getTransaction();
            tx.begin();
            try {
                testLocalRegion.put(localSession, KEY, VALUE1);
                tx.commit();
            } catch (Exception e) {
                tx.rollback();
                throw e;
            }
            assertTrue(insertLatch.await(2, TimeUnit.SECONDS));
            assertEquals(VALUE1, testLocalRegion.get(localSession, KEY));
            assertEquals(VALUE1, testRemoteRegion.get(remoteSession, KEY));
            CountDownLatch removeLatch = new CountDownLatch(isLocalNodeBackupOwner ? 3 : 2);
            ExpectingInterceptor.get(localCache).when((ctx, cmd) -> cmd instanceof RemoveCommand).countDown(removeLatch);
            ExpectingInterceptor.get(remoteCache).when((ctx, cmd) -> cmd instanceof RemoveCommand).countDown(removeLatch);
            regionEvict(localRegion);
            assertTrue(removeLatch.await(2, TimeUnit.SECONDS));
            assertEquals(null, testLocalRegion.get(localSession, KEY));
            assertEquals(null, testRemoteRegion.get(remoteSession, KEY));
        } finally {
            ((Session) localSession).close();
            ((Session) remoteSession).close();
            ExpectingInterceptor.cleanup(localCache, remoteCache);
        }
    }));
}
Also used : Arrays(java.util.Arrays) AvailableSettings(org.hibernate.cfg.AvailableSettings) TestSessionAccess(org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess) Session(org.hibernate.Session) BatchModeJtaPlatform(org.infinispan.test.hibernate.cache.commons.util.BatchModeJtaPlatform) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) TestRegionFactoryProvider(org.infinispan.test.hibernate.cache.commons.util.TestRegionFactoryProvider) QueryResultsRegion(org.hibernate.cache.spi.QueryResultsRegion) ArrayList(java.util.ArrayList) Transaction(org.hibernate.Transaction) AdvancedCache(org.infinispan.AdvancedCache) CacheTestUtil(org.infinispan.test.hibernate.cache.commons.util.CacheTestUtil) RegionFactory(org.hibernate.cache.spi.RegionFactory) TestingUtil(org.infinispan.test.TestingUtil) AccessType(org.hibernate.cache.spi.access.AccessType) Assume(org.junit.Assume) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) RemoveCommand(org.infinispan.commands.write.RemoveCommand) Properties(java.util.Properties) NoJtaPlatform(org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform) SessionFactory(org.hibernate.SessionFactory) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) Collectors(java.util.stream.Collectors) TestRegion(org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess.TestRegion) MetadataSources(org.hibernate.boot.MetadataSources) TimeUnit(java.util.concurrent.TimeUnit) InfinispanBaseRegion(org.infinispan.hibernate.cache.commons.InfinispanBaseRegion) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CacheMode(org.infinispan.configuration.cache.CacheMode) TestingUtil.extractCacheTopology(org.infinispan.test.TestingUtil.extractCacheTopology) Assert.assertNull(org.junit.Assert.assertNull) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) ExpectingInterceptor(org.infinispan.test.hibernate.cache.commons.util.ExpectingInterceptor) SharedSessionContract(org.hibernate.SharedSessionContract) TestRegionFactory(org.infinispan.test.hibernate.cache.commons.util.TestRegionFactory) Assert.assertEquals(org.junit.Assert.assertEquals) InfinispanBaseRegion(org.infinispan.hibernate.cache.commons.InfinispanBaseRegion) RemoveCommand(org.infinispan.commands.write.RemoveCommand) Transaction(org.hibernate.Transaction) TestRegion(org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess.TestRegion) AdvancedCache(org.infinispan.AdvancedCache) CountDownLatch(java.util.concurrent.CountDownLatch) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) Test(org.junit.Test)

Example 2 with TestRegion

use of org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess.TestRegion in project infinispan by infinispan.

the class AbstractGeneralDataRegionTest method testEvictAll.

/**
 * Test method for {@link QueryResultsRegion#evictAll()}.
 * <p/>
 * FIXME add testing of the "immediately without regard for transaction isolation" bit in the
 * CollectionRegionAccessStrategy API.
 */
public void testEvictAll() throws Exception {
    withSessionFactoriesAndRegions(2, (sessionFactories, regions) -> {
        InfinispanBaseRegion localRegion = regions.get(0);
        TestRegion testLocalRegion = TEST_SESSION_ACCESS.fromRegion(localRegion);
        InfinispanBaseRegion remoteRegion = regions.get(1);
        TestRegion testRemoteRegion = TEST_SESSION_ACCESS.fromRegion(remoteRegion);
        AdvancedCache localCache = localRegion.getCache();
        AdvancedCache remoteCache = remoteRegion.getCache();
        Object localSession = sessionFactories.get(0).openSession();
        Object remoteSession = sessionFactories.get(1).openSession();
        try {
            Set localKeys = localCache.keySet();
            assertEquals("No valid children in " + localKeys, 0, localKeys.size());
            Set remoteKeys = remoteCache.keySet();
            assertEquals("No valid children in " + remoteKeys, 0, remoteKeys.size());
            assertNull("local is clean", testLocalRegion.get(null, KEY));
            assertNull("remote is clean", testRemoteRegion.get(null, KEY));
            testLocalRegion.put(localSession, KEY, VALUE1);
            assertEquals(VALUE1, testLocalRegion.get(null, KEY));
            testRemoteRegion.put(remoteSession, KEY, VALUE1);
            assertEquals(VALUE1, testRemoteRegion.get(null, KEY));
            testLocalRegion.evictAll();
            // This should re-establish the region root node in the optimistic case
            assertNull(testLocalRegion.get(null, KEY));
            localKeys = localCache.keySet();
            assertEquals("No valid children in " + localKeys, 0, localKeys.size());
            // Re-establishing the region root on the local node doesn't
            // propagate it to other nodes. Do a get on the remote node to re-establish
            // This only adds a node in the case of optimistic locking
            assertEquals(null, testRemoteRegion.get(null, KEY));
            remoteKeys = remoteCache.keySet();
            assertEquals("No valid children in " + remoteKeys, 0, remoteKeys.size());
            assertEquals("local is clean", null, testLocalRegion.get(null, KEY));
            assertEquals("remote is clean", null, testRemoteRegion.get(null, KEY));
        } finally {
            ((Session) localSession).close();
            ((Session) remoteSession).close();
        }
    });
}
Also used : InfinispanBaseRegion(org.infinispan.hibernate.cache.commons.InfinispanBaseRegion) Set(java.util.Set) TestRegion(org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess.TestRegion) AdvancedCache(org.infinispan.AdvancedCache) Session(org.hibernate.Session)

Aggregations

Set (java.util.Set)2 Session (org.hibernate.Session)2 AdvancedCache (org.infinispan.AdvancedCache)2 InfinispanBaseRegion (org.infinispan.hibernate.cache.commons.InfinispanBaseRegion)2 TestRegion (org.infinispan.test.hibernate.cache.commons.util.TestSessionAccess.TestRegion)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Properties (java.util.Properties)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 SessionFactory (org.hibernate.SessionFactory)1 SharedSessionContract (org.hibernate.SharedSessionContract)1 Transaction (org.hibernate.Transaction)1 MetadataSources (org.hibernate.boot.MetadataSources)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 QueryResultsRegion (org.hibernate.cache.spi.QueryResultsRegion)1 RegionFactory (org.hibernate.cache.spi.RegionFactory)1