1 package org.apache.commons.jcs;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.concurrent.atomic.AtomicInteger;
26
27 import junit.framework.TestCase;
28
29 import org.apache.commons.jcs.access.GroupCacheAccess;
30 import org.apache.commons.jcs.access.exception.CacheException;
31
32
33
34
35
36
37
38 public class JCSConcurrentCacheAccessUnitTest extends TestCase
39 {
40 private final static int THREADS = 10;
41 private final static int LOOPS = 10000;
42
43
44
45
46 protected GroupCacheAccess<Integer, String> cache;
47
48
49
50
51 protected String group = "group";
52
53
54
55
56 protected AtomicInteger errcount;
57
58
59
60
61 protected List<String> valueMismatchList;
62
63 @Override
64 protected void setUp() throws Exception
65 {
66 super.setUp();
67 JCS.setConfigFilename( "/TestJCS-73.ccf" );
68 cache = JCS.getGroupCacheInstance( "cache" );
69 errcount = new AtomicInteger(0);
70 valueMismatchList = Collections.synchronizedList(new ArrayList<String>());
71 }
72
73 @Override
74 protected void tearDown()
75 throws Exception
76 {
77 super.tearDown();
78 cache.clear();
79 cache.dispose();
80 }
81
82
83
84
85 protected class Worker extends Thread
86 {
87 @Override
88 public void run()
89 {
90 String name = getName();
91
92 for (int idx = 0; idx < LOOPS; idx++)
93 {
94 if (idx > 0)
95 {
96
97 String res = cache.getFromGroup(Integer.valueOf(idx-1), group);
98
99 if (res == null)
100 {
101
102 System.out.println("ERROR: for " + idx + " in " + name);
103 errcount.incrementAndGet();
104
105
106 int n = 5;
107 while (n-- > 0)
108 {
109 res = cache.getFromGroup(Integer.valueOf(idx-1), group);
110 if (res != null)
111 {
112
113 System.out.println("ERROR FIXED for " + idx + ": " + res + " " + name);
114 errcount.decrementAndGet();
115 break;
116 }
117
118 System.out.println("ERROR STILL PERSISTS for " + idx + " in " + name);
119 try
120 {
121 Thread.sleep(1000);
122 }
123 catch (InterruptedException e)
124 {
125
126 }
127 }
128 }
129
130 if (!String.valueOf(idx-1).equals(res))
131 {
132 valueMismatchList.add(String.format("Values do not match: %s - %s", String.valueOf(idx-1), res));
133 }
134 }
135
136
137 try
138 {
139 cache.putInGroup(Integer.valueOf(idx), group, String.valueOf(idx));
140 }
141 catch (CacheException e)
142 {
143
144 }
145
146
147
148
149
150 }
151
152 }
153 }
154
155
156
157
158
159 public void testConcurrentAccess()
160 throws Exception
161 {
162 Worker[] worker = new Worker[THREADS];
163
164 for (int i = 0; i < THREADS; i++)
165 {
166 worker[i] = new Worker();
167 worker[i].start();
168 }
169
170 for (int i = 0; i < THREADS; i++)
171 {
172 worker[i].join();
173 }
174
175 assertEquals("Error count should be 0", 0, errcount.intValue());
176 for (String msg : valueMismatchList)
177 {
178 System.out.println(msg);
179 }
180 assertEquals("Value mismatch count should be 0", 0, valueMismatchList.size());
181 }
182
183 }