KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > longrunning > LargeGraphTestAppTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

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 JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Random JavaDoc;
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 JavaDoc {
28     super.setUp();
29     cfg = new ApplicationConfig() {
30
31       public String JavaDoc getApplicationClassname() {
32         return LargeGraphTestApp.class.getName();
33       }
34
35       public void setAttribute(String JavaDoc key, String JavaDoc value) {
36         //
37
}
38
39       public String JavaDoc getAttribute(String JavaDoc key) {
40         return null;
41       }
42
43       public int getIntensity() {
44         throw new AssertionError JavaDoc();
45       }
46
47       public int getGlobalParticipantCount() {
48         throw new AssertionError JavaDoc();
49       }
50
51       public ApplicationConfig copy() {
52         throw new AssertionError JavaDoc();
53       }
54
55       public ServerControl getServerControl() {
56         throw new AssertionError JavaDoc();
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 JavaDoc {
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 JavaDoc {
76     test(new LargeGraphTestApp("yer app id", cfg, listeners), 100);
77   }
78
79   public void testConcurrent() throws Throwable JavaDoc {
80     int threads = 10;
81     final CountDown countdown = new CountDown(threads);
82     final Random JavaDoc random = new Random JavaDoc();
83     final List JavaDoc errors = new ArrayList JavaDoc();
84     for (int i = 0; i < threads; i++) {
85       final String JavaDoc id = i + "";
86       Thread JavaDoc t = new Thread JavaDoc(new Runnable JavaDoc() {
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 JavaDoc 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 JavaDoc i = errors.iterator(); i.hasNext();) {
109       ((Throwable JavaDoc) i.next()).printStackTrace();
110     }
111     if (errors.size() > 0) { throw (Throwable JavaDoc) errors.get(0); }
112   }
113
114 }
Popular Tags