private static final int DATA_OPERATION = 10;
private static final int MAX = 10;
private static String[] ids = new String[MAX];
static {
for(int i=0; i<MAX; i++) {
ids[i] = UUID.randomUUID().toString();
}
}
private Hit hit;
private FullCache<ArticleCategory> cache;
@Before
public void setUp() {
hit = new Hit();
cache = new FullCache<ArticleCategory>() {
@Override
protected List<ArticleCategory> doGetList() {
hit.notHit();
List<ArticleCategory> list = new ArrayList<ArticleCategory>();
for(int i=0; i<MAX; i++) {
ArticleCategory obj = new ArticleCategory();
obj.setId(ids[i]);
list.add(obj);
}
doSleep(DATA_OPERATION);
return list;
}
@Override
public List<ArticleCategory> getCachedList() {
hit.total();
return super.getCachedList();
}
};
}
@Test
public void testMultiThread() {
final int THREADS = 100;
final int LOOP_PER_THREAD = 100000;
List<Thread> threads = new ArrayList<Thread>(THREADS);
// test FullCache.getCachedList(id):
for(int i=0; i<THREADS; i++) {
threads.add(
new Thread() {
public void run() {
for(int j=0; j<LOOP_PER_THREAD; j++) {
List<ArticleCategory> list = cache.getCachedList();
for(int k=0; k<MAX; k++) {
assertEquals(ids[k], list.get(k).getId());
}
}
}
}
);
}
文章来源于领测软件测试网 https://www.ltesting.net/