001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.server.appserver.unit;
006:
007: import com.meterware.httpunit.WebConversation;
008: import com.tc.test.server.appserver.StandardAppServerParameters;
009: import com.tc.test.server.appserver.deployment.AbstractOneServerDeploymentTest;
010: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
011: import com.tc.test.server.appserver.deployment.WebApplicationServer;
012: import com.tc.test.server.util.TcConfigBuilder;
013: import com.tc.util.concurrent.ThreadUtil;
014: import com.tctest.webapp.listeners.BindingListenerWithException;
015: import com.tctest.webapp.listeners.InvalidatorAttributeListener;
016: import com.tctest.webapp.listeners.InvalidatorBindingListener;
017: import com.tctest.webapp.listeners.InvalidatorSessionListener;
018: import com.tctest.webapp.servlets.InvalidatorServlet;
019:
020: import java.util.Date;
021:
022: import junit.framework.Test;
023:
024: public class SessionInvalidatorTest extends
025: AbstractOneServerDeploymentTest {
026: private static final String CONTEXT = "SessionInvalidatorTest";
027: private static final String SERVLET = "InvalidatorServlet";
028:
029: private static final int invalidatorSleepSeconds = 1;
030: private static final int defaultMaxIdleSeconds = 5;
031: private static final int waitFactor = 4;
032:
033: public static Test suite() {
034: return new SessionInvalidatorTestSetup();
035: }
036:
037: public void testInvalidator() throws Exception {
038: WebConversation wc = new WebConversation();
039: // first, sanity check
040: checkResponse("INVALID REQUEST", "action=0", wc);
041:
042: // now, put a string into session...
043: checkResponse("OK", "action=set&key=attr1", wc);
044: // ... and check if it made it there.
045: checkResponse("attr1=attr1", "action=get&key=attr1", wc);
046:
047: checkCallCount("SessionListener.sessionCreated", 1, wc);
048: checkCallCount("BindingListener.valueBound", 1, wc);
049:
050: // now set exception-throwing BindingListener..
051: checkResponse("OK", "action=setwithexception&key=attr2", wc);
052: // ... and check if it DID NOT made it there.
053: checkResponse("attr2=null", "action=get&key=attr2", wc);
054:
055: checkCallCount("BindingListener.valueBound", 2, wc);
056: checkCallCount("BindingListener.valueUnbound", 0, wc);
057:
058: // set session max idle time
059: checkResponse("OK", "action=setmax&key=3", wc);
060:
061: ThreadUtil.reallySleep(waitFactor * defaultMaxIdleSeconds
062: * 1000);
063:
064: checkCallCount("SessionListener.sessionDestroyed", 1, wc);
065: checkCallCount("BindingListener.valueUnbound", 1, wc);
066:
067: // =========================================================
068: // by this point we verified that our old session was invalidated successfully while it WAS NOT being used.
069: // now let's see what happens if it's in use by a LOOONG-running request
070: // =========================================================
071: // make sure we got a new, good session
072: checkResponse("OK", "action=isNew", wc);
073:
074: // now, put a string into session...
075: checkResponse("OK", "action=set&key=attr1", wc);
076: checkCallCount("BindingListener.valueBound", 3, wc);
077:
078: // set session max idle time
079: checkResponse("OK", "action=setmax&key=5", wc);
080:
081: // now, get a long-running request a-running
082: checkResponse("OK", "action=sleep&key=15", wc);
083:
084: // make sure we still got the old session
085: checkResponse("OK", "action=isOld", wc);
086: checkCallCount("BindingListener.valueUnbound", 1, wc);
087: checkCallCount("SessionListener.sessionDestroyed", 1, wc);
088:
089: // now let this session expire
090: // give invalidator at least 2 runs...
091: // set session max idle time
092: checkResponse("OK", "action=setmax&key=5", wc);
093: Thread.sleep(waitFactor * defaultMaxIdleSeconds * 1000);
094: checkCallCount("BindingListener.valueUnbound", 2, wc);
095: checkCallCount("SessionListener.sessionDestroyed", 2, wc);
096: }
097:
098: private void checkResponse(String expected, String params,
099: WebConversation wc) throws Exception {
100: System.err.println("=== Send Request [" + (new Date())
101: + "]: params=[" + params + "]");
102: String actual = request(server0, params, wc);
103: System.err.println("=== Got Response [" + (new Date())
104: + "]: params=[" + params + "], response=[" + actual
105: + "]");
106: assertTimeDirection();
107: assertEquals(expected, actual);
108: }
109:
110: private void checkCallCount(final String key, int expectedCount,
111: WebConversation wc) throws Exception {
112: checkResponse(key + "=" + expectedCount,
113: "action=call_count&key=" + key, wc);
114: }
115:
116: private String request(WebApplicationServer server, String params,
117: WebConversation wc) throws Exception {
118: return server.ping(
119: "/" + CONTEXT + "/" + SERVLET + "?" + params, wc)
120: .getText().trim();
121: }
122:
123: private static class SessionInvalidatorTestSetup extends
124: OneServerTestSetup {
125: public SessionInvalidatorTestSetup() {
126: super (SessionInvalidatorTest.class, CONTEXT);
127: }
128:
129: protected void configureWar(DeploymentBuilder builder) {
130: builder.addServlet(SERVLET, "/" + SERVLET + "/*",
131: InvalidatorServlet.class, null, false);
132: builder.addListener(InvalidatorAttributeListener.class);
133: builder.addListener(InvalidatorSessionListener.class);
134: builder.addListener(InvalidatorBindingListener.class);
135: builder.addListener(BindingListenerWithException.class);
136: }
137:
138: protected void configureTcConfig(TcConfigBuilder clientConfig) {
139: clientConfig.addWebApplication(CONTEXT);
140: clientConfig
141: .addInstrumentedClass(InvalidatorAttributeListener.class
142: .getName());
143: clientConfig
144: .addInstrumentedClass(InvalidatorSessionListener.class
145: .getName());
146: clientConfig
147: .addInstrumentedClass(InvalidatorBindingListener.class
148: .getName());
149: clientConfig
150: .addInstrumentedClass(BindingListenerWithException.class
151: .getName());
152: }
153:
154: protected void configureServerParamers(
155: StandardAppServerParameters params) {
156: params.appendSysProp("com.tc.session.invalidator.sleep",
157: invalidatorSleepSeconds);
158: params.appendSysProp("com.tc.session.maxidle.seconds",
159: defaultMaxIdleSeconds);
160: params.appendSysProp("com.tc.session.debug.invalidate",
161: true);
162: }
163: }
164: }
|