001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tctest;
005:
006: import com.tc.object.config.ConfigLockLevel;
007: import com.tc.object.config.ConfigVisitor;
008: import com.tc.object.config.DSOClientConfigHelper;
009: import com.tc.object.config.TransparencyClassSpec;
010: import com.tc.simulator.app.ApplicationConfig;
011: import com.tc.simulator.listener.ListenerProvider;
012: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: public class NestedConcurrentLockTest extends TransparentTestBase {
019:
020: private static final int NODE_COUNT = 3;
021:
022: public void setUp() throws Exception {
023: super .setUp();
024: getTransparentAppConfig().setClientCount(NODE_COUNT)
025: .setIntensity(1);
026: initializeTestRunner();
027: }
028:
029: protected Class getApplicationClass() {
030: return NestedConcurrentLockTestApp.class;
031: }
032:
033: public static class NestedConcurrentLockTestApp extends
034: AbstractErrorCatchingTransparentApp {
035: private static final int NUM = 1000;
036: private final Object concurrentLock = new Object();
037: private final List list = new ArrayList();
038:
039: public NestedConcurrentLockTestApp(String appId,
040: ApplicationConfig cfg, ListenerProvider listenerProvider) {
041: super (appId, cfg, listenerProvider);
042: }
043:
044: protected void runTest() throws Throwable {
045: for (int i = 1; i <= NUM; i++) {
046: concurrent();
047: if ((i % 100) == 0) {
048: System.out.println(getApplicationId()
049: + " has reached " + i);
050: }
051: }
052: }
053:
054: private void concurrent() {
055: synchronized (concurrentLock) {
056: write();
057: }
058: }
059:
060: private void write() {
061: synchronized (list) {
062: int add = validate();
063: list.add(new Integer(add));
064: }
065: }
066:
067: private int validate() {
068: int expect = 0;
069: for (Iterator iter = list.iterator(); iter.hasNext();) {
070: Integer integer = (Integer) iter.next();
071: if (integer.intValue() != expect) {
072: throw new RuntimeException("Expected " + expect
073: + ", but was " + integer.intValue() + "\n"
074: + list);
075: }
076: expect++;
077: }
078:
079: return expect;
080: }
081:
082: public static void visitL1DSOConfig(ConfigVisitor visitor,
083: DSOClientConfigHelper config) {
084: String testClass = NestedConcurrentLockTestApp.class
085: .getName();
086: TransparencyClassSpec spec = config
087: .getOrCreateSpec(testClass);
088:
089: String methodExpression = "* " + testClass
090: + ".concurrent(..)";
091: config.addAutolock(methodExpression,
092: ConfigLockLevel.CONCURRENT);
093:
094: methodExpression = "* " + testClass + ".write(..)";
095: config.addAutolock(methodExpression, ConfigLockLevel.WRITE);
096:
097: spec.addRoot("list", "list");
098: spec.addRoot("concurrentLock", "concurrentLock");
099: }
100:
101: }
102:
103: }
|