/*
* TeleStax, Open Source Cloud Communications
* Copyright 2011-2015, Telestax Inc and individual contributors
* by the @authors tag.
*
* This program is free software: you can redistribute it and/or modify
* under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package org.mobicents.tools.http.balancer;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.Semaphore;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mobicents.tools.configuration.LoadBalancerConfiguration;
import org.mobicents.tools.sip.balancer.BalancerRunner;
import org.mobicents.tools.smpp.balancer.ClientListener;
import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
/**
* @author Konstantin Nosach (kostyantyn.nosach@telestax.com)
*/
public class ChunkResponseTest
{
private static BalancerRunner balancerRunner;
private static HttpServer server;
private static HttpUser user;
@BeforeClass
public static void initialization()
{
server = new HttpServer(8080, 4444, 2222);
server.setChunkedresponse(true);
server.start();
balancerRunner = new BalancerRunner();
LoadBalancerConfiguration lbConfig = new LoadBalancerConfiguration();
lbConfig.getSipConfiguration().getInternalLegConfiguration().setTcpPort(5065);
lbConfig.getSipConfiguration().getExternalLegConfiguration().setTcpPort(5060);
balancerRunner.start(lbConfig);
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
//tests http balancer
@Test
public void testHttpBalancer()
{
Locker locker = new Locker(1);
user = new HttpUser(locker);
user.start();
locker.waitForClients();
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
assertEquals(1,server.getRequstCount().get());
assertEquals(200, user.codeResponse);
}
@AfterClass
public static void finalization()
{
server.stop();
balancerRunner.stop();
}
private class HttpUser extends Thread
{
int codeResponse;
ClientListener listener;
public HttpUser(ClientListener listener)
{
this.listener = listener;
}
public void run()
{
try
{
WebConversation conversation = new WebConversation();
WebRequest request = new GetMethodWebRequest(new String("http://127.0.0.1:2080/app?fName=Konstantin&lName=Nosach"));
WebResponse response = conversation.getResponse(request);
codeResponse = response.getResponseCode();
listener.clientCompleted();
}
catch (Exception e)
{
listener.clientCompleted();
e.printStackTrace();
}
}
}
private class Locker implements ClientListener{
private Semaphore clientsSemaphore;
private Locker(int clients)
{
clientsSemaphore=new Semaphore(1-clients);
}
@Override
public void clientCompleted()
{
clientsSemaphore.release();
}
public void waitForClients()
{
try
{
clientsSemaphore.acquire();
}
catch(InterruptedException ex)
{
}
}
}
}