Skip to content

Commit 29fd110

Browse files
committed
WELD-1802 RequestScopedCache - Testcase
1 parent 00bdcba commit 29fd110

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.Serializable;
20+
21+
import javax.enterprise.context.ConversationScoped;
22+
23+
@ConversationScoped
24+
public class ConversationScopedBean implements Serializable {
25+
26+
private String value = "foo";
27+
28+
public String getAndSet(String newValue) {
29+
String old = value;
30+
value = newValue;
31+
return old;
32+
}
33+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
21+
import javax.inject.Inject;
22+
import javax.servlet.ServletException;
23+
import javax.servlet.annotation.WebServlet;
24+
import javax.servlet.http.HttpServlet;
25+
import javax.servlet.http.HttpServletRequest;
26+
import javax.servlet.http.HttpServletResponse;
27+
28+
import org.jboss.weld.context.beanstore.ConversationNamingScheme;
29+
30+
@WebServlet(value = "/servlet", asyncSupported = true)
31+
public class SimpleServlet extends HttpServlet {
32+
33+
@Inject
34+
private ConversationScopedBean bean;
35+
36+
@Override
37+
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
38+
String action = req.getParameter("action");
39+
String sequence = req.getParameter("sequence");
40+
String poison = req.getParameter("poison");
41+
if ("getAndSet".equals(action)) {
42+
// the value should always be foo
43+
String value = bean.getAndSet("bar" + sequence);
44+
resp.getWriter().println(value);
45+
if (poison != null) {
46+
// this is a poisoning request
47+
// normal applications should never do something like this
48+
// we just do this to cause an exception to be thrown out of ConversationContext.deactivate
49+
req.removeAttribute(ConversationNamingScheme.PARAMETER_NAME);
50+
}
51+
} else {
52+
throw new IllegalArgumentException(action);
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)