KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > NestedConcurrentLockTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tctest;
5
6 import com.tc.object.config.ConfigVisitor;
7 import com.tc.object.config.ConfigLockLevel;
8 import com.tc.object.config.DSOClientConfigHelper;
9 import com.tc.object.config.TransparencyClassSpec;
10 import com.tc.simulator.app.ApplicationConfig;
11 import com.tc.simulator.listener.ListenerProvider;
12 import com.tctest.runner.AbstractErrorCatchingTransparentApp;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 public class NestedConcurrentLockTest extends TransparentTestBase {
19
20   private static final int NODE_COUNT = 3;
21
22   public void setUp() throws Exception JavaDoc {
23     super.setUp();
24     getTransparentAppConfig().setClientCount(NODE_COUNT).setIntensity(1);
25     initializeTestRunner();
26   }
27
28   protected Class JavaDoc getApplicationClass() {
29     return NestedConcurrentLockTestApp.class;
30   }
31
32   public static class NestedConcurrentLockTestApp extends AbstractErrorCatchingTransparentApp {
33     private static final int NUM = 1000;
34     private final Object JavaDoc concurrentLock = new Object JavaDoc();
35     private final List JavaDoc list = new ArrayList JavaDoc();
36
37     public NestedConcurrentLockTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
38       super(appId, cfg, listenerProvider);
39     }
40
41     protected void runTest() throws Throwable JavaDoc {
42       for (int i = 1; i <= NUM; i++) {
43         concurrent();
44         if ((i % 100) == 0) {
45           System.out.println(getApplicationId() + " has reached " + i);
46         }
47       }
48     }
49
50     private void concurrent() {
51       synchronized (concurrentLock) {
52         write();
53       }
54     }
55
56     private void write() {
57       synchronized (list) {
58         int add = validate();
59         list.add(new Integer JavaDoc(add));
60       }
61     }
62
63     private int validate() {
64       int expect = 0;
65       for (Iterator JavaDoc iter = list.iterator(); iter.hasNext();) {
66         Integer JavaDoc integer = (Integer JavaDoc) iter.next();
67         if (integer.intValue() != expect) { throw new RuntimeException JavaDoc("Expected " + expect + ", but was "
68                                                                        + integer.intValue() + "\n" + list); }
69         expect++;
70       }
71
72       return expect;
73     }
74
75     public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) {
76       String JavaDoc testClass = NestedConcurrentLockTestApp.class.getName();
77       TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
78
79       String JavaDoc methodExpression = "* " + testClass + ".concurrent(..)";
80       config.addAutolock(methodExpression, ConfigLockLevel.CONCURRENT);
81
82       methodExpression = "* " + testClass + ".write(..)";
83       config.addAutolock(methodExpression, ConfigLockLevel.WRITE);
84
85       spec.addRoot("list", "list");
86       spec.addRoot("concurrentLock", "concurrentLock");
87     }
88
89   }
90
91 }
92
Popular Tags