001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest;
006:
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.tc.util.Assert;
013: import com.tctest.runner.AbstractTransparentApp;
014:
015: import java.util.concurrent.CyclicBarrier;
016: import java.util.concurrent.LinkedBlockingQueue;
017:
018: public class LinkedBlockingQueueTestApp extends AbstractTransparentApp {
019: public static final String GC_TEST_KEY = "gc-test";
020:
021: private static final int DEFAULT_NUM_OF_PUT = 1000;
022: private static final int DEFAULT_NUM_OF_LOOP = 5;
023:
024: private static final int GC_NUM_OF_PUT = 1000;
025: private static final int GC_NUM_OF_LOOP = 5;
026: private static final int GC_CREATE_NUM = 5;
027:
028: private static final int CAPACITY = 100;
029:
030: private LinkedBlockingQueue queue = new LinkedBlockingQueue(
031: CAPACITY);
032: private final CyclicBarrier barrier;
033:
034: private boolean isGcTest = false;
035: private final int gcCreateNum;
036: private final int numOfPut;
037: private final int numOfLoop;
038:
039: public LinkedBlockingQueueTestApp(String appId,
040: ApplicationConfig cfg, ListenerProvider listenerProvider) {
041: super (appId, cfg, listenerProvider);
042: barrier = new CyclicBarrier(getParticipantCount());
043:
044: Boolean booleanObject = Boolean.valueOf(cfg
045: .getAttribute(GC_TEST_KEY));
046: isGcTest = booleanObject.booleanValue();
047:
048: if (isGcTest) {
049: gcCreateNum = GC_CREATE_NUM;
050: numOfPut = GC_NUM_OF_PUT;
051: numOfLoop = GC_NUM_OF_LOOP;
052: } else {
053: gcCreateNum = 1;
054: numOfPut = DEFAULT_NUM_OF_PUT;
055: numOfLoop = DEFAULT_NUM_OF_LOOP;
056: }
057:
058: System.err.println("***** setting isGcTest=[" + isGcTest
059: + "] gcCreateNum=[" + gcCreateNum + "] numOfPut=["
060: + numOfPut + "] numOfLoop=[" + numOfLoop + "]");
061: }
062:
063: public void run() {
064: try {
065: int index = barrier.await();
066:
067: for (int j = 0; j < gcCreateNum; j++) {
068: for (int i = 0; i < numOfLoop; i++) {
069: if (index == 0) {
070: doPut();
071: } else {
072: doGet();
073: }
074: barrier.await();
075: }
076: }
077:
078: barrier.await();
079: } catch (Throwable t) {
080: notifyError(t);
081: }
082: }
083:
084: private void doGet() throws Exception {
085: while (true) {
086: Object o = queue.take();
087: if ("STOP".equals(o)) {
088: break;
089: }
090: WorkItem w = (WorkItem) o;
091: System.out.println("Getting " + w.getI());
092: }
093: }
094:
095: private void doPut() throws Exception {
096: for (int i = 0; i < numOfPut; i++) {
097: System.out.println("Putting " + i);
098: queue.put(new WorkItem(i));
099: Assert.assertTrue(queue.size() <= CAPACITY);
100: }
101: int numOfGet = getParticipantCount() - 1;
102: for (int i = 0; i < numOfGet; i++) {
103: queue.put("STOP");
104: }
105: }
106:
107: public static void visitL1DSOConfig(ConfigVisitor visitor,
108: DSOClientConfigHelper config) {
109: String testClass = LinkedBlockingQueueTestApp.class.getName();
110: TransparencyClassSpec spec = config.getOrCreateSpec(testClass);
111:
112: config.addIncludePattern(testClass + "$*");
113:
114: String methodExpression = "* " + testClass + "*.*(..)";
115: config.addWriteAutolock(methodExpression);
116:
117: spec.addRoot("queue", "queue");
118: spec.addRoot("barrier", "barrier");
119: }
120:
121: private static class WorkItem {
122: private final int i;
123:
124: public WorkItem(int i) {
125: this .i = i;
126: }
127:
128: public int getI() {
129: return this.i;
130: }
131: }
132: }
|