1 4 package com.tctest.longrunning; 5 6 import EDU.oswego.cs.dl.util.concurrent.CountDown; 7 8 import com.tc.objectserver.control.ServerControl; 9 import com.tc.simulator.app.ApplicationConfig; 10 import com.tc.simulator.listener.MockListenerProvider; 11 import com.tc.simulator.listener.MockOutputListener; 12 import com.tc.simulator.listener.MockResultsListener; 13 import com.tc.simulator.listener.MockStatsListener; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Random ; 19 20 import junit.framework.TestCase; 21 22 public class LargeGraphTestAppTest extends TestCase { 23 24 private ApplicationConfig cfg; 25 private MockListenerProvider listeners; 26 27 public void setUp() throws Exception { 28 super.setUp(); 29 cfg = new ApplicationConfig() { 30 31 public String getApplicationClassname() { 32 return LargeGraphTestApp.class.getName(); 33 } 34 35 public void setAttribute(String key, String value) { 36 } 38 39 public String getAttribute(String key) { 40 return null; 41 } 42 43 public int getIntensity() { 44 throw new AssertionError (); 45 } 46 47 public int getGlobalParticipantCount() { 48 throw new AssertionError (); 49 } 50 51 public ApplicationConfig copy() { 52 throw new AssertionError (); 53 } 54 55 public ServerControl getServerControl() { 56 throw new AssertionError (); 57 } 58 59 }; 60 listeners = new MockListenerProvider(); 61 listeners.outputListener = new MockOutputListener(); 62 listeners.resultsListener = new MockResultsListener(); 63 listeners.statsListener = new MockStatsListener(); 64 } 65 66 private void test(LargeGraphTestApp application, int objectCount) throws Exception { 67 application.growGraph(objectCount, 50); 68 assertEquals(objectCount, application.getObjectCount()); 69 if (LargeGraphTestApp.doVerify()) { 70 application.verifyGraph(); 71 application.verifyReferences(); 72 } 73 } 74 75 public void testBasic() throws Exception { 76 test(new LargeGraphTestApp("yer app id", cfg, listeners), 100); 77 } 78 79 public void testConcurrent() throws Throwable { 80 int threads = 10; 81 final CountDown countdown = new CountDown(threads); 82 final Random random = new Random (); 83 final List errors = new ArrayList (); 84 for (int i = 0; i < threads; i++) { 85 final String id = i + ""; 86 Thread t = new Thread (new Runnable () { 87 88 public void run() { 89 for (int j = 0; j < 10; j++) { 90 final LargeGraphTestApp app = new LargeGraphTestApp(id + j, cfg, listeners); 91 int objectCount = random.nextInt(10) * 1000; 92 if (objectCount == 0) objectCount = 1000; 93 try { 94 test(app, objectCount); 95 } catch (Throwable e) { 96 errors.add(e); 97 countdown.release(); 98 return; 99 } 100 } 101 countdown.release(); 102 } 103 104 }); 105 t.start(); 106 } 107 countdown.acquire(); 108 for (Iterator i = errors.iterator(); i.hasNext();) { 109 ((Throwable ) i.next()).printStackTrace(); 110 } 111 if (errors.size() > 0) { throw (Throwable ) errors.get(0); } 112 } 113 114 } | Popular Tags |