1 4 package com.tctest.dso; 5 6 import org.apache.commons.httpclient.HttpClient; 7 8 import com.tc.object.config.schema.AutoLock; 9 import com.tc.object.config.schema.LockLevel; 10 import com.tc.object.config.schema.Root; 11 import com.tc.test.server.appserver.unit.AbstractAppServerTestCase; 12 import com.tc.test.server.util.HttpUtil; 13 14 import java.io.IOException ; 15 import java.io.PrintWriter ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.List ; 19 import java.util.Random ; 20 21 import javax.servlet.http.HttpServlet ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 public class DsoRootTest extends AbstractAppServerTestCase { 26 private static final int TOTAL_REQUEST_COUNT = 100; 27 28 protected boolean isSessionTest() { 29 return false; 30 } 31 32 public void testRoot() throws Throwable { 33 List roots = new ArrayList (); 34 String rootName = "counterObject"; 35 String fieldName = RootCounterServlet.class.getName() + ".counterObject"; 36 roots.add(new Root(rootName, fieldName)); 37 addRoots(roots); 38 39 List locks = new ArrayList (); 40 LockLevel lockLevel = LockLevel.WRITE; 41 String methodExpression = "* " + RootCounterServlet.class.getName() + "$Counter.*(..)"; 42 locks.add(new AutoLock(methodExpression, lockLevel)); 43 addLocks(locks); 44 45 startDsoServer(); 46 runNodes(2); 47 } 48 49 private void runNodes(int nodeCount) throws Throwable { 50 HttpClient client = HttpUtil.createHttpClient(); 51 52 int[] ports = new int[nodeCount]; 53 URL [] urls = new URL [nodeCount]; 54 55 for (int i = 0; i < nodeCount; i++) { 56 ports[i] = startAppServer(true).serverPort(); 57 urls[i] = createUrl(ports[i], RootCounterServlet.class); 58 } 59 60 Random random = new Random (); 61 for (int i = 0, currentRequestCount = 0; i < TOTAL_REQUEST_COUNT && currentRequestCount < TOTAL_REQUEST_COUNT; i++) { 62 int remainingRequests = TOTAL_REQUEST_COUNT - currentRequestCount; 63 for (int j = 0; j < random.nextInt(remainingRequests + 1); j++) { 64 int newVal = HttpUtil.getInt(urls[i % nodeCount], client); 65 currentRequestCount++; 66 assertEquals(currentRequestCount, newVal); 67 } 68 } 69 } 70 71 public static class RootCounterServlet extends HttpServlet { 72 private final Counter counterObject = new Counter(); 73 74 private static class Counter { 75 private int counter; 76 77 public Counter() { 78 counter = 0; 79 } 80 81 public synchronized void increment() { 82 counter++; 83 } 84 85 public synchronized void setValue(int newValue) { 86 counter = newValue; 87 } 88 89 public synchronized int getValue() { 90 return counter; 91 } 92 } 93 94 private int getCurrentCountValue() { 95 counterObject.increment(); 96 return counterObject.getValue(); 97 } 98 99 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 100 response.setContentType("text/html"); 101 PrintWriter out = response.getWriter(); 102 out.println(getCurrentCountValue()); 103 } 104 } 105 } 106 | Popular Tags |