【发布时间】:2010-04-25 02:56:40
【问题描述】:
你们能告诉我一种在 Google App Engine 上模拟对 memcache 的并发访问的方法吗?我正在尝试使用 LocalServiceTestHelpers 和线程,但没有任何运气。每次我尝试在一个线程中访问 Memcache 时,都会收到此错误:
ApiProxy$CallNotFoundException: The API package 'memcache' or call 'Increment()' was not found
我猜是GAE SDK的测试库试图模仿真实环境,因此只为一个线程(运行测试的线程)设置环境,其他线程看不到。
这是一段可以重现问题的代码
package org.seamoo.cache.memcacheImpl;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.appengine.api.memcache.MemcacheService;
import com.google.appengine.api.memcache.MemcacheServiceFactory;
import com.google.appengine.tools.development.testing.LocalMemcacheServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
public class MemcacheTest {
LocalServiceTestHelper helper;
public MemcacheTest() {
LocalMemcacheServiceTestConfig memcacheConfig = new LocalMemcacheServiceTestConfig();
helper = new LocalServiceTestHelper(memcacheConfig);
}
/**
*
*/
@BeforeMethod
public void setUp() {
helper.setUp();
}
/**
* @see LocalServiceTest#tearDown()
*/
@AfterMethod
public void tearDown() {
helper.tearDown();
}
@Test
public void memcacheConcurrentAccess() throws InterruptedException {
final MemcacheService service = MemcacheServiceFactory.getMemcacheService();
Runnable runner = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
service.increment("test-key", 1L, 1L);
try {
Thread.sleep(200L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
service.increment("test-key", 1L, 1L);
}
};
Thread t1 = new Thread(runner);
Thread t2 = new Thread(runner);
t1.start();
t2.start();
while (t1.isAlive()) {
Thread.sleep(100L);
}
Assert.assertEquals((Long) (service.get("test-key")), new Long(4L));
}
}
【问题讨论】:
标签: google-app-engine memcached