KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > runner > DistributedTestRunner


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

5 package com.tctest.runner;
6
7 import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
8
9 import com.tc.config.schema.SettableConfigItem;
10 import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
11 import com.tc.config.schema.setup.L2TVSConfigurationSetupManager;
12 import com.tc.config.schema.setup.TVSConfigurationSetupManagerFactory;
13 import com.tc.config.schema.setup.TestTVSConfigurationSetupManagerFactory;
14 import com.tc.lang.TCThreadGroup;
15 import com.tc.lang.ThrowableHandler;
16 import com.tc.logging.TCLogging;
17 import com.tc.object.bytecode.hook.impl.PreparedComponentsFromL2Connection;
18 import com.tc.object.config.ConfigVisitor;
19 import com.tc.object.config.DSOClientConfigHelper;
20 import com.tc.objectserver.impl.DistributedObjectServer;
21 import com.tc.server.TCServerImpl;
22 import com.tc.simulator.app.ApplicationBuilder;
23 import com.tc.simulator.app.ApplicationConfig;
24 import com.tc.simulator.app.DSOApplicationBuilder;
25 import com.tc.simulator.app.ErrorContext;
26 import com.tc.simulator.container.Container;
27 import com.tc.simulator.container.ContainerConfig;
28 import com.tc.simulator.container.ContainerResult;
29 import com.tc.simulator.container.ContainerStateFactory;
30 import com.tc.simulator.control.Control;
31 import com.tc.simulator.listener.ResultsListener;
32 import com.tcsimulator.ControlImpl;
33 import com.tcsimulator.container.ContainerStateFactoryObject;
34 import com.tcsimulator.listener.QueuePrinter;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * Takes an application configuration and some parameters and runs a single-vm, multi-node (multi-classloader) test.
43  */

44 public class DistributedTestRunner implements ResultsListener {
45
46   private final boolean startServer;
47
48   private final Class JavaDoc applicationClass;
49   private final ApplicationConfig applicationConfig;
50   private final ConfigVisitor configVisitor;
51   private final ContainerConfig containerConfig;
52   private final Control control;
53   private final ResultsListener[] resultsListeners;
54   private final DSOClientConfigHelper configHelper;
55   private final ApplicationBuilder[] applicationBuilders;
56   private final Container[] containers;
57   private final ContainerStateFactory containerStateFactory;
58   private final TestGlobalIdGenerator globalIdGenerator;
59   private final TCServerImpl server;
60   private final List JavaDoc errors = new ArrayList JavaDoc();
61   private final List JavaDoc results = new ArrayList JavaDoc();
62   private final DistributedTestRunnerConfig config;
63   private final TVSConfigurationSetupManagerFactory configFactory;
64   private boolean startTimedOut;
65   private boolean executionTimedOut;
66   private final int clientCount;
67   private final int applicationInstanceCount;
68   private final LinkedQueue statsOutputQueue;
69   private final QueuePrinter statsOutputPrinter;
70
71   private final Map JavaDoc optionalAttributes;
72
73   /**
74    * @param applicationClass Class of the application to be executed. It should implement the static method required by
75    * ClassLoaderConfigVisitor.
76    * @param applicationConfig Configuration object for the test application.
77    * @param nodeCount Number of classloaders to create.
78    * @param applicationsPerNode Number of application instances per classloader to execute. This counts as number of
79    * threads per classloader.
80    */

81   public DistributedTestRunner(DistributedTestRunnerConfig config, TVSConfigurationSetupManagerFactory configFactory,
82                                DSOClientConfigHelper configHelper, Class JavaDoc applicationClass, Map JavaDoc optionalAttributes,
83                                ApplicationConfig applicationConfig, int clientCount, int applicationInstanceCount,
84                                boolean startServer) throws Exception JavaDoc {
85     this.optionalAttributes = optionalAttributes;
86     this.clientCount = clientCount;
87     this.applicationInstanceCount = applicationInstanceCount;
88     this.startServer = startServer;
89     this.config = config;
90     this.configFactory = configFactory;
91     this.configHelper = configHelper;
92     this.globalIdGenerator = new TestGlobalIdGenerator();
93     this.applicationClass = applicationClass;
94     this.applicationConfig = applicationConfig;
95     this.configVisitor = new ConfigVisitor();
96     this.containerConfig = newContainerConfig();
97     this.statsOutputQueue = new LinkedQueue();
98     this.statsOutputPrinter = new QueuePrinter(this.statsOutputQueue, System.out);
99     this.containerStateFactory = new ContainerStateFactoryObject(statsOutputQueue);
100     this.control = newContainerControl();
101     this.resultsListeners = newResultsListeners(clientCount);
102     this.applicationBuilders = newApplicationBuilders(clientCount);
103     this.containers = new Container[clientCount];
104     for (int i = 0; i < containers.length; i++) {
105       containers[i] = new Container(this.containerConfig, this.containerStateFactory, this.globalIdGenerator,
106                                     this.control, this.resultsListeners[i], this.applicationBuilders[i]);
107     }
108     L2TVSConfigurationSetupManager manager = configFactory.createL2TVSConfigurationSetupManager(null);
109
110     this.server = new TCServerImpl(manager, new TCThreadGroup(new ThrowableHandler(TCLogging
111         .getLogger(DistributedObjectServer.class))));
112   }
113
114   public void run() {
115     try {
116       Thread JavaDoc statsOutputPrinterThread = new Thread JavaDoc(this.statsOutputPrinter);
117       statsOutputPrinterThread.setDaemon(true);
118       statsOutputPrinterThread.start();
119
120       visitApplicationClassLoaderConfig();
121       if (this.startServer) {
122         this.server.start();
123
124         if (this.configFactory instanceof TestTVSConfigurationSetupManagerFactory) {
125           TestTVSConfigurationSetupManagerFactory testFactory = (TestTVSConfigurationSetupManagerFactory) this.configFactory;
126           ((SettableConfigItem) testFactory.l2DSOConfig().listenPort()).setValue(getServerPort());
127           testFactory.activateConfigurationChange();
128         }
129       }
130
131       for (int i = 0; i < containers.length; i++) {
132         new Thread JavaDoc(containers[i]).start();
133       }
134       final boolean complete = this.control.waitForAllComplete(this.config.executionTimeout());
135
136       if (!complete) {
137         notifyExecutionTimeout();
138       }
139     } catch (Throwable JavaDoc t) {
140       notifyError(new ErrorContext(t));
141     } finally {
142       if (false && this.startServer) this.server.stop();
143     }
144   }
145
146   public int getServerPort() {
147     return this.server.getDSOListenPort();
148   }
149
150   public boolean success() {
151     synchronized (results) {
152       for (Iterator JavaDoc i = results.iterator(); i.hasNext();) {
153         ContainerResult result = (ContainerResult) i.next();
154         if (!result.success()) {
155           System.out.print(result);
156           return false;
157         }
158       }
159     }
160
161     if (errors.size() > 0) {
162       System.err.println(errors.size() + " ERRORS PRESENT");
163       return false;
164     } else if (startTimedOut) {
165       System.err.println("START TIMED OUT; timeout=" + this.config.startTimeout());
166       return false;
167     } else if (executionTimedOut) {
168       System.err.println("EXECUTION TIMED OUT; timeout=" + this.config.executionTimeout());
169       return false;
170     } else if (results.size() != containers.length) {
171       System.err.println(results.size() + " results present, EXPECTING " + containers.length);
172       return false;
173     } else {
174       return true;
175     }
176
177     // unreachable
178
}
179
180   public List JavaDoc getErrors() {
181     synchronized (errors) {
182       return new ArrayList JavaDoc(errors);
183     }
184   }
185
186   public boolean startTimedOut() {
187     return startTimedOut;
188   }
189
190   public boolean executionTimedOut() {
191     return executionTimedOut;
192   }
193
194   private void visitApplicationClassLoaderConfig() {
195     if (optionalAttributes.size() > 0) {
196       this.configVisitor.visit(this.configHelper, this.applicationClass, this.optionalAttributes);
197     } else {
198       this.configVisitor.visit(this.configHelper, this.applicationClass);
199     }
200   }
201
202   private ContainerConfig newContainerConfig() {
203     return new ContainerConfig() {
204
205       public int getApplicationInstanceCount() {
206         return applicationInstanceCount;
207       }
208
209       public long getContainerStartTimeout() {
210         return config.startTimeout();
211       }
212
213       public long getApplicationStartTimeout() {
214         return config.startTimeout();
215       }
216
217       public long getApplicationExecutionTimeout() {
218         return config.executionTimeout();
219       }
220
221       public boolean isMaster() {
222         return false;
223       }
224
225     };
226   }
227
228   private Control newContainerControl() {
229     return new ControlImpl(this.clientCount);
230   }
231
232   private ResultsListener[] newResultsListeners(int count) {
233     ResultsListener[] rv = new ResultsListener[count];
234     for (int i = 0; i < rv.length; i++) {
235       rv[i] = this;
236     }
237     return rv;
238   }
239
240   private ApplicationBuilder[] newApplicationBuilders(int count) throws Exception JavaDoc {
241     ApplicationBuilder[] rv = new ApplicationBuilder[count];
242     for (int i = 0; i < rv.length; i++) {
243       L1TVSConfigurationSetupManager l1ConfigManager = this.configFactory.createL1TVSConfigurationSetupManager();
244       l1ConfigManager.setupLogging();
245       PreparedComponentsFromL2Connection components = new PreparedComponentsFromL2Connection(l1ConfigManager);
246       rv[i] = new DSOApplicationBuilder(this.configHelper, this.applicationConfig, components);
247     }
248     return rv;
249   }
250
251   /*********************************************************************************************************************
252    * ResultsListener interface
253    */

254
255   public void setGlobalId(long globalId) {
256     return;
257   }
258
259   public void notifyStartTimeout() {
260     this.startTimedOut = true;
261   }
262
263   public void notifyExecutionTimeout() {
264     this.executionTimedOut = true;
265   }
266
267   public void notifyError(ErrorContext ectxt) {
268     synchronized (this.errors) {
269       ectxt.dump(System.err);
270       this.errors.add(ectxt);
271     }
272   }
273
274   public void notifyResult(Object JavaDoc result) {
275     synchronized (this.results) {
276       this.results.add(result);
277     }
278   }
279
280   public void dumpServer() {
281     this.server.dump();
282   }
283 }
284
Popular Tags