/* * 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.infinispan.configuration.global.GlobalConfigurationBuilder; 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.sip.balancer.UserBasedAlgorithm; 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 WithandWithoutInstanseIdTest { private static BalancerRunner balancerRunner; private static int numberNodes = 1; private static int numberUsers = 2; private static HttpServer [] serverArray; private static HttpUser [] userArray; private static String instanceId = "ID1f2a2222772f4195948d040a2ccc648c"; @BeforeClass public static void initialization() { serverArray = new HttpServer[numberNodes]; for(int i = 0; i < numberNodes; i++) { serverArray[i] = new HttpServer(8080+i, 4444+i, instanceId,2222); serverArray[i].start(); } balancerRunner = new BalancerRunner(); LoadBalancerConfiguration lbConfig = new LoadBalancerConfiguration(); lbConfig.getSipConfiguration().getInternalLegConfiguration().setUdpPort(5065); lbConfig.getSipConfiguration().getAlgorithmConfiguration().setAlgorithmClass(UserBasedAlgorithm.class.getName()); lbConfig.getSipConfiguration().getAlgorithmConfiguration().setSipHeaderAffinityKey("From"); balancerRunner.start(lbConfig); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } //tests callSID and instanceId parameters from http request @Test public void testInstanceId() { new GlobalConfigurationBuilder().globalJmxStatistics().allowDuplicateDomains(true); userArray = new HttpUser[numberUsers]; Locker locker = new Locker(numberUsers); for(int i = 0; i < numberUsers;i++) { if(i==0) userArray[i] = new HttpUser(1, locker,"ID1f2a2222772f4195948d040a2ccc648c-"); else userArray[i] = new HttpUser(1, locker,""); userArray[i].start(); } locker.waitForClients(); assertEquals(numberUsers,serverArray[0].getRequstCount().get()); for(String str : serverArray[0].getRequests()) assertEquals("/restcomm/2012-04-24/Accounts/accountSid/Calls/CA00af667a6a2cbfda0c07d923e78194cd",str); for(int i = 0; i < numberUsers;i++) assertEquals(200, userArray[i].codeResponse); } @AfterClass public static void finalization() { for(int i = 0; i < serverArray.length; i++) serverArray[i].stop(); balancerRunner.stop(); } private class HttpUser extends Thread { int codeResponse; int accountSid; String instanceId; ClientListener listener; public HttpUser(int accountSid, ClientListener listener,String instanceId) { this.accountSid = accountSid; this.listener = listener; this.instanceId = instanceId; } public void run() { try { WebConversation conversation = new WebConversation(); WebRequest request = new GetMethodWebRequest( "http://user:password@127.0.0.1:2080/restcomm/2012-04-24/Accounts/accountSid/Calls/"+instanceId+"CA00af667a6a2cbfda0c07d923e78194cd"); 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) { } } } }