/jboss-as-7.1.1.Final/testsuite/integration/clust/src/test/java/org/jboss/as/test/clustering/cluster/web/ClusteredWebFailoverTestCase.java
Java | 285 lines | 165 code | 41 blank | 79 comment | 2 complexity | 640cc22eed83051148a1b83c6b438b69 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a 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 */
22package org.jboss.as.test.clustering.cluster.web;
23
24import java.io.IOException;
25import java.net.URL;
26import java.util.Properties;
27import java.util.concurrent.ExecutionException;
28
29import javax.servlet.http.HttpServletResponse;
30
31import org.apache.http.HttpResponse;
32import org.apache.http.client.methods.HttpGet;
33import org.apache.http.impl.client.DefaultHttpClient;
34import org.jboss.arquillian.container.test.api.ContainerController;
35import org.jboss.arquillian.container.test.api.Deployer;
36import org.jboss.arquillian.container.test.api.OperateOnDeployment;
37import org.jboss.arquillian.container.test.api.RunAsClient;
38import org.jboss.arquillian.junit.Arquillian;
39import org.jboss.arquillian.junit.InSequence;
40import org.jboss.arquillian.test.api.ArquillianResource;
41import org.jboss.as.test.clustering.single.web.SimpleServlet;
42import org.jboss.as.test.http.util.HttpClientUtils;
43import org.junit.Assert;
44import org.junit.BeforeClass;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47
48import static org.jboss.as.test.clustering.ClusteringTestConstants.CONTAINER_1;
49import static org.jboss.as.test.clustering.ClusteringTestConstants.CONTAINER_2;
50import static org.jboss.as.test.clustering.ClusteringTestConstants.DEPLOYMENT_1;
51import static org.jboss.as.test.clustering.ClusteringTestConstants.DEPLOYMENT_2;
52import static org.jboss.as.test.clustering.ClusteringTestConstants.GRACE_TIME_TO_MEMBERSHIP_CHANGE;
53
54/**
55 * Test that failover and undeploy works.
56 *
57 * @author Radoslav Husar
58 */
59@RunWith(Arquillian.class)
60@RunAsClient
61public abstract class ClusteredWebFailoverTestCase {
62
63 /** Controller for testing failover and undeploy **/
64 @ArquillianResource
65 private ContainerController controller;
66 @ArquillianResource
67 private Deployer deployer;
68
69 @BeforeClass
70 public static void printSysProps() {
71 Properties sysprops = System.getProperties();
72 System.out.println("System properties:\n" + sysprops);
73 }
74
75 /**
76 * Workaround for Arquillian so that you can use "@ArquillianResource(C.class) @OperateOnDeployment(D)" because the
77 * containers need to be started beforehand.
78 */
79 @Test
80 @InSequence(1)
81 public void testStartContainersAndDeployments() {
82 // Container is unmanaged, need to start manually.
83 controller.start(CONTAINER_1);
84 deployer.deploy(DEPLOYMENT_1);
85 controller.start(CONTAINER_2);
86 deployer.deploy(DEPLOYMENT_2);
87 }
88
89 /**
90 * Test simple graceful shutdown failover:
91 *
92 * 1/ Start 2 containers and deploy <distributable/> webapp.
93 * 2/ Query first container creating a web session.
94 * 3/ Shutdown first container.
95 * 4/ Query second container verifying sessions got replicated.
96 * 5/ Bring up the first container.
97 * 6/ Query first container verifying that updated sessions replicated back.
98 *
99 * @throws IOException
100 * @throws InterruptedException
101 */
102 @Test
103 @InSequence(2)
104 public void testGracefulSimpleFailover(
105 @ArquillianResource(SimpleServlet.class) @OperateOnDeployment(DEPLOYMENT_1) URL baseURL1,
106 @ArquillianResource(SimpleServlet.class) @OperateOnDeployment(DEPLOYMENT_2) URL baseURL2)
107 throws IOException, InterruptedException, ExecutionException {
108
109 DefaultHttpClient client = HttpClientUtils.relaxedCookieHttpClient();
110
111 String url1 = baseURL1.toString() + "simple";
112 String url2 = baseURL2.toString() + "simple";
113
114 try {
115 HttpResponse response = tryGet(client, url1);
116 System.out.println("Requested " + url1 + ", got " + response.getFirstHeader("value").getValue() + ".");
117 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
118 Assert.assertEquals(1, Integer.parseInt(response.getFirstHeader("value").getValue()));
119 response.getEntity().getContent().close();
120
121 // Lets do this twice to have more debug info if failover is slow.
122 response = client.execute(new HttpGet(url1));
123 System.out.println("Requested " + url1 + ", got " + response.getFirstHeader("value").getValue() + ".");
124 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
125 Assert.assertEquals(2, Integer.parseInt(response.getFirstHeader("value").getValue()));
126 response.getEntity().getContent().close();
127
128 // Gracefully shutdown the 1st container.
129 controller.stop(CONTAINER_1);
130
131 // Now check on the 2nd server
132
133 // Note that this DOES rely on the fact that both servers are running on the "same" domain,
134 // which is '127.0.0.0'. Otherwise you will have to spoof cookies. @Rado
135
136 response = tryGet(client, url2);
137 System.out.println("Requested " + url2 + ", got " + response.getFirstHeader("value").getValue() + ".");
138 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
139 Assert.assertEquals("Session failed to replicate after container 1 was shutdown.", 3, Integer.parseInt(response.getFirstHeader("value").getValue()));
140 response.getEntity().getContent().close();
141
142 // Lets do one more check.
143 response = client.execute(new HttpGet(url2));
144 System.out.println("Requested " + url2 + ", got " + response.getFirstHeader("value").getValue() + ".");
145 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
146 Assert.assertEquals(4, Integer.parseInt(response.getFirstHeader("value").getValue()));
147 response.getEntity().getContent().close();
148
149 controller.start(CONTAINER_1);
150
151 // Lets wait for the cluster to update membership and tranfer state.
152
153 response = tryGet(client, url1);
154 System.out.println("Requested " + url1 + ", got " + response.getFirstHeader("value").getValue() + ".");
155 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
156 Assert.assertEquals("Session failed to replicate after container 1 was brough up.", 5, Integer.parseInt(response.getFirstHeader("value").getValue()));
157 response.getEntity().getContent().close();
158
159 // Lets do this twice to have more debug info if failover is slow.
160 response = client.execute(new HttpGet(url1));
161 System.out.println("Requested " + url1 + ", got " + response.getFirstHeader("value").getValue() + ".");
162 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
163 Assert.assertEquals(6, Integer.parseInt(response.getFirstHeader("value").getValue()));
164 response.getEntity().getContent().close();
165 } finally {
166 client.getConnectionManager().shutdown();
167 }
168
169 // Is would be done automatically, keep for 2nd test is added
170 deployer.undeploy(DEPLOYMENT_1);
171 controller.stop(CONTAINER_1);
172 deployer.undeploy(DEPLOYMENT_2);
173 controller.stop(CONTAINER_2);
174
175 // Assert.fail("Show me the logs please!");
176 }
177
178 @Test
179 @InSequence(3)
180 public void testStartContainersAndDeploymentsForUndeployFailover() {
181 // Container is unmanaged, need to start manually.
182 controller.start(CONTAINER_1);
183 deployer.deploy(DEPLOYMENT_1);
184
185 controller.start(CONTAINER_2);
186 deployer.deploy(DEPLOYMENT_2);
187 }
188
189 /**
190 * Test simple undeploy failover:
191 *
192 * 1/ Start 2 containers and deploy <distributable/> webapp.
193 * 2/ Query first container creating a web session.
194 * 3/ Undeploy application from the first container.
195 * 4/ Query second container verifying sessions got replicated.
196 * 5/ Redeploy application to the first container.
197 * 6/ Query first container verifying that updated sessions replicated back.
198 *
199 * @throws IOException
200 * @throws InterruptedException
201 */
202 @Test
203 @InSequence(4)
204 public void testGracefulUndeployFailover(
205 @ArquillianResource(SimpleServlet.class) @OperateOnDeployment(DEPLOYMENT_1) URL baseURL1,
206 @ArquillianResource(SimpleServlet.class) @OperateOnDeployment(DEPLOYMENT_2) URL baseURL2)
207 throws IOException, InterruptedException {
208
209 DefaultHttpClient client = HttpClientUtils.relaxedCookieHttpClient();
210
211 String url1 = baseURL1.toString() + "simple";
212 String url2 = baseURL2.toString() + "simple";
213
214 try {
215 HttpResponse response = tryGet(client, url1);
216 System.out.println("Requested " + url1 + ", got " + response.getFirstHeader("value").getValue() + ".");
217 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
218 Assert.assertEquals(1, Integer.parseInt(response.getFirstHeader("value").getValue()));
219 response.getEntity().getContent().close();
220
221 // Lets do this twice to have more debug info if failover is slow.
222 response = client.execute(new HttpGet(url1));
223 System.out.println("Requested " + url1 + ", got " + response.getFirstHeader("value").getValue() + ".");
224 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
225 Assert.assertEquals(2, Integer.parseInt(response.getFirstHeader("value").getValue()));
226 response.getEntity().getContent().close();
227
228 // Gracefully undeploy from the 1st container.
229 deployer.undeploy(DEPLOYMENT_1);
230
231 // Now check on the 2nd server
232
233 // Note that this DOES rely on the fact that both servers are running on the "same" domain,
234 // which is '127.0.0.1'. Otherwise you will have to spoof cookies. @Rado
235 response = tryGet(client, url2);
236 System.out.println("Requested " + url2 + ", got " + response.getFirstHeader("value").getValue() + ".");
237 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
238 Assert.assertEquals("Session failed to replicate after container 1 was shutdown.", 3, Integer.parseInt(response.getFirstHeader("value").getValue()));
239 response.getEntity().getContent().close();
240
241 // Lets do one more check.
242 response = tryGet(client, url2);
243 System.out.println("Requested " + url2 + ", got " + response.getFirstHeader("value").getValue() + ".");
244 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
245 Assert.assertEquals(4, Integer.parseInt(response.getFirstHeader("value").getValue()));
246 response.getEntity().getContent().close();
247
248 // Redeploy
249 deployer.deploy(DEPLOYMENT_1);
250
251 response = tryGet(client, url1);
252 System.out.println("Requested " + url1 + ", got " + response.getFirstHeader("value").getValue() + ".");
253 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
254 Assert.assertEquals("Session failed to replicate after container 1 was brough up.", 5, Integer.parseInt(response.getFirstHeader("value").getValue()));
255 response.getEntity().getContent().close();
256
257 // Lets do this twice to have more debug info if failover is slow.
258 response = client.execute(new HttpGet(url1));
259 System.out.println("Requested " + url1 + ", got " + response.getFirstHeader("value").getValue() + ".");
260 Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
261 Assert.assertEquals(6, Integer.parseInt(response.getFirstHeader("value").getValue()));
262 response.getEntity().getContent().close();
263 } finally {
264 client.getConnectionManager().shutdown();
265 }
266
267 // Is would be done automatically, keep for when 3nd test is added
268 deployer.undeploy(DEPLOYMENT_1);
269 controller.stop(CONTAINER_1);
270 deployer.undeploy(DEPLOYMENT_2);
271 controller.stop(CONTAINER_2);
272
273 // Assert.fail("Show me the logs please!");
274 }
275
276 private HttpResponse tryGet(final DefaultHttpClient client, final String url1) throws IOException {
277 final long startTime;
278 HttpResponse response = client.execute(new HttpGet(url1));
279 startTime = System.currentTimeMillis();
280 while(response.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK && startTime + GRACE_TIME_TO_MEMBERSHIP_CHANGE > System.currentTimeMillis()) {
281 response = client.execute(new HttpGet(url1));
282 }
283 return response;
284 }
285}