使用线程测试GAE数据存储区

时间:2012-04-09 02:15:54

标签: java google-app-engine google-cloud-datastore jdo google-eclipse-plugin

我想用多线程测试我的JDO - DAO,以验证持久层是否知道同步。

这是我在JUnit测试中的一段代码

for(int i=0;i<10;i++) {
    Runnable runnable = new UserAccountLifeCycle(..);
    Thread thread = new Thread(runnable);
    thread.start();
}

..其中UserAccountLifeCycle是一个扩展另一个业务层类的类,而使用以下代码实现“Runnable”

@Override
public void run() {
    CategoryBusiness catBusiness = new CategoryBusiness(..);
    Category cat1 = catBusiness.createEntity(..);
    catBusiness.createEntities(..);
    ContentBusiness conBusiness = new ContentBusiness(..);
    conBusiness.createEntities(..);
}

如果我参加测试,我会得到:

  

线程“Thread-6”中的异常java.lang.NullPointerException:无API   环境已注册此线程。在   com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:86)     在   com.google.appengine.api.datastore.DatastoreAttributes。(DatastoreAttributes.java:28)     在   com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.getDatastoreAttributes(AsyncDatastoreServiceImpl.java:952)     在   com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.getDatastoreType(AsyncDatastoreServiceImpl.java:944)     在   com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.get(AsyncDatastoreServiceImpl.java:267)     在   com.google.appengine.api.datastore.DatastoreServiceImpl $ 1.runInternal(DatastoreServiceImpl.java:78)     在   com.google.appengine.api.datastore.DatastoreServiceImpl $ 1.runInternal(DatastoreServiceImpl.java:75)     在   com.google.appengine.api.datastore.TransactionRunner.runInTransaction(TransactionRunner.java:31)     在   com.google.appengine.api.datastore.DatastoreServiceImpl.get(DatastoreServiceImpl.java:75)     在   com.google.appengine.api.datastore.DatastoreServiceImpl.get(DatastoreServiceImpl.java:62)     在   org.datanucleus.store.appengine.RuntimeExceptionWrappingDatastoreService.get(RuntimeExceptionWrappingDatastoreService.java:56)     在   org.datanucleus.store.appengine.DatastorePersistenceHandler.get(DatastorePersistenceHandler.java:100)     在   org.datanucleus.store.appengine.DatastorePersistenceHandler.get(DatastorePersistenceHandler.java:112)     在   org.datanucleus.store.appengine.DatastorePersistenceHandler.fetchObject(DatastorePersistenceHandler.java:517)     在   org.datanucleus.state.JDOStateManagerImpl.validate(JDOStateManagerImpl.java:4263)     在   org.datanucleus.ObjectManagerImpl.findObject(ObjectManagerImpl.java:2444)     在   org.datanucleus.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1671)     在   org.datanucleus.jdo.JDOPersistenceManager.getObjectById(JDOPersistenceManager.java:1767)     在   org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManager.getObjectById(DatastoreJDOPersistenceManager.java:73)     在   it.icaroproject.main.persistence.dao.GaedatastoreUserAccountDAO.addCategories(GaedatastoreUserAccountDAO.java:174)     在   it.icaroproject.main.business.CategoryBusiness.createEntities(CategoryBusiness.java:83)     在   it.icaroproject.main.business.CategoryBusiness.createEntity(CategoryBusiness.java:76)     在   it.icaroproject.test.business.UserAccountLifeCycle.run(UserAccountLifeCycle.java:52)     在java.lang.Thread.run(Thread.java:680)

如果我运行此代码有效:

for(int i=0;i<10;i++) {
    UserAccountLifeCycle lifeCycle = new UserAccountLifeCycle(i,testUser.getKey());
    lifeCycle.run();
}

(我知道它不会将该方法作为线程运行,但它的目的是证明JUnit测试已正确配置GAE local testing

2 个答案:

答案 0 :(得分:2)

虽然App Engine Java运行时支持线程,但dev_appserver不支持。因此,在本地开发环境中测试并发性的结果不能代表生产环境。

答案 1 :(得分:0)

您可以尝试在新线程中建议的解决方案:

Acceptance testing preloading of data into GAE dev server datastore