-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
RaceTestCase.java
74 lines (62 loc) · 2.17 KB
/
RaceTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package ognl.race;
import ognl.DefaultMemberAccess;
import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
public class RaceTestCase {
@Test
public void testOgnlRace(){
int concurrent = 128;
final int batchCount = 2000;
final CountDownLatch start = new CountDownLatch(1);
final CountDownLatch wait = new CountDownLatch(concurrent);
final AtomicInteger errCount = new AtomicInteger(0);
final Persion persion = new Persion();
for (int i = 0; i < concurrent;i++){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
start.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int j = 0; j < batchCount;j++){
if(j % 2 == 0) {
runValue(persion, "yn", errCount);
} else {
runValue(persion, "name", errCount);
}
}
wait.countDown();
}
});
thread.setName("work-"+i);
thread.start();
}
start.countDown();
try {
wait.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.out.println("error:" + errCount.get());
Assert.assertTrue(errCount.get() == 0);
}
private void runValue(Persion persion,String name,AtomicInteger errCount) {
OgnlContext context = new OgnlContext(null,null,new DefaultMemberAccess(false));
context.setRoot(persion);
try {
Object value = Ognl.getValue(name, context, context.getRoot());
// System.out.println(value);
} catch (OgnlException e) {
errCount.incrementAndGet();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}