#14 开始整理Modules的单元测试,提高测试覆盖率,并顺手改进代码。

This commit is contained in:
Calvin
2012-05-05 23:39:31 +08:00
parent da2440535c
commit e819fbcefd
50 changed files with 271 additions and 451 deletions
@@ -66,9 +66,9 @@ public class SpyMemcachedClient implements DisposableBean {
}
/**
* 安全的Set方法,在updateTimeout秒内返回结果, 否则返回false并取消操作.
* 安全的Set方法, 保证在updateTimeout秒内返回执行结果, 否则返回false并取消操作.
*/
public boolean safeSet(String key, Object value, int expiration) {
public boolean safeSet(String key, int expiration, Object value) {
Future<Boolean> future = memcachedClient.set(key, expiration, value);
try {
return future.get(updateTimeout, TimeUnit.MILLISECONDS);
@@ -86,7 +86,7 @@ public class SpyMemcachedClient implements DisposableBean {
}
/**
* 安全的Delete方法,在updateTimeout秒内返回结果, 否则返回false并取消操作.
* 安全的Delete方法, 保证在updateTimeout秒内返回执行结果, 否则返回false并取消操作.
*/
public boolean safeDelete(String key) {
Future<Boolean> future = memcachedClient.delete(key);
@@ -2,6 +2,8 @@ package org.springside.modules.memcached;
import static org.junit.Assert.*;
import java.util.Map;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.beans.factory.annotation.Autowired;
@@ -11,12 +13,14 @@ import org.springside.modules.test.category.UnStable;
import org.springside.modules.test.spring.SpringContextTestCase;
import org.springside.modules.utils.Threads;
import com.google.common.collect.Lists;
@Category(UnStable.class)
@ContextConfiguration(locations = { "/applicationContext-memcached.xml" })
public class SpyMemcachedClientTest extends SpringContextTestCase {
@Autowired
private SpyMemcachedClient spyMemcachedClient;
private SpyMemcachedClient client;
@Test
public void normal() {
@@ -24,15 +28,49 @@ public class SpyMemcachedClientTest extends SpringContextTestCase {
String key = "consumer:1";
String value = "admin";
spyMemcachedClient.set(key, 60 * 60 * 1, value);
String key2 = "consumer:2";
String value2 = "user";
//get/set
client.set(key, 60 * 60 * 1, value);
Threads.sleep(1000);
String result = spyMemcachedClient.get(key);
String result = client.get(key);
assertEquals(value, result);
spyMemcachedClient.delete(key);
//safeSet
client.safeSet(key2, 60 * 60 * 1, value2);
result = client.get(key2);
assertEquals(value2, result);
//bulk
Map<String, Object> bulkResult = client.getBulk(Lists.newArrayList(key, key2));
assertEquals(2, bulkResult.size());
assertEquals(value, bulkResult.get(key));
//delete
client.delete(key);
Threads.sleep(1000);
result = spyMemcachedClient.get(key);
result = client.get(key);
assertNull(result);
client.safeDelete(key);
result = client.get(key);
assertNull(result);
}
@Test
public void incr() {
String key = "counter";
assertEquals(1, client.incr(key, 1, 1));
//注意counter的实际类型是String
assertEquals("1", client.get(key));
assertEquals(2, client.incr(key, 1, 1));
assertEquals("2", client.get(key));
assertEquals(0, client.decr(key, 2, 1));
assertEquals("0", client.get(key));
}
}