I would like to test my JDO – DAO with multiple threads in order to verify if the persistence layer is aware of synchronization.
Here is my piece of code in a JUnit test
for(int i=0;i<10;i++) {
Runnable runnable = new UserAccountLifeCycle(..);
Thread thread = new Thread(runnable);
thread.start();
}
.. where UserAccountLifeCycle is a class that extends another business layer class and implements “Runnable” with this code:
@Override
public void run() {
CategoryBusiness catBusiness = new CategoryBusiness(..);
Category cat1 = catBusiness.createEntity(..);
catBusiness.createEntities(..);
ContentBusiness conBusiness = new ContentBusiness(..);
conBusiness.createEntities(..);
}
If I run the test I get:
Exception in thread “Thread-6” java.lang.NullPointerException: No API
environment is registered for this thread. at
com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:86)
at
com.google.appengine.api.datastore.DatastoreAttributes.(DatastoreAttributes.java:28)
at
com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.getDatastoreAttributes(AsyncDatastoreServiceImpl.java:952)
at
com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.getDatastoreType(AsyncDatastoreServiceImpl.java:944)
at
com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.get(AsyncDatastoreServiceImpl.java:267)
at
com.google.appengine.api.datastore.DatastoreServiceImpl$1.runInternal(DatastoreServiceImpl.java:78)
at
com.google.appengine.api.datastore.DatastoreServiceImpl$1.runInternal(DatastoreServiceImpl.java:75)
at
com.google.appengine.api.datastore.TransactionRunner.runInTransaction(TransactionRunner.java:31)
at
com.google.appengine.api.datastore.DatastoreServiceImpl.get(DatastoreServiceImpl.java:75)
at
com.google.appengine.api.datastore.DatastoreServiceImpl.get(DatastoreServiceImpl.java:62)
at
org.datanucleus.store.appengine.RuntimeExceptionWrappingDatastoreService.get(RuntimeExceptionWrappingDatastoreService.java:56)
at
org.datanucleus.store.appengine.DatastorePersistenceHandler.get(DatastorePersistenceHandler.java:100)
at
org.datanucleus.store.appengine.DatastorePersistenceHandler.get(DatastorePersistenceHandler.java:112)
at
org.datanucleus.store.appengine.DatastorePersistenceHandler.fetchObject(DatastorePersistenceHandler.java:517)
at
org.datanucleus.state.JDOStateManagerImpl.validate(JDOStateManagerImpl.java:4263)
at
org.datanucleus.ObjectManagerImpl.findObject(ObjectManagerImpl.java:2444)
at
org.datanucleus.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1671)
at
org.datanucleus.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1767)
at
org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManager.getObjectById(DatastoreJDOPersistenceManager.java:73)
at
it.icaroproject.main.persistence.dao.GaedatastoreUserAccountDAO.addCategories(GaedatastoreUserAccountDAO.java:174)
at
it.icaroproject.main.business.CategoryBusiness.createEntities(CategoryBusiness.java:83)
at
it.icaroproject.main.business.CategoryBusiness.createEntity(CategoryBusiness.java:76)
at
it.icaroproject.test.business.UserAccountLifeCycle.run(UserAccountLifeCycle.java:52)
at java.lang.Thread.run(Thread.java:680)
If I run this code works:
for(int i=0;i<10;i++) {
UserAccountLifeCycle lifeCycle = new UserAccountLifeCycle(i,testUser.getKey());
lifeCycle.run();
}
(I know it doesn’t run the method as a thread, but it is intended as the proof that JUnit test is configured correctly with GAE local testing)
Although the App Engine Java runtime supports threads, the dev_appserver doesn’t. Results from testing concurrency on the local dev environment are thus not going to be representative of the production environment.