Package org.xlightweb.test

Source Code of org.xlightweb.test.CustomWorkerpoolTest

/*
*  Copyright (c) xlightweb.org, 2008 - 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.test;


import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;


import org.junit.Assert;

import org.junit.Test;
import org.xlightweb.BadMessageException;
import org.xlightweb.FutureResponseHandler;
import org.xlightweb.GetRequest;
import org.xlightweb.HttpResponse;
import org.xlightweb.IBodyDataHandler;
import org.xlightweb.IHttpExchange;
import org.xlightweb.IHttpRequest;
import org.xlightweb.IHttpRequestHandler;
import org.xlightweb.IHttpResponse;
import org.xlightweb.IHttpResponseHandler;
import org.xlightweb.NonBlockingBodyDataSource;
import org.xlightweb.PostRequest;
import org.xlightweb.QAUtil;
import org.xlightweb.client.HttpClient;
import org.xlightweb.client.HttpClientConnection;
import org.xlightweb.server.HttpServer;
import org.xsocket.connection.Server;



/**
*
* @author grro@xlightweb.org
*/
public final class CustomWorkerpoolTest {
 
 
 
  @Test
  public void testCustomClientWorkerPool() throws Exception {
   
    final Server server = new HttpServer(new RequestHandler());
    server.start();

    MyWorkerPool wp = new MyWorkerPool();
    HttpClient httpClient = new HttpClient();
    httpClient.setWorkerpool(wp)
   
    ResponseHandler rh = new ResponseHandler(2);
    httpClient.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/"), rh);
    QAUtil.sleep(500);
    Assert.assertEquals(1, wp.getRunning());
   
    httpClient.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/"), rh);
    QAUtil.sleep(500);
    Assert.assertEquals(2, wp.getRunning());
   
    QAUtil.sleep(2500);
    Assert.assertEquals(0, wp.getRunning());

   
    httpClient.close();
    server.close();
  }
 
 
  @Test
  public void testCustomClientDataHandlerWorkerPool() throws Exception {
   
    final Server server = new HttpServer(new RequestHandler());
    server.start();

    MyWorkerPool wp = new MyWorkerPool();
    HttpClient httpClient = new HttpClient();
    httpClient.setWorkerpool(wp)
   
    ResponseHandler2 rh = new ResponseHandler2();
    httpClient.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/"), rh);
    QAUtil.sleep(500);
    Assert.assertTrue(rh.getDataHandlerThreadName().startsWith("worker#"));
   
   
    httpClient.close();
    server.close();
  }
 
 
  @Test
  public void testCustomClientConnectionWorkerpool() throws Exception {
   
    final Server server = new HttpServer(new RequestHandler());
    server.start();

    MyWorkerPool wp = new MyWorkerPool();
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    con.setWorkerpool(wp)
   
    ResponseHandler rh = new ResponseHandler(1);
    con.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/"), rh);
    QAUtil.sleep(300);
    Assert.assertEquals(1, wp.getRunning());
   
   
    con.close();
    server.close();
  }
 
 
  @Test
  public void testCustomClientConnectionDataHandlerWorkerPool() throws Exception {
   
    final Server server = new HttpServer(new RequestHandler());
    server.start();

    MyWorkerPool wp = new MyWorkerPool();
    HttpClientConnection con = new HttpClientConnection("localhost", server.getLocalPort());
    con.setWorkerpool(wp)
   
    ResponseHandler2 rh = new ResponseHandler2();
    con.send(new GetRequest("http://localhost:" + server.getLocalPort() + "/"), rh);
    QAUtil.sleep(500);
    Assert.assertTrue(rh.getDataHandlerThreadName().startsWith("worker#"));
   
   
    con.close();
    server.close();
  }
 
 
 
  @Test
  public void testCustomServerWorkerPool() throws Exception {
   
    MyWorkerPool wp = new MyWorkerPool();
    RequestHandler reqHdl = new RequestHandler();
    final Server server = new HttpServer(reqHdl);
    server.setWorkerpool(wp);
    server.start();
   
    HttpClient httpClient = new HttpClient();
   
   
    FutureResponseHandler rh1 = new FutureResponseHandler();
    httpClient.send(new PostRequest("http://localhost:" + server.getLocalPort() + "/?waittimeSec=1", "text/plain", "test"), rh1);
    QAUtil.sleep(300);
    Assert.assertEquals(1, wp.getRunning());

   
    FutureResponseHandler rh2 = new FutureResponseHandler();
    httpClient.send(new PostRequest("http://localhost:" + server.getLocalPort() + "/?waittimeSec=1", "text/plain", "test"), rh2);
    QAUtil.sleep(300);
    Assert.assertEquals(2, wp.getRunning());
   
    QAUtil.sleep(1500);
    Assert.assertEquals(0, wp.getRunning());

   
    Assert.assertEquals(200, rh1.getResponse().getStatus());
    Assert.assertEquals(200, rh2.getResponse().getStatus());
   
    httpClient.close();
    server.close();
  }
 
 
 
  @Test
  public void testCustomServerDataHandlerWorkerPool() throws Exception {
   
    MyWorkerPool wp = new MyWorkerPool();
    RequestHandler2 reqHdl = new RequestHandler2();
    final Server server = new HttpServer(reqHdl);
    server.setWorkerpool(wp);
    server.start();
   
    HttpClient httpClient = new HttpClient();
   
   
    FutureResponseHandler rh1 = new FutureResponseHandler();
    httpClient.send(new PostRequest("http://localhost:" + server.getLocalPort() + "/?waittimeSec=1", "text/plain", "test"), rh1);
    QAUtil.sleep(300);
    Assert.assertTrue("thread name " + reqHdl.getDataHandlerThreadName() + " does not starts with worker#", reqHdl.getDataHandlerThreadName().startsWith("worker#"));

    Assert.assertEquals(200, rh1.getResponse().getStatus());
   
    httpClient.close();
    server.close();
  }
 
 
  private static final class ResponseHandler implements IHttpResponseHandler {
   
    private int waittimeSec = 0;
   
    public ResponseHandler(int waittimeSec) {
      this.waittimeSec = waittimeSec;
    }

    public void onResponse(IHttpResponse response) throws IOException {
      QAUtil.sleep(waittimeSec * 1000);
    }
   
    public void onException(IOException ioe) throws IOException {
    }
  }
 
 
 

  private static final class ResponseHandler2 implements IHttpResponseHandler {
   
    private String dataHandlerThreadName = "unset";


    public void onResponse(IHttpResponse response) throws IOException {
      IBodyDataHandler bh = new IBodyDataHandler() {
        public boolean onData(NonBlockingBodyDataSource bodyDataSource) throws BufferUnderflowException {
          dataHandlerThreadName = Thread.currentThread().getName();
          return true;
        }
      };
      response.getNonBlockingBody().setDataHandler(bh);

    }
   
    public void onException(IOException ioe) throws IOException {
    }
   
    String getDataHandlerThreadName() {
      return dataHandlerThreadName;
    }
  }
 
 
 
 
  private static final class RequestHandler implements IHttpRequestHandler {
   

    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {

      IHttpRequest request = exchange.getRequest();
      int waittimeSec = request.getIntParameter("waittimeSec", 0);
      QAUtil.sleep(waittimeSec * 1000);
      exchange.send(new HttpResponse("OK"));
    }
  }
 
 

  private static final class RequestHandler2 implements IHttpRequestHandler {
   
    private String dataHandlerThreadName = "unset";

    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {

      IHttpRequest request = exchange.getRequest();
     
      IBodyDataHandler bh = new IBodyDataHandler() {
        public boolean onData(NonBlockingBodyDataSource bodyDataSource) throws BufferUnderflowException {
          dataHandlerThreadName = Thread.currentThread().getName();
          return true;
        }
      };
      request.getNonBlockingBody().setDataHandler(bh);
     
      exchange.send(new HttpResponse("OK"));
    }
   
   
    String getDataHandlerThreadName() {
      return dataHandlerThreadName;
    }
  }
 
 
  private static final class MyWorkerPool implements Executor {
   
    private AtomicInteger running = new AtomicInteger(0);
   
   
    public void execute(final Runnable command) {
     
      Thread t = new Thread() {
       
        @Override
        public void run() {
          try {
            running.incrementAndGet();
            command.run();
          } finally {
            running.decrementAndGet();
          }
        }
      };
      t.setName("worker#" + System.currentTimeMillis());
      t.start();
    }
   
    int getRunning() {
      return running.get();
    }
  }
}
TOP

Related Classes of org.xlightweb.test.CustomWorkerpoolTest

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.