Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/rmi/rmic/newrmic/equivalence/AppleUserImpl.java
38867 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.rmi.RemoteException;24import java.rmi.Naming;25import java.rmi.server.UnicastRemoteObject;26import java.rmi.registry.LocateRegistry;27import java.rmi.registry.Registry;28import java.util.Random;29import java.util.ArrayList;30import java.util.Date;31import java.util.logging.Logger;32import java.util.logging.Level;3334/**35* The AppleUserImpl class implements the behavior of the remote36* "apple user" objects exported by the server. The application server37* passes each of its remote "apple" objects to an apple user, and an38* AppleUserThread is created for each apple.39*/40public class AppleUserImpl41extends UnicastRemoteObject42implements AppleUser43{44private static Logger logger = Logger.getLogger("reliability.appleuser");45private static int threadNum = 0;46private static long testDuration = 0;47private static int maxLevel = 7;48private static Thread server = null;49private static Exception status = null;50private static Random random = new Random();5152public AppleUserImpl() throws RemoteException {53}5455/**56* Allows the other server process to indicate that it is ready57* to start "juicing".58*/59public synchronized void startTest() throws RemoteException {60this.notifyAll();61}6263/**64* Allows the other server process to report an exception to this65* process and thereby terminate the test.66*/67public void reportException(Exception status) throws RemoteException {68synchronized (AppleUserImpl.class) {69this.status = status;70AppleUserImpl.class.notifyAll();71}72}7374/**75* "Use" supplied apple object. Create an AppleUserThread to76* stress it out.77*/78public synchronized void useApple(Apple apple) throws RemoteException {79String threadName = Thread.currentThread().getName();80logger.log(Level.FINEST,81threadName + ": AppleUserImpl.useApple(): BEGIN");8283AppleUserThread t =84new AppleUserThread("AppleUserThread-" + (++threadNum), apple);85t.start();8687logger.log(Level.FINEST,88threadName + ": AppleUserImpl.useApple(): END");89}9091/**92* The AppleUserThread class repeatedly invokes calls on its associated93* Apple object to stress the RMI system.94*/95class AppleUserThread extends Thread {9697Apple apple;9899public AppleUserThread(String name, Apple apple) {100super(name);101this.apple = apple;102}103104public void run() {105int orangeNum = 0;106long stopTime = System.currentTimeMillis() + testDuration;107Logger logger = Logger.getLogger("reliability.appleuserthread");108109try {110do { // loop until stopTime is reached111112/*113* Notify apple with some apple events. This tests114* serialization of arrays.115*/116int numEvents = Math.abs(random.nextInt() % 5);117AppleEvent[] events = new AppleEvent[numEvents];118for (int i = 0; i < events.length; ++ i) {119events[i] = new AppleEvent(orangeNum % 3);120}121apple.notify(events);122123/*124* Request a new orange object be created in125* the application server.126*/127Orange orange = apple.newOrange(128"Orange(" + getName() + ")-" + (++orangeNum));129130/*131* Create a large message of random ints to pass to orange.132*/133int msgLength = 1000 + Math.abs(random.nextInt() % 3000);134int[] message = new int[msgLength];135for (int i = 0; i < message.length; ++ i) {136message[i] = random.nextInt();137}138139/*140* Invoke recursive call on the orange. Base case141* of recursion inverts messgage.142*/143OrangeEchoImpl echo = new OrangeEchoImpl(144"OrangeEcho(" + getName() + ")-" + orangeNum);145int[] response = orange.recurse(echo, message,1462 + Math.abs(random.nextInt() % (maxLevel + 1)));147148/*149* Verify message was properly inverted and not corrupted150* through all the recursive method invocations.151*/152if (response.length != message.length) {153throw new RuntimeException(154"ERROR: CORRUPTED RESPONSE: " +155"wrong length of returned array " + "(should be " +156message.length + ", is " + response.length + ")");157}158for (int i = 0; i < message.length; ++ i) {159if (~message[i] != response[i]) {160throw new RuntimeException(161"ERROR: CORRUPTED RESPONSE: " +162"at element " + i + "/" + message.length +163" of returned array (should be " +164Integer.toHexString(~message[i]) + ", is " +165Integer.toHexString(response[i]) + ")");166}167}168169try {170Thread.sleep(Math.abs(random.nextInt() % 10) * 1000);171} catch (InterruptedException e) {172}173174} while (System.currentTimeMillis() < stopTime);175176} catch (Exception e) {177status = e;178}179synchronized (AppleUserImpl.class) {180AppleUserImpl.class.notifyAll();181}182}183}184185private static void usage() {186System.out.println("Usage: AppleUserImpl [-hours <hours> | " +187"-seconds <seconds>]");188System.out.println(" [-maxLevel <maxLevel>]");189System.out.println(" hours The number of hours to run the juicer.");190System.out.println(" The default is 0 hours.");191System.out.println(" seconds The number of seconds to run the juicer.");192System.out.println(" The default is 0 seconds.");193System.out.println(" maxLevel The maximum number of levels to ");194System.out.println(" recurse on each call.");195System.out.println(" The default is 7 levels.");196//TestLibrary.bomb("Bad argument");197}198199/**200* Entry point for the "juicer" server process. Create and export201* an apple user implementation in an rmiregistry running on localhost.202*/203public static void main(String[] args)204205{206//TestLibrary.suggestSecurityManager("java.rmi.RMISecurityManager");207long startTime = 0;208String durationString = null;209210// parse command line args211try {212for (int i = 0; i < args.length ; i++ ) {213String arg = args[i];214if (arg.equals("-hours")) {215if (durationString != null) {216usage();217}218i++;219int hours = Integer.parseInt(args[i]);220durationString = hours + " hours";221testDuration = hours * 60 * 60 * 1000;222} else if (arg.equals("-seconds")) {223if (durationString != null) {224usage();225}226i++;227long seconds = Long.parseLong(args[i]);228durationString = seconds + " seconds";229testDuration = seconds * 1000;230} else if (arg.equals("-maxLevel")) {231i++;232maxLevel = Integer.parseInt(args[i]);233} else {234usage();235}236}237if (durationString == null) {238durationString = testDuration + " milliseconds";239}240} catch (Throwable t) {241usage();242}243244AppleUserImpl user = null;245try {246user = new AppleUserImpl();247} catch (RemoteException e) {248//TestLibrary.bomb("Failed to create AppleUser", e);249}250251synchronized (user) {252int port = -1;253// create new registry and bind new AppleUserImpl in registry254try {255Registry registry = TestLibrary.createRegistryOnUnusedPort();256port = TestLibrary.getRegistryPort(registry);257Naming.rebind("rmi://localhost:" + port + "/AppleUser",user);258} catch (RemoteException e) {259//TestLibrary.bomb("Failed to bind AppleUser", e);260} catch (java.net.MalformedURLException e) {261//TestLibrary.bomb("Failed to bind AppleUser", e);262}263264// start the other server if available265try {266Class app = Class.forName("ApplicationServer");267java.lang.reflect.Constructor appConstructor =268app.getDeclaredConstructor(new Class[] {Integer.TYPE});269server = new Thread((Runnable) appConstructor.newInstance(port));270} catch (ClassNotFoundException e) {271// assume the other server is running in a separate process272logger.log(Level.INFO, "Application server must be " +273"started in separate process");274} catch (Exception ie) {275//TestLibrary.bomb("Could not instantiate server", ie);276}277278// wait for other server to call startTest method279try {280logger.log(Level.INFO, "Waiting for application server " +281"process to start");282user.wait();283} catch (InterruptedException ie) {284//TestLibrary.bomb("AppleUserImpl interrupted", ie);285}286}287288startTime = System.currentTimeMillis();289logger.log(Level.INFO, "Test starting");290291// wait for exception to be reported or first thread to complete292try {293logger.log(Level.INFO, "Waiting " + durationString + " for " +294"test to complete or exception to be thrown");295296synchronized (AppleUserImpl.class) {297AppleUserImpl.class.wait();298}299300if (status != null) {301//TestLibrary.bomb("juicer server reported an exception", status);302} else {303logger.log(Level.INFO, "TEST PASSED");304}305} catch (Exception e) {306logger.log(Level.INFO, "TEST FAILED");307//TestLibrary.bomb("unexpected exception", e);308} finally {309logger.log(Level.INFO, "Test finished");310long actualDuration = System.currentTimeMillis() - startTime;311logger.log(Level.INFO, "Test duration was " +312(actualDuration/1000) + " seconds " +313"(" + (actualDuration/3600000) + " hours)");314}315}316}317318319