| 1 9 package org.jboss.portal.test.cms.stress; 10 11 import junit.framework.TestCase; 12 import EDU.oswego.cs.dl.util.concurrent.Executor; 13 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 14 import org.jboss.portal.cms.CMS; 15 import org.jboss.portal.cms.NodeFactory; 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.Element ; 18 import org.apache.log4j.Logger; 19 import org.apache.log4j.Appender; 20 import org.apache.log4j.ConsoleAppender; 21 import org.apache.log4j.SimpleLayout; 22 23 import javax.xml.parsers.DocumentBuilderFactory ; 24 import java.io.InputStream ; 25 import java.io.File ; 26 import java.util.Iterator ; 27 28 32 public class ConcurrentTestCase extends TestCase 33 { 34 35 Logger log = Logger.getLogger(ConcurrentTestCase.class); 36 37 static 38 { 39 Logger root = Logger.getRootLogger(); 40 Appender appender = new ConsoleAppender(new SimpleLayout()); 41 root.addAppender(appender); 42 } 43 44 public ConcurrentTestCase(String name) 45 { 46 super(name); 47 } 48 49 private CMS cms; 50 private NodeFactory factory; 51 52 private File tmp; 53 54 protected void setUp() throws Exception  55 { 56 tmp = File.createTempFile("cms", ".tmp"); 57 if (!tmp.delete()) 58 { 59 throw new Exception ("Cannot delete tmp file"); 60 } 61 if (!tmp.mkdirs()) 62 { 63 throw new Exception ("Cannot create dir"); 64 } 65 66 System.setProperty("jboss.server.data.dir", tmp.getAbsolutePath()); 67 68 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 69 InputStream in = loader.getResourceAsStream("test/domain2.xml"); 70 Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in); 71 Element elt = doc.getDocumentElement(); 72 cms = new CMS(); 73 cms.setConfig(elt); 74 cms.create(); 75 cms.start(); 76 77 factory = cms.getNodeFactory(); 78 } 79 80 protected void tearDown() throws Exception  81 { 82 cms.stop(); 83 cms.destroy(); 84 } 85 86 private String [] alphabet = {"client0","client1","client2","client3","client4"}; 87 88 public void testA() throws Exception  89 { 90 int size = 4; 91 92 Command[][] scenarios = new Command[size][]; 93 for (int i = 0;i < scenarios.length;i++) 94 { 95 scenarios[i] = new Command[] 96 { 97 new BeginTxCommand(), 98 new CreateDirCommand("/" + alphabet[i] + "/a"), 99 new CreateContentCommand("/" + alphabet[i] + "/a/b", "abc" + i), 100 new EndTxCommand(true), 101 new SleepCommand("50"), 102 new BeginTxCommand(), 103 new TestContentCommand("/" + alphabet[i] + "/a/b", "abc" + i), 104 new TestDirCommand("/" + alphabet[i] + "/a"), 105 new DeleteCommand("/" + alphabet[i] + "/a"), 106 new EndTxCommand(true), 107 new SleepCommand("50") 108 }; 109 } 110 111 Client[] clients = new Client[size]; 112 for (int i = 0; i < clients.length; i++) 113 { 114 clients[i] = new Client(i, 10, scenarios[i], 200, factory); 115 } 116 117 Thread [] threads = new Thread [size]; 118 119 for (int i = 0; i < clients.length; i++) 120 { 121 Client client = clients[i]; 122 threads[i] = new Thread (client, "CMS-" + i); 123 log.info("created " + i); 124 threads[i].start(); 125 log.info("started " + i); 126 } 128 129 130 for (int i = 0; i < threads.length; i++) 134 { 135 Thread thread = threads[i]; 136 log.info("waiting " + i); 137 thread.join(); 138 log.info("joined " + i); 139 } 140 141 for (int k = 0; k < clients.length; k++) 142 { 143 Client client = clients[k]; 144 for (Iterator i = client.getResults().iterator(); i.hasNext();) 145 { 146 Client.Result result = (Client.Result)i.next(); 147 for (Iterator j = result.getFailures().iterator(); j.hasNext();) 148 { 149 Failure failure = (Failure)j.next(); 150 System.out.println("failure = " + failure); 151 } 152 } 153 } 154 } 155 156 157 } 158 | Popular Tags |