1 22 package org.jboss.test.cluster.test; 23 24 import junit.framework.Test; 25 import org.apache.commons.httpclient.HttpClient; 26 import org.apache.commons.httpclient.HttpMethod; 27 import org.apache.commons.httpclient.methods.GetMethod; 28 import org.jboss.test.JBossClusteredTestCase; 29 30 import java.util.Random ; 31 32 import EDU.oswego.cs.dl.util.concurrent.Semaphore; 33 34 40 public class SessionBasedConcurrentTestCase 41 extends BaseTest 42 { 43 Throwable ex_ = null; 44 final int PERMITS = 100; 45 Semaphore sem_ = new Semaphore(PERMITS); 46 String setURLName_; 47 String getURLName_; 48 49 public SessionBasedConcurrentTestCase(String name) 50 { 51 super(name); 52 setURLName_ = "/http-sr/testsessionreplication.jsp"; 53 getURLName_ = "/http-sr/getattribute.jsp"; 54 } 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 ex_ = null; 59 } 60 61 public static Test suite() throws Exception 62 { 63 Test t1 = JBossClusteredTestCase.getDeploySetup(SessionBasedConcurrentTestCase.class, 64 "http-sr.war"); 65 return t1; 66 } 67 68 72 public void testConcurrentPut() 73 throws Exception 74 { 75 int TIMES = 10; 76 for(int i=0; i < 10; i++) 77 { 78 String threadName = "startWithServer_1_ " +i; 79 Thread t1 = runThread(threadName, baseURL0_, baseURL1_, servers_[1], TIMES, i); 80 threadName = "startWithServer_2_ " +i; 81 Thread t2 = runThread(threadName, baseURL1_, baseURL0_, servers_[0], TIMES, i); 82 t1.start(); 83 t2.start(); 84 } 85 86 sleepThread(1000); 87 while(true) { 88 if(sem_.permits() != PERMITS) 89 { 90 sleepThread(1000); 91 continue; 92 } else 93 { 94 break; 95 } 96 } 97 98 if(ex_ != null) 99 { 100 fail("Test fail " +ex_); 101 } 102 } 103 104 109 protected Thread runThread(final String threadName, final String baseURL0, final String baseURL1, 110 final String server2, final int TIMES, final int SEED) { 111 return new Thread (threadName) { 112 Random rand = new Random (SEED); 113 public void run() { 114 try { 115 sem_.acquire(); 116 } catch (InterruptedException e) { 117 e.printStackTrace(); 118 ex_ = e; 119 return; 120 } 121 122 try { 123 for(int i=0; i < TIMES; i++) 124 { 125 work(); 126 long msecs = rand.nextInt(200); 128 sleepThread(msecs); 129 } 130 } finally { 131 sem_.release(); 132 } 133 } 134 135 protected void work() { 136 String attr = ""; 137 getLog().debug("Enter runThread"); 138 139 String setURLName = setURLName_; 140 String getURLName = getURLName_; 141 142 getLog().debug(setURLName + ":::::::" + getURLName); 143 144 HttpClient client = new HttpClient(); 146 147 makeGet(client, baseURL0 +setURLName); 149 150 attr = makeGetWithState(client, baseURL0 +getURLName); 152 153 getLog().debug("Switching to server " +server2); 155 setCookieDomainToThisServer(client, server2); 156 String attr2 = makeGet(client, baseURL1 +getURLName); 157 158 try { 160 assertEquals("Http session replication attribtues retrieved from both servers ", attr, attr2); 161 } catch (Throwable ex) { 162 ex_ = ex; 163 } 164 165 getLog().debug("Http Session Replication has happened"); 166 getLog().debug("Exit runThread"); 167 } 168 }; 169 } 170 } 171 | Popular Tags |