1 package org.sapia.ubik.net; 2 3 import junit.framework.TestCase; 4 5 import java.io.IOException ; 6 7 import java.net.Socket ; 8 9 import java.rmi.RemoteException ; 10 11 12 20 public class ConnectionPoolTest extends TestCase { 21 25 public ConnectionPoolTest(String arg0) { 26 super(arg0); 27 } 28 29 public void testAcquireNoMaxSize() throws Exception { 30 ConnectionPool pool = new ConnectionPool("test", 9999, 31 new TestConnectionFactory(), new DefaultClientSocketFactory(), -1); 32 33 for (int i = 0; i < 100; i++) { 34 pool.acquire(); 35 } 36 } 37 38 public void testAcquireWithMaxSize() throws Exception { 39 ConnectionPool pool = new ConnectionPool("test", 9999, 40 new TestConnectionFactory(), new DefaultClientSocketFactory(), 3); 41 42 pool.acquire(100); 43 pool.acquire(100); 44 pool.acquire(100); 45 46 try { 47 pool.acquire(100); 48 throw new Exception ("new connection should not have been created"); 49 } catch (Exception e) { 50 } 52 53 super.assertEquals(3, pool.getCount()); 54 } 55 56 public void testRelease() throws Exception { 57 ConnectionPool pool = new ConnectionPool("test", 9999, 58 new TestConnectionFactory(), new DefaultClientSocketFactory(), 3); 59 60 Connection conn; 61 pool.acquire(100); 62 pool.acquire(100); 63 conn = pool.acquire(100); 64 pool.release(conn); 65 pool.acquire(100); 66 super.assertEquals(3, pool.getCount()); 67 } 68 69 72 static class TestConnection implements Connection { 73 TCPAddress _id = new TCPAddress("test", 8888); 74 75 78 public void close() { 79 } 80 81 84 public ServerAddress getServerAddress() { 85 return _id; 86 } 87 88 91 public Object receive() 92 throws IOException , ClassNotFoundException , RemoteException { 93 return "ACK"; 94 } 95 96 99 public void send(Object o) throws IOException , RemoteException { 100 } 101 } 102 103 static class TestConnectionFactory implements ConnectionFactory { 104 107 public Connection newConnection(Socket sock) 108 throws IOException , UnsupportedOperationException { 109 return new TestConnection(); 110 } 111 112 115 public Connection newConnection(String host, int port) 116 throws IOException { 117 return new TestConnection(); 118 } 119 } 120 } 121 | Popular Tags |