KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cache > test > eviction > ConcurrentEvictAndRemoveTestCase


1 /*
2  *
3  * JBoss, the OpenSource J2EE webOS
4  *
5  * Distributable under LGPL license.
6  * See terms of license at gnu.org.
7  */

8
9 package org.jboss.test.cache.test.eviction;
10
11 import java.util.Random JavaDoc;
12
13 import junit.framework.Test;
14 import junit.framework.TestCase;
15 import junit.framework.TestSuite;
16
17 import org.apache.log4j.Logger;
18 import org.jboss.cache.Fqn;
19 import org.jboss.cache.PropertyConfigurator;
20 import org.jboss.cache.TreeCache;
21 import org.jboss.cache.eviction.RegionManager;
22 /**
23  * Local mode test for concurrent EvictionTimerTask evicting nodes and Client removing nodes from TreeCache.
24  *
25  * @version $Revision: 1.1.4.1 $
26  * @author <a HREF="mailto:uwe.lamprecht@gmx.de">Uwe Lamprecht</a> July 29 2004
27  */

28 public final class ConcurrentEvictAndRemoveTestCase extends TestCase
29 {
30     private static final Logger logger = Logger.getLogger(ConcurrentEvictAndRemoveTestCase.class.getName());
31     private TreeCache cache;
32
33
34    static void log(String JavaDoc msg) {
35       System.out.println(msg);
36       logger.info(msg);
37    }
38
39    void err(String JavaDoc msg) {
40       System.err.println(msg);
41       logger.error(msg);
42    }
43
44     public ConcurrentEvictAndRemoveTestCase(String JavaDoc name)
45     {
46         super(name);
47     }
48
49     public void setUp() throws Exception JavaDoc
50     {
51         cache = new TreeCache();
52         cache.setCacheMode(TreeCache.LOCAL);
53         PropertyConfigurator config = new PropertyConfigurator();
54         config.configure(cache, "META-INF/local-eviction-service.xml");
55         cache.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
56         cache.start();
57     }
58     public void tearDown() throws Exception JavaDoc
59     {
60         cache.stop();
61     }
62
63     public void test() throws Exception JavaDoc
64     {
65         final long MAX_LOOP_TIME = 4000; // value to experiment;)
66
int numberOfInitialCreatedNodes = 0;
67
68         log("*** Start Test: ConcurrentEvictAndRemoveTest ***");
69
70         // creation of nodes
71
{
72             final StopWatch stopWatch = new StopWatch();
73             int i = 0;
74             while(true) {
75                 i++;
76                 final Fqn fqn = Fqn.fromString("/test/item_" + i);
77                 cache.put(fqn, "att", null);
78                 if (stopWatch.getTime()>MAX_LOOP_TIME) {
79                     break;
80                 }
81             }
82             numberOfInitialCreatedNodes = i;
83             log(
84                     "nodes created: "
85                     + numberOfInitialCreatedNodes
86                     + " time: "
87                     + stopWatch.getTime()
88                     + " ms"
89
90             );
91         }
92
93         // random remove nodes
94
{
95             int numberCacheHitSuccess = 0;
96             int numberCacheHitFailure = 0;
97             final Random JavaDoc random = new Random JavaDoc();
98             final StopWatch stopWatch = new StopWatch();
99
100             while(true) {
101                 final Fqn fqn = Fqn.fromString("/test/item_" + random.nextInt(numberOfInitialCreatedNodes));
102                 if (cache.exists(fqn)) {
103                     cache.remove(fqn);
104                     numberCacheHitSuccess++;
105                 } else {
106                     numberCacheHitFailure++;
107                 }
108                 if (stopWatch.getTime()>MAX_LOOP_TIME) {
109                     break;
110                 }
111             }
112             log(
113                     "nodes removed: "
114                     + numberCacheHitSuccess
115                     + ", total cache hits: "
116                     + (numberCacheHitFailure + numberCacheHitSuccess)
117                     + " time: "
118                     + stopWatch.getTime()
119                     + " ms"
120             );
121         }
122
123         // create/remove nodes (-->queue overflow and blocking)
124
{
125             log("create put/remove events");
126             final int SEQUENCE_SIZE = 10000; // number of nodes processed by a single thread
127
final long TIMEOUT_DEFAULT = 100000;
128             final int MAX_TIMEOUT_FACTOR = 10;
129
130             for(int i = 0; i < RegionManager.CAPACITY; i = i + SEQUENCE_SIZE) {
131                 final int sequenceStart = i;
132                 final int sequenceEnd = i + SEQUENCE_SIZE -1;
133
134                 Thread JavaDoc thread = new Thread JavaDoc(
135                         new HelperThread(cache, sequenceStart, sequenceEnd)
136                 );
137
138                 long timeout = (HelperThread.getTime()==HelperThread.EXECUTION_TIME_NOT_INITIALIZED)?
139                         TIMEOUT_DEFAULT:
140                             HelperThread.getTime()*MAX_TIMEOUT_FACTOR;
141
142                 thread.start();
143                 try {
144                     thread.join(timeout);
145                     if (thread.isAlive()) {
146                         // Timeout occurred; thread has not finished
147
err("Timeout of " + timeout + " ms exceeded for sequence of put/remove operations.");
148                         fail("Timeout of " + timeout + " ms exceeded for sequence of put/remove operations.");
149                     } else {
150                         // Finished
151
}
152                 }
153                 catch(InterruptedException JavaDoc ignore) {
154                     err("Thread join interrupted.");
155                 }
156             }
157         }
158         log("*** Successfully finished! ***");
159     }
160     public static Test suite() throws Exception JavaDoc
161     {
162         return new TestSuite(ConcurrentEvictAndRemoveTestCase.class);
163     }
164
165     // private util classes
166
private static final class StopWatch {
167         public StopWatch() {
168             startTime = System.currentTimeMillis() ;
169         }
170         private final long startTime; // Start time of stopwatch.
171
// (Time is measured in milliseconds.)
172

173         public String JavaDoc toString() {
174             return String.valueOf(getTime());
175         }
176         public long getTime() {
177             return System.currentTimeMillis() - startTime;
178         }
179     }
180     private static final class HelperThread implements Runnable JavaDoc {
181         private static final Logger logger = Logger.getLogger(HelperThread.class.getName());
182         private final TreeCache cache;
183         private final int start;
184         private final int end;
185         // static member
186
public static final long EXECUTION_TIME_NOT_INITIALIZED = -1;
187         private static long previousExceutionTime = EXECUTION_TIME_NOT_INITIALIZED;
188         private static final String JavaDoc FQN_PREFIX = "/test/overflow-item_";
189
190         public HelperThread ( TreeCache cache, int start, int end) {
191             this.cache = cache;
192             this.start = start;
193             this.end = end;
194         }
195         public void run() {
196             StopWatch stopWatch = new StopWatch();
197             log(
198                     "process node range: "
199                     + this.start
200                     + "/"
201                     + this.end
202             );
203             try {
204                 for (int i = start; i<=end; i++) {
205                     final Fqn fqn = Fqn.fromString(FQN_PREFIX + i);
206                     cache.put(fqn, "att", null);
207                     cache.remove(fqn);
208                     //Thread.yield();
209
}
210             } catch (Exception JavaDoc e) {
211                 System.out.println("exception: " + e);
212                 if ( e instanceof RuntimeException JavaDoc) {
213                     throw (RuntimeException JavaDoc)e;
214                 } else {
215                     throw new RuntimeException JavaDoc(e);
216                 }
217             }
218             previousExceutionTime = stopWatch.getTime();
219             log(
220                     "time: "
221                     + previousExceutionTime
222                     + " ms"
223             );
224         }
225         public static long getTime() {
226             return previousExceutionTime;
227         }
228     }
229 }
230
Popular Tags