PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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