1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier; 7 8 import com.tc.object.config.ConfigVisitor; 9 import com.tc.object.config.DSOClientConfigHelper; 10 import com.tc.object.config.TransparencyClassSpec; 11 import com.tc.object.config.spec.CyclicBarrierSpec; 12 import com.tc.simulator.app.ApplicationConfig; 13 import com.tc.simulator.listener.ListenerProvider; 14 import com.tc.util.Assert; 15 import com.tctest.runner.AbstractErrorCatchingTransparentApp; 16 17 import java.io.BufferedWriter ; 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.DataInputStream ; 21 import java.io.DataOutputStream ; 22 import java.io.StringWriter ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 public class RandomIOClassesTest extends TransparentTestBase { 27 28 private static final int NODE_COUNT = 2; 29 30 protected void setUp() throws Exception { 31 super.setUp(); 32 getTransparentAppConfig().setClientCount(NODE_COUNT).setIntensity(1); 33 initializeTestRunner(); 34 } 35 36 protected Class getApplicationClass() { 37 return RandomIOClassesTestApp.class; 38 } 39 40 public static class RandomIOClassesTestApp extends AbstractErrorCatchingTransparentApp { 41 42 private final Map map = new HashMap (); 43 private final CyclicBarrier barrier; 44 45 public RandomIOClassesTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 46 super(appId, cfg, listenerProvider); 47 this.barrier = new CyclicBarrier(getParticipantCount()); 48 } 49 50 protected void runTest() throws Throwable { 51 int index = barrier.barrier(); 52 53 if (index == 0) { 54 StringWriter sw = new StringWriter (); 55 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 56 57 synchronized (map) { 58 map.put("sw", sw); 59 map.put("bw", new BufferedWriter (sw)); 60 map.put("baos", baos); 61 map.put("dos", new DataOutputStream (baos)); 62 } 63 } 64 65 barrier.barrier(); 66 67 final StringWriter sw = (StringWriter ) map.get("sw"); 68 final BufferedWriter bw = (BufferedWriter ) map.get("bw"); 69 final ByteArrayOutputStream baos = (ByteArrayOutputStream ) map.get("baos"); 70 final DataOutputStream dos = (DataOutputStream ) map.get("dos"); 71 72 if (index == 0) { 73 synchronized (bw) { 74 bw.write("Hello"); 75 bw.write(" there"); 76 bw.flush(); 77 } 78 79 synchronized (dos) { 80 dos.writeUTF("Nothing beats a distributed DataOutputStream"); 81 dos.writeInt(42); 82 dos.writeBoolean(false); 83 } 84 } 85 86 barrier.barrier(); 87 88 if (index == 1) { 89 Assert.assertEquals("Hello there", sw.toString()); 90 91 DataInputStream dis = new DataInputStream (new ByteArrayInputStream (baos.toByteArray())); 92 Assert.assertEquals(dis.readUTF(), "Nothing beats a distributed DataOutputStream"); 93 Assert.assertEquals(42, dis.readInt()); 94 Assert.assertFalse(dis.readBoolean()); 95 } 96 } 97 98 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 99 String testClass = RandomIOClassesTestApp.class.getName(); 100 TransparencyClassSpec spec = config.getOrCreateSpec(testClass); 101 102 spec.addRoot("map", "map"); 103 spec.addRoot("barrier", "barrier"); 104 105 String methodExpression = "* " + testClass + "*.*(..)"; 106 config.addWriteAutolock(methodExpression); 107 108 new CyclicBarrierSpec().visit(visitor, config); 109 } 110 111 } 112 113 } 114 | Popular Tags |