001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Mikhail A. Markov, Vasily Zakharov
021: * @version $Revision: 1.1.2.4 $
022: */package org.apache.harmony.rmi;
023:
024: import java.io.EOFException;
025:
026: import java.rmi.RMISecurityManager;
027:
028: import java.rmi.registry.LocateRegistry;
029: import java.rmi.registry.Registry;
030:
031: import java.rmi.server.RemoteObject;
032: import java.rmi.server.UnicastRemoteObject;
033:
034: import org.apache.harmony.rmi.common.SubProcess;
035: import org.apache.harmony.rmi.test.MyRemoteInterface1;
036: import org.apache.harmony.rmi.test.TestObject;
037:
038: import junit.framework.Test;
039: import junit.framework.TestSuite;
040:
041: /**
042: * Unit test for RMI Distributed Garbage Collector.
043: *
044: * @author Mikhail A. Markov, Vasily Zakharov
045: * @version $Revision: 1.1.2.4 $
046: */
047: public class DGCTest extends RMITestBase {
048:
049: /**
050: * String to identify that a registry process must be started.
051: */
052: private static final String REGISTRY_ID = "registry";
053:
054: /**
055: * String to identify that a server process for test 0 must be started.
056: */
057: private static final String SERVER_ID_0 = "server0";
058:
059: /**
060: * String to identify that a client process for test 0 must be started.
061: */
062: private static final String CLIENT_ID_0 = "client0";
063:
064: /**
065: * String to identify that a server process for test 3 must be started.
066: */
067: private static final String SERVER_ID_3 = "server3";
068:
069: /**
070: * Garbage collector tick (in milliseconds).
071: */
072: private static final int GC_TICK = 10000;
073:
074: /**
075: * No-arg constructor to enable serialization.
076: */
077: public DGCTest() {
078: super ();
079: }
080:
081: /**
082: * Constructs this test case with the given name.
083: *
084: * @param name
085: * Name for this test case.
086: */
087: public DGCTest(String name) {
088: super (name);
089: }
090:
091: /**
092: * Test0
093: *
094: * @throws Exception
095: * If some error occurs.
096: */
097: public void test0() throws Exception {
098: System.out.println("test0 starting");
099: test0(CONFIG_DIRECT_SOCKET, true, CONFIG_DIRECT_SOCKET, true,
100: CONFIG_DIRECT_SOCKET, true);
101: System.out.println("test0 complete");
102: }
103:
104: /**
105: * Test3
106: *
107: * @throws Exception
108: * If some error occurs.
109: */
110: public void test3() throws Exception {
111: System.out.println("test3 starting");
112: test3(CONFIG_DIRECT_SOCKET, true);
113: System.out.println("test3 complete");
114: }
115:
116: /**
117: * Test0
118: *
119: * @param configServer
120: * Server configuration to set environment for.
121: *
122: * @param endorsedServer
123: * If endorsedDirs and bootClassPath should be propagated to
124: * server.
125: *
126: * @param configClient
127: * Client configuration to set environment for.
128: *
129: * @param endorsedClient
130: * If endorsedDirs and bootClassPath should be propagated to
131: * client.
132: *
133: * @param configRegistry
134: * Registry configuration to set environment for.
135: *
136: * @param endorsedRegistry
137: * If endorsedDirs and bootClassPath should be propagated to
138: * registry.
139: *
140: * @throws Exception
141: * If some error occurs.
142: */
143: private void test0(int configServer, boolean endorsedServer,
144: int configClient, boolean endorsedClient,
145: int configRegistry, boolean endorsedRegistry)
146: throws Exception {
147: SubProcess registry = null;
148: SubProcess server = null;
149: SubProcess client = null;
150:
151: try {
152: System.out.println("test0: creating registry");
153: registry = startProcess("org.apache.harmony.rmi.DGCTest",
154: REGISTRY_ID, configRegistry, endorsedRegistry);
155: registry.pipeError();
156: System.out.println("test0: Expecting READY from registry");
157: registry.expect();
158: registry.pipeInput();
159:
160: System.out.println("test0: starting server");
161: server = startProcess("org.apache.harmony.rmi.DGCTest",
162: SERVER_ID_0, configServer, endorsedServer);
163: server.pipeError();
164: server.closeOutput();
165: System.out.println("test0: Expecting READY from server");
166: server.expect();
167:
168: System.out.println("test0: starting client");
169: client = startProcess("org.apache.harmony.rmi.DGCTest",
170: CLIENT_ID_0, configClient, endorsedClient);
171: client.pipeInput();
172: client.pipeError();
173: client.closeOutput();
174:
175: System.out.println("test0: Expecting STARTED from server");
176: server.expect("TestObject.test1() started");
177: server.pipeInput();
178:
179: System.out.println("test0: destroying registry");
180: registry.destroy();
181:
182: System.out.println("test0: destroying client");
183: client.destroy();
184:
185: System.out.println("test0: waiting for server to return");
186: assertEquals("Test server return", 0, server.waitFor());
187: } finally {
188: if (registry != null) {
189: registry.destroy();
190: }
191: if (client != null) {
192: client.destroy();
193: }
194: if (server != null) {
195: server.destroy();
196: }
197: }
198: }
199:
200: /**
201: * Test3
202: *
203: * @param config
204: * Configuration to set environment for.
205: *
206: * @param endorsed
207: * If endorsedDirs and bootClassPath should be propagated to
208: * test VM.
209: *
210: * @throws Exception
211: * If some error occurs.
212: */
213: private void test3(int config, boolean endorsed) throws Exception {
214: SubProcess server = null;
215:
216: try {
217: System.out.println("test3: starting server");
218: server = startProcess("org.apache.harmony.rmi.DGCTest",
219: SERVER_ID_3, config, endorsed);
220: server.pipeInput();
221: server.pipeError();
222: server.closeOutput();
223: assertEquals("Test server return", 0, server.waitFor());
224: } finally {
225: if (server != null) {
226: server.destroy();
227: }
228: }
229: }
230:
231: /**
232: * Runs registry process, wait for READY and exits with export
233: * or stays on if input stream is closed.
234: *
235: * @param config
236: * Number of the configuration to run.
237: *
238: * @throws Exception
239: * If some error occurs.
240: */
241: private void runRegistry(int config) throws Exception {
242: System.err.println("Registry starting");
243: System.setSecurityManager(new RMISecurityManager());
244: setEnvironmentForConfig(config);
245: Registry reg = LocateRegistry.createRegistry(REGISTRY_PORT);
246: System.err
247: .println("Registry initialized, telling READY to parent");
248: SubProcess.tellOut();
249: System.err.println("Expecting READY from parent");
250:
251: try {
252: SubProcess.expectIn();
253: UnicastRemoteObject.unexportObject(reg, true);
254: System.err.println("Registry exiting");
255: } catch (EOFException e) {
256: System.err
257: .println("EOFException caught, registry stays on");
258: }
259: }
260:
261: /**
262: * Runs test server process.
263: *
264: * @param config
265: * Number of the configuration to run.
266: *
267: * @throws Exception
268: * If some error occurs.
269: */
270: private void runTestServer0(int config) throws Exception {
271: System.err.println("Test server started");
272: System.setSecurityManager(new RMISecurityManager());
273: setEnvironmentForConfig(config);
274: MyRemoteInterface1 obj = new TestObject();
275: UnicastRemoteObject.exportObject(obj, CUSTOM_PORT_4);
276: LocateRegistry.getRegistry().rebind(TEST_STRING_1,
277: RemoteObject.toStub(obj));
278: GCThread.create();
279: System.err
280: .println("Test server initialized, telling READY to parent");
281: SubProcess.tellOut();
282: }
283:
284: /**
285: * Runs test client process.
286: *
287: * @param config
288: * Number of the configuration to run.
289: *
290: * @throws Exception
291: * If some error occurs.
292: */
293: private void runTestClient0(int config) throws Exception {
294: System.err.println("Test client started");
295: System.setSecurityManager(new RMISecurityManager());
296: setEnvironmentForConfig(config);
297: Registry reg = LocateRegistry.getRegistry();
298: MyRemoteInterface1 mri = (MyRemoteInterface1) reg
299: .lookup(TEST_STRING_1);
300: mri.test1();
301: System.err.println("Test client completed");
302: }
303:
304: /**
305: * Runs test server process.
306: *
307: * @param config
308: * Number of the configuration to run.
309: *
310: * @throws Exception
311: * If some error occurs.
312: */
313: private void runTestServer3(int config) throws Exception {
314: System.err.println("Test server started");
315: System.setSecurityManager(new RMISecurityManager());
316: setEnvironmentForConfig(config);
317: Registry reg = LocateRegistry.createRegistry(REGISTRY_PORT);
318: TestObject obj = new TestObject();
319: UnicastRemoteObject.exportObject(obj, REGISTRY_PORT);
320: obj = null;
321: System.gc();
322: System.err.println("Test server exiting");
323: }
324:
325: /**
326: * Calls system garbage collector ({@link System#gc()}) periodically.
327: */
328: static class GCThread extends Thread {
329:
330: /**
331: * Creates this thread and marks it as daemon thread.
332: */
333: public GCThread() {
334: super ();
335: setDaemon(true);
336: }
337:
338: /**
339: * {@inheritDoc}
340: */
341: public void run() {
342: while (true) {
343: try {
344: sleep(GC_TICK);
345: } catch (InterruptedException e) {
346: }
347:
348: System.out.println("GCThread: Calling GC");
349: System.gc();
350: }
351: }
352:
353: /**
354: * Creates new GCThread thread.
355: */
356: public static void create() {
357: new GCThread().start();
358: }
359: }
360:
361: /**
362: * Returns test suite for this class.
363: *
364: * @return Test suite for this class.
365: */
366: public static Test suite() {
367: return new TestSuite(DGCTest.class);
368: }
369:
370: /**
371: * Starts the testing from the command line.
372: *
373: * @param args
374: * Command line parameters.
375: */
376: public static void main(String args[]) {
377: switch (args.length) {
378: case 0:
379: // Run tests normally.
380: junit.textui.TestRunner.run(suite());
381: break;
382: case 2:
383: // Run registry, test server or client process.
384: int config = new Integer(args[1]).intValue();
385: String param = args[0].intern();
386: DGCTest dgcTest = new DGCTest();
387:
388: try {
389: if (param == REGISTRY_ID) {
390: dgcTest.runRegistry(config);
391: } else if (param == SERVER_ID_0) {
392: dgcTest.runTestServer0(config);
393: } else if (param == CLIENT_ID_0) {
394: dgcTest.runTestClient0(config);
395: } else if (param == SERVER_ID_3) {
396: dgcTest.runTestServer3(config);
397: } else {
398: System.err.println("Bad parameter: " + param);
399: abort();
400: }
401: } catch (Exception e) {
402: System.err.println("Child process (" + param + ", "
403: + config + ") failed: " + e);
404: e.printStackTrace();
405: abort();
406: }
407: System.err.println("Child process (" + param + ", "
408: + config + ") is terminating OK");
409: break;
410: default:
411: System.err.println("Bad number of parameters: "
412: + args.length + ", expected: 2.");
413: abort();
414: }
415: }
416: }
|