1 5 package com.tc.net.protocol.tcm; 6 7 import com.tc.net.TCSocketAddress; 8 import com.tc.net.protocol.PlainNetworkStackHarnessFactory; 9 import com.tc.net.protocol.transport.ConnectionIDFactory; 10 import com.tc.net.protocol.transport.DefaultConnectionIdFactory; 11 import com.tc.net.protocol.transport.NullConnectionPolicy; 12 import com.tc.object.session.NullSessionManager; 13 import com.tc.object.session.SessionProvider; 14 import com.tc.util.TCTimeoutException; 15 16 import java.io.IOException ; 17 import java.net.InetAddress ; 18 import java.net.UnknownHostException ; 19 import java.util.Collections ; 20 21 import junit.framework.TestCase; 22 23 28 public class NetworkListenerTest extends TestCase { 29 30 CommunicationsManager commsMgr; 31 32 public void setUp() throws Exception { 33 super.setUp(); 34 commsMgr = new CommunicationsManagerImpl(new NullMessageMonitor(), new PlainNetworkStackHarnessFactory(), 35 new NullConnectionPolicy()); 36 } 37 38 public void tearDown() throws Exception { 39 super.tearDown(); 40 41 if (commsMgr != null) { 42 commsMgr.shutdown(); 43 } 44 } 45 46 public void testBindException() throws Exception { 47 assertTrue(commsMgr.getAllListeners().length == 0); 48 49 ConnectionIDFactory cidf = new DefaultConnectionIdFactory(); 50 SessionProvider sessionProvider = new NullSessionManager(); 51 NetworkListener lsnr = commsMgr.createListener(sessionProvider, new TCSocketAddress(0), true, cidf, false); 52 53 try { 54 lsnr.start(Collections.EMPTY_SET); 55 } catch (IOException ioe) { 56 fail(ioe.getMessage()); 57 } 58 59 NetworkListener lsnr2 = commsMgr.createListener(sessionProvider, new TCSocketAddress(lsnr.getBindPort()), true, 60 cidf, false); 61 try { 62 lsnr2.start(Collections.EMPTY_SET); 63 fail(); 64 } catch (IOException ioe) { 65 } 67 68 assertTrue(commsMgr.getAllListeners().length == 1); 69 70 lsnr.stop(5000); 71 72 assertTrue(commsMgr.getAllListeners().length == 0); 73 } 74 75 public void testMany() throws UnknownHostException , TCTimeoutException { 76 assertTrue(commsMgr.getAllListeners().length == 0); 77 78 final int cnt = 20; 79 NetworkListener[] listeners = new NetworkListener[cnt]; 80 81 for (int i = 0; i < cnt; i++) { 82 NetworkListener lsnr = commsMgr.createListener(new NullSessionManager(), new TCSocketAddress(InetAddress 83 .getByName("127.0.0.1"), 0), true, new DefaultConnectionIdFactory()); 84 85 try { 86 lsnr.start(Collections.EMPTY_SET); 87 listeners[i] = lsnr; 88 } catch (IOException ioe) { 89 fail(ioe.getMessage()); 90 } 91 } 92 93 assertTrue(commsMgr.getAllListeners().length == cnt); 94 95 for (int i = 0; i < cnt; i++) { 96 98 listeners[i].stop(5000); 99 listeners[i].stop(5000); 100 } 101 102 assertTrue(commsMgr.getAllListeners().length == 0); 103 } 104 105 } 106 | Popular Tags |