gpt4 book ai didi

google-app-engine - 谷歌应用引擎 : Unit testing concurrent access to memcache

转载 作者:太空宇宙 更新时间:2023-11-03 15:27:24 26 4
gpt4 key购买 nike

你们能告诉我一种在 Google App Engine 上模拟并发访问内存缓存的方法吗?我正在尝试使用 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));
}
}

最佳答案

您是要测试您的应用程序还是 App Engine 的内存缓存实现?并发读取和写入下的内存缓存语义很好理解 - 您最好模拟可能发生的情况,以验证您的应用程序是否能够很好地处理这些情况。

关于google-app-engine - 谷歌应用引擎 : Unit testing concurrent access to memcache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2706975/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com