1 4 package com.tctest; 5 6 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue; 7 import EDU.oswego.cs.dl.util.concurrent.SynchronizedInt; 8 9 import com.tc.object.config.ConfigVisitor; 10 import com.tc.object.config.DSOClientConfigHelper; 11 import com.tc.object.config.TransparencyClassSpec; 12 import com.tc.object.config.spec.LinkedQueueSpec; 13 import com.tc.object.config.spec.SynchronizedIntSpec; 14 import com.tc.simulator.app.ApplicationConfig; 15 import com.tc.simulator.listener.ListenerProvider; 16 import com.tctest.runner.AbstractTransparentApp; 17 18 import java.util.Random ; 19 20 public class LinkedQueueTestApp extends AbstractTransparentApp { 21 22 public static int COUNT = 500; 23 public static int DEBUG_COUNT = 100; 24 25 private LinkedQueue queue = new LinkedQueue(); 26 private SynchronizedInt in = new SynchronizedInt(0); 28 private SynchronizedInt out = new SynchronizedInt(0); 29 30 public LinkedQueueTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 31 super(appId, cfg, listenerProvider); 32 } 33 34 public void run() { 35 Random random = new Random (); 36 while (out.get() < COUNT) { 37 if ((random.nextInt(2) == 0) || (in.get() >= COUNT)) { 38 get(); 39 } else { 40 put(); 41 } 42 } 43 } 44 45 private void get() { 46 synchronized (out) { 47 try { 48 if (!queue.isEmpty()) { 49 Integer i = (Integer ) queue.take(); 50 if (i.intValue() != out.increment()) { 52 throw new AssertionError (" Got = " + i.intValue() + " and Expected = " + out.get()); 53 } 54 println(" Got : " + i); 56 } 58 } catch (InterruptedException e) { 59 throw new AssertionError (e); 60 } 61 } 62 63 } 64 65 private void put() { 66 synchronized (in) { 67 try { 68 Integer i = new Integer (in.increment()); 69 queue.put(i); 70 println("Put : " + i); 72 } catch (InterruptedException e) { 73 throw new AssertionError (e); 74 } 75 } 76 } 77 78 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 79 80 String testClassName = LinkedQueueTestApp.class.getName(); 81 TransparencyClassSpec spec = config.getOrCreateSpec(testClassName); 82 83 spec.addRoot("queue", testClassName + ".queue"); 85 spec.addRoot("in", testClassName + ".in"); 86 spec.addRoot("out", testClassName + ".out"); 87 88 String runExpression = "* " + testClassName + ".*(..)"; 90 System.err.println("Adding write autolock for: " + runExpression); 91 config.addWriteAutolock(runExpression); 92 93 new SynchronizedIntSpec().visit(visitor, config); 94 95 new LinkedQueueSpec().visit(visitor, config); 96 } 97 98 118 private static void println(Object o) { 119 System.err.println(Thread.currentThread().getName() + " : " + String.valueOf(o)); 120 } 121 122 } 123 | Popular Tags |