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.deployment.AbstractTwoServerDeploymentTest;
009: import com.tc.test.server.appserver.deployment.DeploymentBuilder;
010: import com.tc.test.server.appserver.deployment.WebApplicationServer;
011: import com.tc.test.server.util.TcConfigBuilder;
012: import com.tctest.webapp.servlets.SynchWriteMultiThreadsTestServlet;
013:
014: import java.util.ArrayList;
015: import java.util.Collections;
016: import java.util.List;
017:
018: import junit.framework.Test;
019:
020: /**
021: * Test session synchronous write with heavy load
022: */
023: public class SynchWriteMultiThreadsTest extends
024: AbstractTwoServerDeploymentTest {
025: private static final String CONTEXT = "SynchWriteMultiThreadsTest";
026: private static final String SERVLET = "SynchWriteMultiThreadsTestServlet";
027: private static final int INTENSITY = 1000;
028: private static final int NUM_OF_DRIVERS = 15;
029:
030: public static Test suite() {
031: return new SynchWriteMultiThreadsTestSetup();
032: }
033:
034: private static class Driver extends Thread {
035: private WebConversation wc;
036: private SynchWriteMultiThreadsTest parent;
037: private List errors;
038:
039: public Driver(SynchWriteMultiThreadsTest parent, List errors) {
040: this .parent = parent;
041: this .errors = errors;
042: this .wc = new WebConversation();
043: }
044:
045: public void run() {
046: try {
047: assertEquals("OK", request(parent.server0,
048: "server=0&command=ping", wc));
049: assertEquals("OK", request(parent.server1,
050: "server=1&command=ping", wc));
051: generateTransactionRequests();
052: } catch (Throwable e) {
053: errors.add(e);
054: throw new RuntimeException(e);
055: }
056: }
057:
058: private void generateTransactionRequests() throws Exception {
059: for (int i = 0; i < INTENSITY; i++) {
060: assertEquals("OK", request(parent.server0,
061: "server=0&command=insert&data=" + i, wc));
062: }
063: }
064:
065: public void validate() throws Exception {
066: assertEquals("0", request(parent.server1,
067: "server=1&command=query&data=0", wc));
068: assertEquals("" + (INTENSITY - 1), request(parent.server1,
069: "server=1&command=query&data=" + (INTENSITY - 1),
070: wc));
071: }
072: }
073:
074: private static String request(WebApplicationServer server,
075: String params, WebConversation con) throws Exception {
076: return server.ping(
077: "/" + CONTEXT + "/" + SERVLET + "?" + params, con)
078: .getText().trim();
079: }
080:
081: public final void testSynchWriteWithLoad() throws Exception {
082: List errors = Collections.synchronizedList(new ArrayList());
083: // start all drivers
084: Driver[] drivers = new Driver[NUM_OF_DRIVERS];
085: for (int i = 0; i < NUM_OF_DRIVERS; i++) {
086: drivers[i] = new Driver(this , errors);
087: drivers[i].start();
088: }
089:
090: // wait for all of them to finish
091: for (int i = 0; i < NUM_OF_DRIVERS; i++) {
092: drivers[i].join();
093: }
094:
095: // proceed only if there are no errors inside any threads
096: if (errors.size() == 0) {
097: // send kill signal to server0
098: killServer0(server0);
099:
100: // validate data on server1
101: for (int i = 0; i < NUM_OF_DRIVERS; i++) {
102: drivers[i].validate();
103: }
104: } else {
105: fail("Exception found in driver thread. ",
106: (Throwable) errors.get(0));
107: }
108: }
109:
110: private void killServer0(WebApplicationServer server) {
111: try {
112: assertEquals("OK", request(server, "server=0&command=kill",
113: new WebConversation()));
114: } catch (Throwable e) {
115: // expected
116: System.err
117: .println("Caught expected exception from killing server1");
118: }
119: }
120:
121: private static class SynchWriteMultiThreadsTestSetup extends
122: TwoServerTestSetup {
123: public SynchWriteMultiThreadsTestSetup() {
124: super (SynchWriteMultiThreadsTest.class, CONTEXT);
125: }
126:
127: protected void configureWar(DeploymentBuilder builder) {
128: builder.addServlet(SERVLET, "/" + SERVLET + "/*",
129: SynchWriteMultiThreadsTestServlet.class, null,
130: false);
131: }
132:
133: protected void configureTcConfig(TcConfigBuilder clientConfig) {
134: clientConfig.addWebApplication(CONTEXT, true);
135: }
136: }
137: }
|