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);
}
}));
}
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();
}
});
}
Aggregations