|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source |
| 3 | + * Copyright 2014, Red Hat, Inc., and individual contributors |
| 4 | + * by the @authors tag. See the copyright.txt in the distribution for a |
| 5 | + * full listing of individual contributors. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.jboss.weld.tests.contexts.cache; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.net.MalformedURLException; |
| 21 | +import java.net.URL; |
| 22 | + |
| 23 | +import junit.framework.Assert; |
| 24 | + |
| 25 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 26 | +import org.jboss.arquillian.junit.Arquillian; |
| 27 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 28 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 29 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 30 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 31 | +import org.jboss.weld.tests.category.Integration; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.experimental.categories.Category; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | + |
| 36 | +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; |
| 37 | +import com.gargoylesoftware.htmlunit.WebClient; |
| 38 | + |
| 39 | +@RunWith(Arquillian.class) |
| 40 | +@Category(Integration.class) |
| 41 | +public class RequestScopedCacheLeakTest { |
| 42 | + |
| 43 | + @ArquillianResource |
| 44 | + private URL contextPath; |
| 45 | + |
| 46 | + @Deployment(testable = false) |
| 47 | + public static WebArchive getDeployment() { |
| 48 | + return ShrinkWrap.create(WebArchive.class).addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") |
| 49 | + .addClasses(SimpleServlet.class, ConversationScopedBean.class); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void test() throws Exception { |
| 54 | + WebClient webClient = new WebClient(); |
| 55 | + webClient.setThrowExceptionOnFailingStatusCode(false); |
| 56 | + for (int i = 0; i < 100; i++) { |
| 57 | + // first, send out a hundred of poisoning requests |
| 58 | + // each of these should leave a thread in a broken state |
| 59 | + sendRequest(webClient, i, true); |
| 60 | + } |
| 61 | + for (int i = 0; i < 100; i++) { |
| 62 | + // now send out normal requests to see if they are affected by the thread's broken state |
| 63 | + String result = sendRequest(webClient, i, false); |
| 64 | + Assert.assertFalse("Invalid state detected after " + (i + 1) + " requests", result.startsWith("bar")); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private String sendRequest(WebClient webClient, int sequence, boolean poison) throws FailingHttpStatusCodeException, MalformedURLException, IOException { |
| 69 | + final String path = getPath("getAndSet", sequence, poison); |
| 70 | + return webClient.getPage(path).getWebResponse().getContentAsString().trim(); |
| 71 | + } |
| 72 | + |
| 73 | + private String getPath(String test, int sequence, boolean poison) { |
| 74 | + String path = contextPath + "/servlet?action=" + test + "&sequence=" + sequence; |
| 75 | + if (poison) { |
| 76 | + path += "&poison=true"; |
| 77 | + } |
| 78 | + return path; |
| 79 | + } |
| 80 | +} |
0 commit comments