Package org.xlightweb.client

Source Code of org.xlightweb.client.HttpClientConnectionPoolTest$MyServlet

/*
*  Copyright (c) xlightweb.org, 2006 - 2009. All rights reserved.
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Lesser General Public
*  License as published by the Free Software Foundation; either
*  version 2.1 of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
*  Lesser General Public License for more details.
*
*  You should have received a copy of the GNU Lesser General Public
*  License along with this library; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
* The latest copy of this software may be found on http://www.xlightweb.org/
*/
package org.xlightweb.client;



import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.BufferUnderflowException;
import java.nio.channels.Channels;
import java.nio.channels.ClosedChannelException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import org.xlightweb.BlockingBodyDataSource;
import org.xlightweb.BodyDataSink;
import org.xlightweb.FutureResponseHandler;
import org.xlightweb.GetRequest;
import org.xlightweb.HttpRequestHeader;
import org.xlightweb.IHttpResponse;
import org.xlightweb.QAUtil;
import org.xlightweb.WebContainer;
import org.xlightweb.client.HttpClientConnection;
import org.xlightweb.client.HttpClientConnectionPool;
import org.xsocket.MaxReadSizeExceededException;
import org.xsocket.connection.IDataHandler;
import org.xsocket.connection.INonBlockingConnection;
import org.xsocket.connection.IServer;
import org.xsocket.connection.NonBlockingConnectionPool;
import org.xsocket.connection.Server;





/**
*
* @author grro@xlightweb.org
*/
public final class HttpClientConnectionPoolTest  {

 

  @Test
  public void testNative() throws Exception {
     
      IDataHandler dh = new IDataHandler() {
         
          public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException {
         
              connection.readStringByDelimiter("\r\n\r\n");
             
              connection.write("HTTP/1.1 200 OK\r\n" +
                               "Server: myServer\r\n" +
                               "Content-Type: text/plain; charset=UTF-8\r\n" +
                               "Content-Length: 30\r\n" +
                               "\r\n" +
                               "123456789012345678901234567890");
              return true;
          }
      };
     
      IServer server = new Server(dh);
      server.start();
     
      NonBlockingConnectionPool pool = new NonBlockingConnectionPool();
             
        for (int i = 0; i < 1000; i++) {
            HttpClientConnection httpCon = new HttpClientConnection(pool.getNonBlockingConnection("localhost", server.getLocalPort()));
            IHttpResponse resp = httpCon.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
       
            InputStream in = Channels.newInputStream(resp.getBlockingBody());
            in.close();
               
            httpCon.close();
        }
       
        pool.close();
  }
 
 
 
  @Ignore
  @Test
  public void testDefaultPersistentConnectionTimeout() throws Exception {
   
      IDataHandler dh = new IDataHandler() {
         
          public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException {
              connection.readStringByDelimiter("\r\n\r\n");

              connection.write("HTTP/1.1 200 OK\r\n" +
                               "Server: myServer\r\n" +
                               "Content-Type: text/plain; charset=UTF-8\r\n" +
                               "Content-Length: 30\r\n" +
                               "\r\n" +
                               "123456789012345678901234567890");
              return true;
          }
      };
     
      IServer server = new Server(dh);
      server.start();
     
      HttpClientConnectionPool pool = new HttpClientConnectionPool();
             
      HttpClientConnection httpCon = pool.getHttpClientConnection("localhost", server.getLocalPort());
      IHttpResponse resp = httpCon.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
       
      InputStream in = Channels.newInputStream(resp.getBlockingBody());
      in.close();


      QAUtil.sleep(2000);
     
      Assert.assertEquals(1, pool.getNumIdle());
       
      QAUtil.sleep(15000);
      Assert.assertEquals(0, pool.getNumIdle());
      Assert.assertEquals(1, pool.getNumDestroyed());
     
        pool.close();
  }

 
 

  @Test
  public void testSimple() throws Exception {
     
      IDataHandler dh = new IDataHandler() {
         
          public boolean onData(INonBlockingConnection connection) throws IOException, BufferUnderflowException, ClosedChannelException, MaxReadSizeExceededException {
         
              connection.readStringByDelimiter("\r\n\r\n");
             
              connection.write("HTTP/1.1 200 OK\r\n" +
                               "Server: myServer\r\n" +
                               "Content-Type: text/plain; charset=UTF-8\r\n" +
                               "Content-Length: 30\r\n" +
                               "\r\n" +
                               "123456789012345678901234567890");
              return true;
          }
      };
     
      IServer server = new Server(dh);
      server.start();
     
      HttpClientConnectionPool pool = new HttpClientConnectionPool();
             
        for (int i = 0; i < 1000; i++) {
            HttpClientConnection httpCon = pool.getHttpClientConnection("localhost", server.getLocalPort());
            IHttpResponse resp = httpCon.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
       
            InputStream in = Channels.newInputStream(resp.getBlockingBody());
            in.close();
            System.out.print(".");
        }
       
        pool.close();
  }

 

    @Test
    public void testStreaming() throws Exception {

        WebContainer container = new WebContainer(new MyServlet());
        container.start();
            
        HttpClientConnectionPool pool = new HttpClientConnectionPool();

       
        for (int i = 0; i < 100; i++) {
            HttpClientConnection con = pool.getHttpClientConnection("localhost", container.getLocalPort());
           
            FutureResponseHandler respHdl = new FutureResponseHandler();
            BodyDataSink dataSink = con.send(new HttpRequestHeader("POST", "http://localhost:" + container.getLocalPort() + "/test"), respHdl);
           
            dataSink.write("test");
           
            IHttpResponse response = respHdl.getResponse();
            Assert.assertEquals(200, response.getStatus());
           
            BlockingBodyDataSource dataSource = response.getBlockingBody();
            Assert.assertEquals("test", dataSource.readStringByLength(4));
           
            dataSink.write("12345");
            Assert.assertEquals("12345", dataSource.readStringByLength(5));
           
            dataSink.write("789");
            Assert.assertEquals("789", dataSource.readStringByLength(3));
           
            dataSink.close();
            con.close();   
           
            System.out.print(".");
        }
       
       
        pool.close();
        container.stop();
    }   
   
   
   
    private static final class MyServlet extends HttpServlet {

        private static final long serialVersionUID = -6112517976734846433L;

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            InputStream is = req.getInputStream();
            OutputStream os = resp.getOutputStream();
                       
            byte[] data = new byte[4096];
            int read = 0;
            do {
                read = is.read(data);
                if (read > 0) {
                    os.write(data, 0, read);
                    os.flush();
                }
            } while (read >= 0);
           
            is.close();
            os.close();
        }
       
    }  
}
TOP

Related Classes of org.xlightweb.client.HttpClientConnectionPoolTest$MyServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.