我有下面的单元测试,它使用net.spy.memcached.MemcachedClient
@Test
public void testCASFailsWrite() throws Exception {
Integer begin = new Integer(3);
client.set(key, 0, begin);
CASValue<Object> casValue = client.gets(key);
Assert.assertNotNull(casValue);
Assert.assertTrue(casValue.getValue() instanceof Integer);
Integer fromCache = (Integer) casValue.getValue();
Integer nextSeq = new Integer(fromCache + 9);
long myInvalidValue = casValue.getCas() - 1;
CASResponse response = client.cas(key, myInvalidValue, nextSeq);
Assert.assertEquals(CASResponse.EXISTS, response);
}我希望CASResponse会显示该值已经存在,因为我有一个无效的casValue,但是在最后一次断言时,它返回为OK。我希望确保我的密钥句柄跨多个JVM并发更新。我的单元测试是通过com.thimbleware.jmemcached.AbstractCache实现使用嵌入式memcache进程。当我的键已经在memcache中,而我没有相同的casValue()时,我能依靠CASResponse来显示存在吗?
发布于 2013-04-13 08:25:47
这很可能是因为您正在对一个全新的memcached实例运行此测试。Cas值在memcached实例中是全局的,第一个集合接收cas值1,第二个集合获取cas值2,依此类推。当您运行测试时,您的键获得的CAS值为1。因为您的无效cas是通过递减此键的CAS值而生成的,所以您的无效CAS值最终为0。在更新中指定cas值为0意味着忽略cas而仅更新。将myInvalidValue设置为像123456这样的随机数,这个问题就会消失。另一件有趣的事情是,如果你在上面两次运行你的测试,那么第二次就会通过。
https://stackoverflow.com/questions/15957901
复制相似问题