1 /*------------------------------------------------------------------------------
  2 Name:      TestCorbaThreads.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.test.qos;
  7 
  8 import org.xmlBlaster.client.protocol.I_CallbackExtended;
  9 import org.xmlBlaster.client.protocol.corba.CorbaConnection;
 10 import org.xmlBlaster.client.protocol.corba.CorbaCallbackServer;
 11 import org.xmlBlaster.client.protocol.I_CallbackServer;
 12 import org.xmlBlaster.client.qos.ConnectQos;
 13 import org.xmlBlaster.util.Global;
 14 import org.xmlBlaster.util.ThreadLister;
 15 import org.xmlBlaster.client.key.UpdateKey;
 16 import org.xmlBlaster.client.qos.UpdateQos;
 17 import org.xmlBlaster.util.EmbeddedXmlBlaster;
 18 import org.xmlBlaster.util.protocol.corba.OrbInstanceFactory;
 19 import org.xmlBlaster.util.qos.address.CallbackAddress;
 20 import org.xmlBlaster.util.XmlBlasterException;
 21 
 22 import org.omg.CORBA.ORB;
 23 import org.omg.PortableServer.POA;
 24 
 25 import org.xmlBlaster.test.Util;
 26 
 27 import java.util.logging.Logger;
 28 import java.util.logging.Level;
 29 
 30 import junit.framework.*;
 31 
 32 
 33 /**
 34  * This client tests the number of threads opened and cleaned up by JacORB corba library. 
 35  * <p>
 36  * Invoke examples:<br />
 37  * <pre>
 38  *    java junit.textui.TestRunner -noloading org.xmlBlaster.test.qos.TestCorbaThreads
 39  *
 40  *    java junit.swingui.TestRunner -noloading org.xmlBlaster.test.qos.TestCorbaThreads
 41  * </pre>
 42  */
 43 public class TestCorbaThreads extends TestCase implements I_CallbackExtended
 44 {
 45    private Global glob;
 46    private static Logger log = Logger.getLogger(TestCorbaThreads.class.getName());
 47    private EmbeddedXmlBlaster serverThread;
 48    private final String loginName = "TestCorbaThreads";
 49    private CorbaConnection corbaConnection = null;
 50    private I_CallbackServer cbServer = null;
 51    private String[] args = { "-protocol", "IOR" };
 52 
 53    /**
 54     * Constructs the TestCorbaThreads object.
 55     * <p />
 56     * @param testName  The name used in the test suite
 57     */
 58    public TestCorbaThreads(Global glob, String testName) {
 59        super(testName);
 60        this.glob = glob.getClone(args);
 61    }
 62 
 63    /**
 64     * Test ORB and POA creation and orb.shutdown()
 65     */
 66    public void testJacORB() {
 67       Util.gc(2);
 68       int threadsBefore = ThreadLister.countThreads();
 69       for (int ii=0; ii<20; ii++) {
 70          System.out.println("Hit a key for ORB #" + ii + "/20");
 71          try { System.in.read(); } catch(java.io.IOException e) {}
 72          ORB orb = OrbInstanceFactory.createOrbInstance(glob, new String[0], null, new CallbackAddress(glob));
 73          try {
 74             POA rootPOA = org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
 75             rootPOA.the_POAManager().activate();
 76          }
 77          catch (Throwable e) {
 78             e.printStackTrace();
 79             System.out.println("ERROR: " + e.toString());
 80          }
 81          // Without orb.shutdown we use 2 threads for each loop!
 82          orb.shutdown(true);
 83       }
 84       Util.gc(2);
 85       assertEquals("JacORB has a thread leak", threadsBefore, ThreadLister.countThreads());
 86    }
 87 
 88    protected void setUp() {
 89       glob.init(Util.getOtherServerPorts(8116));
 90       String[] args = { "-protocol", "IOR",
 91                         "-ProtocolPlugin[IOR][1.0]", "org.xmlBlaster.protocol.corba.CorbaDriver",
 92                         "-CbProtocolPlugin[IOR][1.0]", "org.xmlBlaster.protocol.corba.CallbackCorbaDriver" };
 93       glob.init(args);
 94       serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
 95       log.info("XmlBlaster is ready for testing on bootstrapPort 8116");
 96    }
 97 
 98    /**
 99     * Login. 
100     * <p />
101     * Creates a CORBA connection and does a login.<br />
102     * - One connection for the sender client<br />
103     * - One connection for the receiver client
104     * - One connection for the receiver2 client
105     */
106    private void login() {
107       try {
108          String passwd = "secret";
109 
110          cbServer = new CorbaCallbackServer();
111          CallbackAddress cba = new CallbackAddress(this.glob);
112          cbServer.initialize(this.glob, loginName, cba, this);
113 
114          corbaConnection = new CorbaConnection();
115          corbaConnection.init(glob, null);
116          //cbServer = new CorbaCallbackServer(this.glob, loginName, this, corbaConnection.getOrb());
117          ConnectQos connectQos = new ConnectQos(glob, loginName, passwd);
118          corbaConnection.connect(connectQos.toXml());
119       }
120       catch (Exception e) {
121           log.severe(e.toString());
122           e.printStackTrace();
123       }
124    }
125 
126    protected void tearDown() {
127       EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
128       this.serverThread = null;
129       // reset to default server bootstrapPort (necessary if other tests follow in the same JVM).
130       Util.resetPorts(glob);
131       log.info("Ports reset to default: " + glob.getProperty().toXml());
132    }
133 
134    /**
135     * cleaning up .... logout
136     */
137    private void logout() {
138       if (corbaConnection != null) {
139          try { Thread.sleep(200L); } catch( InterruptedException i) {}   // Wait 200 milli seconds, until all updates are processed ...
140          corbaConnection.disconnect(null);
141          corbaConnection = null;
142          System.gc();
143       }
144       if (cbServer != null) {
145          try { cbServer.shutdown(); } catch(Exception e) { log.severe("shutdownCb(): " + e.toString()); }
146          cbServer = null;
147          System.gc();
148       }
149    }
150 
151 
152    /**
153     * TEST: Send a message to one destination
154     * <p />
155     * The returned subscribeOid is checked
156     */
157    public void testThread() {
158       log.info("Testing thread consume on multiple login/logouts, used threads before any login=" + ThreadLister.countThreads());
159       ThreadLister.listAllThreads(System.out);
160       int threadsBefore = 0;
161 
162       for (int ii=0; ii<5; ii++) {
163          log.info("Testing login/logout no = " + ii);
164          login();
165          logout();
166          if (ii==0) {
167             Util.gc(2);
168             threadsBefore = ThreadLister.countThreads();
169             log.info("Testing thread consume on multiple login/logouts, used threads after first login=" + threadsBefore);
170             ThreadLister.listAllThreads(System.out);
171          }
172       }
173       Util.gc(2);
174       ThreadLister.listAllThreads(System.out);
175       int threadsAfter = ThreadLister.countThreads();
176       log.info("Currently used threads after 5 login/logout=" + threadsAfter);
177       assertTrue("We have a thread leak, threadsBefore=" + threadsBefore +
178                  " threadsAfter=" + threadsAfter, threadsAfter <= threadsBefore);
179    }
180 
181 
182    /**
183     * These update() methods are enforced by I_CallbackExtended. 
184     */
185    public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
186       if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
187       return "";
188    }
189    public String update(String cbSessionId, String updateKeyLiteral, byte[] content, String updateQosLiteral) {
190       if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
191       return "";
192    }
193    public String[] update(String cbSessionId, org.xmlBlaster.util.MsgUnitRaw[] msgUnitArr) {
194       if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
195       String[] retArr = new String[msgUnitArr.length];
196       for (int ii=0; ii<retArr.length; ii++) retArr[ii] = "";
197       return retArr;
198    }
199    public void updateOneway(String cbSessionId, String updateKeyLiteral, byte[] content, String updateQosLiteral) {
200       if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
201    }
202    public void updateOneway(String cbSessionId, org.xmlBlaster.util.MsgUnitRaw[] msgUnitArr) {
203       if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
204    }
205 
206    public void lostConnection(XmlBlasterException xmlBlasterException) {
207       if (log.isLoggable(Level.FINER)) log.finer("Lost connection ...");
208    }
209 
210    /**
211     * Method is used by TestRunner to load these tests
212     */
213    public static Test suite() {
214        TestSuite suite= new TestSuite();
215        suite.addTest(new TestCorbaThreads(new Global(), "testThread"));
216        return suite;
217    }
218 
219    /**
220     * Invoke: java org.xmlBlaster.test.qos.TestCorbaThreads
221     * <p />
222     * @deprecated Use the TestRunner from the testsuite to run it:<p />
223     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestCorbaThreads</pre>
224     */
225    public static void main(String args[]) {
226       Global glob = new Global(); // initializes args etc.
227       if (glob.init(args) < 0) {
228          System.out.println("Wrong params");
229          return;
230       }
231 
232       TestCorbaThreads testSub = new TestCorbaThreads(glob, "TestCorbaThreads");
233       testSub.setUp();
234       testSub.testJacORB();
235       testSub.testThread();
236       testSub.tearDown();
237    }
238 }


syntax highlighted by Code2HTML, v. 0.9.1