KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cluster > test > SessionBasedConcurrentTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
31
32 import EDU.oswego.cs.dl.util.concurrent.Semaphore;
33
34 /**
35  * Simple clustering test case of get/set. It is session based granularity with concurrent access.
36  *
37  * @author Ben Wang
38  * @version $Revision: 1.0
39  */

40 public class SessionBasedConcurrentTestCase
41       extends BaseTest
42 {
43    Throwable JavaDoc ex_ = null;
44    final int PERMITS = 100;
45    Semaphore sem_ = new Semaphore(PERMITS);
46    String JavaDoc setURLName_;
47    String JavaDoc getURLName_;
48
49    public SessionBasedConcurrentTestCase(String JavaDoc 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 JavaDoc {
57       super.setUp();
58       ex_ = null;
59    }
60
61    public static Test suite() throws Exception JavaDoc
62    {
63       Test t1 = JBossClusteredTestCase.getDeploySetup(SessionBasedConcurrentTestCase.class,
64             "http-sr.war");
65       return t1;
66    }
67
68    /**
69     * Test different set in different servers.
70     * @throws Exception
71     */

72    public void testConcurrentPut()
73          throws Exception JavaDoc
74    {
75       int TIMES = 10;
76       for(int i=0; i < 10; i++)
77       {
78          String JavaDoc threadName = "startWithServer_1_ " +i;
79          Thread JavaDoc t1 = runThread(threadName, baseURL0_, baseURL1_, servers_[1], TIMES, i);
80          threadName = "startWithServer_2_ " +i;
81          Thread JavaDoc 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    /**
105     * Thread to execute the http request.
106     * @param threadName
107     * @return
108     */

109    protected Thread JavaDoc runThread(final String JavaDoc threadName, final String JavaDoc baseURL0, final String JavaDoc baseURL1,
110                               final String JavaDoc server2, final int TIMES, final int SEED) {
111       return new Thread JavaDoc(threadName) {
112          Random JavaDoc rand = new Random JavaDoc(SEED);
113          public void run() {
114             try {
115                sem_.acquire();
116             } catch (InterruptedException JavaDoc 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                   // Random numbder between [0, 200].
127
long msecs = rand.nextInt(200);
128                   sleepThread(msecs);
129                }
130             } finally {
131                sem_.release();
132             }
133          }
134
135          protected void work() {
136             String JavaDoc attr = "";
137             getLog().debug("Enter runThread");
138
139             String JavaDoc setURLName = setURLName_;
140             String JavaDoc getURLName = getURLName_;
141
142             getLog().debug(setURLName + ":::::::" + getURLName);
143
144             // Create an instance of HttpClient.
145
HttpClient client = new HttpClient();
146
147             // Set the session attribute first
148
makeGet(client, baseURL0 +setURLName);
149
150             // Get the Attribute set by testsessionreplication.jsp
151
attr = makeGetWithState(client, baseURL0 +getURLName);
152
153             // Let's switch to server 2 to retrieve the session attribute.
154
getLog().debug("Switching to server " +server2);
155             setCookieDomainToThisServer(client, server2);
156             String JavaDoc attr2 = makeGet(client, baseURL1 +getURLName);
157
158             // Check the result
159
try {
160               assertEquals("Http session replication attribtues retrieved from both servers ", attr, attr2);
161             } catch (Throwable JavaDoc 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