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 EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
007: import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt;
008:
009: import com.tc.object.config.ConfigVisitor;
010: import com.tc.object.config.DSOClientConfigHelper;
011: import com.tc.object.config.TransparencyClassSpec;
012: import com.tc.object.config.spec.LinkedQueueSpec;
013: import com.tc.object.config.spec.SynchronizedIntSpec;
014: import com.tc.simulator.app.ApplicationConfig;
015: import com.tc.simulator.listener.ListenerProvider;
016: import com.tctest.runner.AbstractTransparentApp;
017:
018: import java.util.Random;
019:
020: public class LinkedQueueTestApp extends AbstractTransparentApp {
021:
022: public static int COUNT = 500;
023: public static int DEBUG_COUNT = 100;
024:
025: private LinkedQueue queue = new LinkedQueue();
026: //private List queue = new ArrayList();
027: private SynchronizedInt in = new SynchronizedInt(0);
028: private SynchronizedInt out = new SynchronizedInt(0);
029:
030: public LinkedQueueTestApp(String appId, ApplicationConfig cfg,
031: ListenerProvider listenerProvider) {
032: super (appId, cfg, listenerProvider);
033: }
034:
035: public void run() {
036: Random random = new Random();
037: while (out.get() < COUNT) {
038: if ((random.nextInt(2) == 0) || (in.get() >= COUNT)) {
039: get();
040: } else {
041: put();
042: }
043: }
044: }
045:
046: private void get() {
047: synchronized (out) {
048: try {
049: if (!queue.isEmpty()) {
050: Integer i = (Integer) queue.take();
051: //Integer i = (Integer) take(queue);
052: if (i.intValue() != out.increment()) {
053: throw new AssertionError(" Got = "
054: + i.intValue() + " and Expected = "
055: + out.get());
056: }
057: //if ((i.intValue() % DEBUG_COUNT) == 0) {
058: println(" Got : " + i);
059: //}
060: }
061: } catch (InterruptedException e) {
062: throw new AssertionError(e);
063: }
064: }
065:
066: }
067:
068: private void put() {
069: synchronized (in) {
070: try {
071: Integer i = new Integer(in.increment());
072: queue.put(i);
073: //put(queue, i);
074: println("Put : " + i);
075: } catch (InterruptedException e) {
076: throw new AssertionError(e);
077: }
078: }
079: }
080:
081: public static void visitL1DSOConfig(ConfigVisitor visitor,
082: DSOClientConfigHelper config) {
083:
084: String testClassName = LinkedQueueTestApp.class.getName();
085: TransparencyClassSpec spec = config
086: .getOrCreateSpec(testClassName);
087:
088: // Create Roots
089: spec.addRoot("queue", testClassName + ".queue");
090: spec.addRoot("in", testClassName + ".in");
091: spec.addRoot("out", testClassName + ".out");
092:
093: // Create locks
094: String runExpression = "* " + testClassName + ".*(..)";
095: System.err.println("Adding write autolock for: "
096: + runExpression);
097: config.addWriteAutolock(runExpression);
098:
099: new SynchronizedIntSpec().visit(visitor, config);
100:
101: new LinkedQueueSpec().visit(visitor, config);
102: }
103:
104: // private static Object take(List workQueue2) {
105: // synchronized (workQueue2) {
106: // while (workQueue2.size() == 0) {
107: // try {
108: // workQueue2.wait();
109: // } catch (InterruptedException e) {
110: // throw new RuntimeException(e);
111: // }
112: // }
113: // return workQueue2.remove(0);
114: // }
115: // }
116: //
117: // private static void put(List workQueue2, Object o) {
118: // synchronized (workQueue2) {
119: // workQueue2.add(o);
120: // workQueue2.notify();
121: // }
122: // }
123:
124: private static void println(Object o) {
125: System.err.println(Thread.currentThread().getName() + " : "
126: + String.valueOf(o));
127: }
128:
129: }
|