test/java/rmi/transport/pinLastArguments/PinLastArguments.java

Print this page




  26  * @summary Passing live remote references as part of the arguments of
  27  * a remote invocation should not cause an AssertionError (on the
  28  * second and subsequent attempts) when system assertions are enabled,
  29  * nor should it cause the references to be pinned until a subsequent
  30  * such remote invocation occurs (if the argument stream was not
  31  * released cleanly because of a marshalling failure).
  32  * @author Peter Jones
  33  *
  34  * @run main/othervm -esa PinLastArguments
  35  */
  36 
  37 import java.io.NotSerializableException;
  38 import java.lang.ref.Reference;
  39 import java.lang.ref.WeakReference;
  40 import java.rmi.MarshalException;
  41 import java.rmi.Remote;
  42 import java.rmi.RemoteException;
  43 import java.rmi.server.UnicastRemoteObject;
  44 
  45 public class PinLastArguments {

  46 
  47     public interface Ping extends Remote {
  48         void ping(Object first, Object second) throws RemoteException;
  49     }
  50 
  51     private static class PingImpl implements Ping {
  52         PingImpl() { }
  53         public void ping(Object first, Object second) {
  54             System.err.println("ping invoked: " + first + ", " + second);
  55         }



  56     }
  57 
  58     public static void main(String[] args) throws Exception {
  59         System.err.println("\nRegression test for bug 6332349\n");
  60 
  61         Ping impl = new PingImpl();
  62         Reference<?> ref = new WeakReference<Ping>(impl);
  63         try {
  64             Ping stub = (Ping) UnicastRemoteObject.exportObject(impl, 0);
  65             Object notSerializable = new Object();
  66             stub.ping(impl, null);
  67             try {
  68                 stub.ping(impl, notSerializable);
  69             } catch (MarshalException e) {
  70                 if (e.getCause() instanceof NotSerializableException) {
  71                     System.err.println("ping invocation failed as expected");
  72                 } else {
  73                     throw e;
  74                 }
  75             }
  76         } finally {
  77             UnicastRemoteObject.unexportObject(impl, true);
  78         }
  79         impl = null;
  80 

  81         System.gc();



  82 
  83         if (ref.get() != null) {
  84             throw new Error("TEST FAILED: impl not garbage collected");
  85         }
  86 
  87         System.err.println("TEST PASSED");
  88     }
  89 }


  26  * @summary Passing live remote references as part of the arguments of
  27  * a remote invocation should not cause an AssertionError (on the
  28  * second and subsequent attempts) when system assertions are enabled,
  29  * nor should it cause the references to be pinned until a subsequent
  30  * such remote invocation occurs (if the argument stream was not
  31  * released cleanly because of a marshalling failure).
  32  * @author Peter Jones
  33  *
  34  * @run main/othervm -esa PinLastArguments
  35  */
  36 
  37 import java.io.NotSerializableException;
  38 import java.lang.ref.Reference;
  39 import java.lang.ref.WeakReference;
  40 import java.rmi.MarshalException;
  41 import java.rmi.Remote;
  42 import java.rmi.RemoteException;
  43 import java.rmi.server.UnicastRemoteObject;
  44 
  45 public class PinLastArguments {
  46     private static volatile boolean finalized = false;
  47         
  48     public interface Ping extends Remote {
  49         void ping(Object first, Object second) throws RemoteException;
  50     }
  51 
  52     private static class PingImpl implements Ping {
  53         PingImpl() { }
  54         public void ping(Object first, Object second) {
  55             System.err.println("ping invoked: " + first + ", " + second);
  56         }
  57         protected void finalize() {
  58             finalized = true;
  59         }
  60     }
  61 
  62     public static void main(String[] args) throws Exception {
  63         System.err.println("\nRegression test for bug 6332349\n");
  64 
  65         Ping impl = new PingImpl();
  66         Reference<?> ref = new WeakReference<Ping>(impl);
  67         try {
  68             Ping stub = (Ping) UnicastRemoteObject.exportObject(impl, 0);
  69             Object notSerializable = new Object();
  70             stub.ping(impl, null);
  71             try {
  72                 stub.ping(impl, notSerializable);
  73             } catch (MarshalException e) {
  74                 if (e.getCause() instanceof NotSerializableException) {
  75                     System.err.println("ping invocation failed as expected");
  76                 } else {
  77                     throw e;
  78                 }
  79             }
  80         } finally {
  81             UnicastRemoteObject.unexportObject(impl, true);
  82         }
  83         impl = null;
  84 
  85         while(!finalized) {
  86             System.gc();
  87             System.runFinalization();
  88             Thread.sleep(20);
  89         }
  90 
  91         if (ref.get() != null) {
  92             throw new Error("TEST FAILED: impl not garbage collected");
  93         }
  94 
  95         System.err.println("TEST PASSED");
  96     }
  97 }