001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.longrunning;
006:
007: import EDU.oswego.cs.dl.util.concurrent.CountDown;
008:
009: import com.tc.net.proxy.TCPProxy;
010: import com.tc.objectserver.control.ServerControl;
011: import com.tc.simulator.app.ApplicationConfig;
012: import com.tc.simulator.listener.MockListenerProvider;
013: import com.tc.simulator.listener.MockOutputListener;
014: import com.tc.simulator.listener.MockResultsListener;
015: import com.tc.simulator.listener.MockStatsListener;
016:
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.Random;
021:
022: import junit.framework.TestCase;
023:
024: public class LargeGraphTestAppTest extends TestCase {
025:
026: private ApplicationConfig cfg;
027: private MockListenerProvider listeners;
028:
029: public void setUp() throws Exception {
030: super .setUp();
031: cfg = new ApplicationConfig() {
032:
033: public String getApplicationClassname() {
034: return LargeGraphTestApp.class.getName();
035: }
036:
037: public void setAttribute(String key, String value) {
038: //
039: }
040:
041: public String getAttribute(String key) {
042: return null;
043: }
044:
045: public int getIntensity() {
046: throw new AssertionError();
047: }
048:
049: public int getGlobalParticipantCount() {
050: throw new AssertionError();
051: }
052:
053: public ApplicationConfig copy() {
054: throw new AssertionError();
055: }
056:
057: public ServerControl getServerControl() {
058: throw new AssertionError();
059: }
060:
061: public int getValidatorCount() {
062: throw new AssertionError();
063: }
064:
065: public int getGlobalValidatorCount() {
066: throw new AssertionError();
067: }
068:
069: public TCPProxy[] getProxies() {
070: throw new AssertionError();
071: }
072:
073: public ServerControl[] getServerControls() {
074: throw new AssertionError();
075: }
076:
077: public Object getAttributeObject(String key) {
078: throw new AssertionError();
079: }
080: };
081: listeners = new MockListenerProvider();
082: listeners.outputListener = new MockOutputListener();
083: listeners.resultsListener = new MockResultsListener();
084: listeners.statsListener = new MockStatsListener();
085: }
086:
087: private void test(LargeGraphTestApp application, int objectCount)
088: throws Exception {
089: application.growGraph(objectCount, 50);
090: assertEquals(objectCount, application.getObjectCount());
091: if (LargeGraphTestApp.doVerify()) {
092: application.verifyGraph();
093: application.verifyReferences();
094: }
095: }
096:
097: public void testBasic() throws Exception {
098: test(new LargeGraphTestApp("yer app id", cfg, listeners), 100);
099: }
100:
101: public void testConcurrent() throws Throwable {
102: int threads = 10;
103: final CountDown countdown = new CountDown(threads);
104: final Random random = new Random();
105: final List errors = new ArrayList();
106: for (int i = 0; i < threads; i++) {
107: final String id = i + "";
108: Thread t = new Thread(new Runnable() {
109:
110: public void run() {
111: for (int j = 0; j < 10; j++) {
112: final LargeGraphTestApp app = new LargeGraphTestApp(
113: id + j, cfg, listeners);
114: int objectCount = random.nextInt(10) * 1000;
115: if (objectCount == 0)
116: objectCount = 1000;
117: try {
118: test(app, objectCount);
119: } catch (Throwable e) {
120: errors.add(e);
121: countdown.release();
122: return;
123: }
124: }
125: countdown.release();
126: }
127:
128: });
129: t.start();
130: }
131: countdown.acquire();
132: for (Iterator i = errors.iterator(); i.hasNext();) {
133: ((Throwable) i.next()).printStackTrace();
134: }
135: if (errors.size() > 0) {
136: throw (Throwable) errors.get(0);
137: }
138: }
139:
140: }
|