📄 endpointapitest.java
字号:
/* * Copyright (c) 2001 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. * * $Id: EndpointApiTest.java,v 1.7 2004/05/24 18:34:57 jice Exp $ */package net.jxta.impl.endpoint;import junit.framework.*;import net.jxta.peergroup.*;import net.jxta.endpoint.*;import java.io.IOException;public class EndpointApiTest extends TestCase implements EndpointListener, MessengerEventListener, OutgoingMessageEventListener { static PeerGroup pg; boolean hasMessage = false; boolean hasMessenger = false; boolean msgSent = false; int msgrCounter = 0; private synchronized boolean waitForMessage() { try { if (!hasMessage) { wait(10000); } } catch (InterruptedException ie) { } if (! hasMessage) return false; hasMessage = false; return true; } public void processIncomingMessage(Message message, EndpointAddress src, EndpointAddress dst) { synchronized(this) { hasMessage = true; notify(); } } private synchronized void clearMsgrCounter() { msgrCounter = 0; } private synchronized int getMsgrCounter() { return msgrCounter; } private synchronized boolean waitForMessenger() { try { if (!hasMessenger) { wait(10000); } } catch (InterruptedException ie) { } if (! hasMessenger) return false; hasMessenger = false; return true; } public boolean messengerReady(MessengerEvent evt) { if (evt.getMessenger() == null) { return true; } synchronized(this) { hasMessenger = true; msgrCounter++; notify(); } return true; } private synchronized boolean waitForMessageSent() { try { if (!msgSent) { wait(10000); } } catch (InterruptedException ie) { } if (! msgSent) return false; msgSent = false; return true; } public void messageSendFailed(OutgoingMessageEvent evt) { synchronized(this) { msgSent = true; notify(); } } public void messageSendSucceeded(OutgoingMessageEvent evt) { synchronized(this) { msgSent = true; notify(); } } public EndpointApiTest(java.lang.String testName) throws net.jxta.exception.PeerGroupException { super(testName); System.setProperty("net.jxta.tls.password", "password"); System.setProperty("net.jxta.tls.principal", "password"); synchronized( EndpointApiTest.class ) { if( null == pg ) pg = PeerGroupFactory.newNetPeerGroup(); } } public void testGetMessenger() { try { Thread.sleep(5000); } catch (Exception e) { } EndpointService endp = endp = pg.getEndpointService(); // Remove listener from previous test. endp.removeIncomingMessageListener("EndpointApiTest", "0"); if ( endp.addIncomingMessageListener(this, "EndpointApiTest", "0") == false ) { fail("Could not add listener"); } // AsyncTest EndpointAddress localAddr = new EndpointAddress( "jxta", pg.getPeerID().getUniqueValue().toString(),"EndpointApiTest", "0"); Messenger m = endp.getMessengerImmediate(localAddr, null); if (m == null) { fail("could not get tcp messenger to local peer...tcp is on ?"); } try { m.sendMessageB(new Message(), null, null); } catch(IOException ioe) { fail("Cannot send messages to unresolved messenger"); } int n = 0; while ((m.getState() & Messenger.RESOLVED) == 0) { try { Thread.sleep(100); } catch(InterruptedException ie) { } if (n++ > 10) { break; } } while ((m.getState() & Messenger.RESOLVED) == 0) { fail("could not resolve immediate messenger to local peer"); } if (! waitForMessage()) { try { m.sendMessageB(new Message(), null, null); } catch(IOException ioe) { fail("messenger resolved automatically, but cannot send messages"); } if (!waitForMessage()) { fail("messenger resolved automatically, but messages get lost after that"); } fail("messenger resolved automatically, but initial message was lost"); } // Leak test for (int i=0; i< 1000000; i++) { m = endp.getMessengerImmediate(localAddr, null); } m = null; // Bottleneck test for (int i=0; i< 1000000; i++) { EndpointAddress changingSvc = new EndpointAddress( "jxta", pg.getPeerID().getUniqueValue().toString(),"EndpointApiTest", ""+i); m = endp.getMessengerImmediate(changingSvc, null); } m = null; } public void testGetMessengerListener() { try { Thread.sleep(5000); } catch (Exception e) { } EndpointService endp = endp = pg.getEndpointService(); // Listener legacy api test. EndpointAddress localAddr = new EndpointAddress( "jxta", pg.getPeerID().getUniqueValue().toString(),"EndpointApiTest", "0"); clearMsgrCounter(); endp.getMessenger(this, localAddr, null); endp.getMessenger(this, localAddr, null); endp.getMessenger(this, localAddr, null); while (getMsgrCounter() != 3) { if (! waitForMessenger()) { fail("could not get messenger via listener. got only " + getMsgrCounter()); } } } public void testSendMessageListener() { try { Thread.sleep(5000); } catch (Exception e) { } EndpointService endp = endp = pg.getEndpointService(); // Remove listener from previous test. endp.removeIncomingMessageListener("EndpointApiTest", "0"); if ( endp.addIncomingMessageListener(this, "EndpointApiTest", "0") == false ) { fail("Could not add listener"); } // AsyncTest EndpointAddress localAddr = new EndpointAddress( "jxta", pg.getPeerID().getUniqueValue().toString(),"EndpointApiTest", "0"); Messenger m = endp.getMessengerImmediate(localAddr, null); if (m == null) { fail("could not get tcp messenger to local peer...tcp is on ?"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -