use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.
the class ConcurrentWriteTest method testManyUsers.
// Ignoring the test as it's more of a stress-test: this should be enabled manually
@Ignore
@Test
public void testManyUsers() throws Throwable {
try {
// setup - create users
for (int i = 0; i < USER_COUNT; i++) {
Customer customer = createCustomer(0);
getCustomerIDs().add(customer.getId());
}
assertEquals("failed to create enough Customers", USER_COUNT, getCustomerIDs().size());
final ExecutorService executor = Executors.newFixedThreadPool(USER_COUNT);
CyclicBarrier barrier = new CyclicBarrier(USER_COUNT + 1);
List<Future<Void>> futures = new ArrayList<Future<Void>>(USER_COUNT);
for (Integer customerId : getCustomerIDs()) {
Future<Void> future = executor.submit(new UserRunner(customerId, barrier));
futures.add(future);
// rampup
Thread.sleep(LAUNCH_INTERVAL_MILLIS);
}
// wait for all threads to finish
barrier.await(2, TimeUnit.MINUTES);
log.info("All threads finished, let's shutdown the executor and check whether any exceptions were reported");
for (Future<Void> future : futures) {
future.get();
}
executor.shutdown();
log.info("All future gets checked");
} catch (Throwable t) {
log.error("Error running test", t);
throw t;
}
}
use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.
the class ConcurrentWriteTest method getFirstContact.
/**
* -load existing Customer -get customer's contacts; return 1st one
*
* @param customerId
* @return first Contact or null if customer has none
*/
private Contact getFirstContact(Integer customerId) throws Exception {
assert customerId != null;
return withTxSessionApply(s -> {
Customer customer = s.load(Customer.class, customerId);
Set<Contact> contacts = customer.getContacts();
Contact firstContact = contacts.isEmpty() ? null : contacts.iterator().next();
if (TERMINATE_ALL_USERS) {
markRollbackOnly(s);
}
return firstContact;
});
}
use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.
the class ConcurrentWriteTest method testSingleUser.
@Test
public void testSingleUser() throws Exception {
// setup
sessionFactory().getStatistics().clear();
// wait a while to make sure that timestamp comparison works after invalidateRegion
TIME_SERVICE.advance(1);
Customer customer = createCustomer(0);
final Integer customerId = customer.getId();
getCustomerIDs().add(customerId);
// wait a while to make sure that timestamp comparison works after collection remove (during insert)
TIME_SERVICE.advance(1);
assertNull("contact exists despite not being added", getFirstContact(customerId));
// check that cache was hit
SecondLevelCacheStatistics customerSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Customer.class.getName());
assertEquals(1, customerSlcs.getPutCount());
assertEquals(1, customerSlcs.getElementCountInMemory());
assertEquals(1, customerSlcs.getEntries().size());
log.infof("Add contact to customer {0}", customerId);
SecondLevelCacheStatistics contactsCollectionSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Customer.class.getName() + ".contacts");
assertEquals(1, contactsCollectionSlcs.getPutCount());
assertEquals(1, contactsCollectionSlcs.getElementCountInMemory());
assertEquals(1, contactsCollectionSlcs.getEntries().size());
final Contact contact = addContact(customerId);
assertNotNull("contact returned by addContact is null", contact);
assertEquals("Customer.contacts cache was not invalidated after addContact", 0, contactsCollectionSlcs.getElementCountInMemory());
assertNotNull("Contact missing after successful add call", getFirstContact(customerId));
// read everyone's contacts
readEveryonesFirstContact();
removeContact(customerId);
assertNull("contact still exists after successful remove call", getFirstContact(customerId));
}
use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.
the class EntityCollectionInvalidationTest method testConcurrentLoadAndRemoval.
@TestForIssue(jiraKey = "HHH-9881")
@Test
public void testConcurrentLoadAndRemoval() throws Exception {
if (!remoteCustomerCache.getCacheConfiguration().clustering().cacheMode().isInvalidation()) {
// This test is tailored for invalidation-based strategies, using pending puts cache
return;
}
AtomicReference<Exception> getException = new AtomicReference<>();
AtomicReference<Exception> deleteException = new AtomicReference<>();
Phaser getPhaser = new Phaser(2);
HookInterceptor hookInterceptor = new HookInterceptor(getException);
AdvancedCache remotePPCache = remoteCustomerCache.getCacheManager().getCache(remoteCustomerCache.getName() + "-" + InfinispanRegionFactory.DEF_PENDING_PUTS_RESOURCE).getAdvancedCache();
remotePPCache.getAdvancedCache().addInterceptor(hookInterceptor, 0);
IdContainer idContainer = new IdContainer();
withTxSession(localFactory, s -> {
Customer customer = new Customer();
customer.setName("JBoss");
s.persist(customer);
idContainer.customerId = customer.getId();
});
// start loading
Thread getThread = new Thread(() -> {
try {
withTxSession(remoteFactory, s -> {
s.get(Customer.class, idContainer.customerId);
});
} catch (Exception e) {
log.error("Failure to get customer", e);
getException.set(e);
}
}, "get-thread");
Thread deleteThread = new Thread(() -> {
try {
withTxSession(localFactory, s -> {
Customer customer = s.get(Customer.class, idContainer.customerId);
s.delete(customer);
});
} catch (Exception e) {
log.error("Failure to delete customer", e);
deleteException.set(e);
}
}, "delete-thread");
// get thread should block on the beginning of PutFromLoadValidator#acquirePutFromLoadLock
hookInterceptor.block(getPhaser, getThread);
getThread.start();
arriveAndAwait(getPhaser);
deleteThread.start();
deleteThread.join();
hookInterceptor.unblock();
arriveAndAwait(getPhaser);
getThread.join();
if (getException.get() != null) {
throw new IllegalStateException("get-thread failed", getException.get());
}
if (deleteException.get() != null) {
throw new IllegalStateException("delete-thread failed", deleteException.get());
}
Customer localCustomer = getCustomer(idContainer.customerId, localFactory);
assertNull(localCustomer);
Customer remoteCustomer = getCustomer(idContainer.customerId, remoteFactory);
assertNull(remoteCustomer);
assertTrue(remoteCustomerCache.isEmpty());
}
use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.
the class EntityCollectionInvalidationTest method modifyCustomer.
private IdContainer modifyCustomer(Integer id, SessionFactory sessionFactory) throws Exception {
log.debug("Modify customer with id=" + id);
return withTxSessionApply(sessionFactory, session -> {
IdContainer ids = new IdContainer();
Set contactIds = new HashSet();
Customer customer = doGetCustomer(id, session);
customer.setName("NewJBoss");
ids.customerId = customer.getId();
Set<Contact> contacts = customer.getContacts();
for (Contact c : contacts) {
contactIds.add(c.getId());
}
Contact contact = contacts.iterator().next();
contacts.remove(contact);
contactIds.remove(contact.getId());
ids.contactIds = contactIds;
contact.setCustomer(null);
session.save(customer);
return ids;
});
}
Aggregations