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.CyclicBarrier;
007:
008: import com.tc.object.config.ConfigVisitor;
009: import com.tc.object.config.DSOClientConfigHelper;
010: import com.tc.object.config.TransparencyClassSpec;
011: import com.tc.object.config.spec.CyclicBarrierSpec;
012: import com.tc.simulator.app.ApplicationConfig;
013: import com.tc.simulator.listener.ListenerProvider;
014: import com.tc.util.Assert;
015: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
016:
017: import java.io.BufferedWriter;
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.DataInputStream;
021: import java.io.DataOutputStream;
022: import java.io.StringWriter;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: public class RandomIOClassesTest extends TransparentTestBase {
027:
028: private static final int NODE_COUNT = 2;
029:
030: protected void setUp() throws Exception {
031: super .setUp();
032: getTransparentAppConfig().setClientCount(NODE_COUNT)
033: .setIntensity(1);
034: initializeTestRunner();
035: }
036:
037: protected Class getApplicationClass() {
038: return RandomIOClassesTestApp.class;
039: }
040:
041: public static class RandomIOClassesTestApp extends
042: AbstractErrorCatchingTransparentApp {
043:
044: private final Map map = new HashMap();
045: private final CyclicBarrier barrier;
046:
047: public RandomIOClassesTestApp(String appId,
048: ApplicationConfig cfg, ListenerProvider listenerProvider) {
049: super (appId, cfg, listenerProvider);
050: this .barrier = new CyclicBarrier(getParticipantCount());
051: }
052:
053: protected void runTest() throws Throwable {
054: int index = barrier.barrier();
055:
056: if (index == 0) {
057: StringWriter sw = new StringWriter();
058: ByteArrayOutputStream baos = new ByteArrayOutputStream();
059:
060: synchronized (map) {
061: map.put("sw", sw);
062: map.put("bw", new BufferedWriter(sw));
063: map.put("baos", baos);
064: map.put("dos", new DataOutputStream(baos));
065: }
066: }
067:
068: barrier.barrier();
069:
070: final StringWriter sw = (StringWriter) map.get("sw");
071: final BufferedWriter bw = (BufferedWriter) map.get("bw");
072: final ByteArrayOutputStream baos = (ByteArrayOutputStream) map
073: .get("baos");
074: final DataOutputStream dos = (DataOutputStream) map
075: .get("dos");
076:
077: if (index == 0) {
078: synchronized (bw) {
079: bw.write("Hello");
080: bw.write(" there");
081: bw.flush();
082: }
083:
084: synchronized (dos) {
085: dos
086: .writeUTF("Nothing beats a distributed DataOutputStream");
087: dos.writeInt(42);
088: dos.writeBoolean(false);
089: }
090: }
091:
092: barrier.barrier();
093:
094: if (index == 1) {
095: Assert.assertEquals("Hello there", sw.toString());
096:
097: DataInputStream dis = new DataInputStream(
098: new ByteArrayInputStream(baos.toByteArray()));
099: Assert.assertEquals(dis.readUTF(),
100: "Nothing beats a distributed DataOutputStream");
101: Assert.assertEquals(42, dis.readInt());
102: Assert.assertFalse(dis.readBoolean());
103: }
104: }
105:
106: public static void visitL1DSOConfig(ConfigVisitor visitor,
107: DSOClientConfigHelper config) {
108: String testClass = RandomIOClassesTestApp.class.getName();
109: TransparencyClassSpec spec = config
110: .getOrCreateSpec(testClass);
111:
112: spec.addRoot("map", "map");
113: spec.addRoot("barrier", "barrier");
114:
115: String methodExpression = "* " + testClass + "*.*(..)";
116: config.addWriteAutolock(methodExpression);
117:
118: new CyclicBarrierSpec().visit(visitor, config);
119: }
120:
121: }
122:
123: }
|