Update of /cvsroot/exist/eXist-1.0/src/org/exist/xmldb/test/concurrent
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11682/src/org/exist/xmldb/test/concurrent
Modified Files:
RemoveAppendAction.java ConcurrentTestBase.java
ConcurrentXUpdateTest.java DBUtils.java
Added Files:
ConcurrentResourceTest.java AllTests.java
ReplaceResourceAction.java
Log Message:
More tests added.
Index: ConcurrentTestBase.java
===================================================================
RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xmldb/test/concurrent/ConcurrentTestBase.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ConcurrentTestBase.java 18 Sep 2004 10:51:45 -0000 1.1
--- ConcurrentTestBase.java 18 Sep 2004 12:24:23 -0000 1.2
***************
*** 22,25 ****
--- 22,27 ----
import java.io.File;
+ import java.util.ArrayList;
+ import java.util.List;
import org.xmldb.api.base.Collection;
***************
*** 35,56 ****
public abstract class ConcurrentTestBase extends TestCase {
! protected String uri;
! protected Collection root;
! protected String testCol;
protected String[] wordList;
protected volatile boolean failed = false;
/**
! * @param arg0
*/
public ConcurrentTestBase(String name, String uri, String testCollection) {
super(name);
! this.uri = uri;
! this.testCol = testCollection;
}
! public abstract void testConcurrent() throws Exception;
/*
--- 37,96 ----
public abstract class ConcurrentTestBase extends TestCase {
! protected String rootColURI;
! protected Collection rootCol;
! protected String testColName;
! protected Collection testCol;
protected String[] wordList;
+ protected List actions = new ArrayList(5);
+
protected volatile boolean failed = false;
/**
! * @param name the name of the test.
! * @param uri the XMLDB URI of the root collection.
! * @param testCollection the name of the collection that will be created for the test.
*/
public ConcurrentTestBase(String name, String uri, String testCollection) {
super(name);
! this.rootColURI = uri;
! this.testColName = testCollection;
}
! /**
! * Add an {@link Action} to the list of actions that will be processed
! * concurrently. Should be called after {@link #setUp()}.
! *
! * @param action the action.
! * @param repeat number of times the actions should be repeated.
! */
! public void addAction(Action action, int repeat, long delay) {
! actions.add(new Runner(action, repeat, delay));
! }
!
! public Collection getTestCollection() {
! return testCol;
! }
!
! public void testConcurrent() throws Exception {
! // start all threads
! for(int i = 0; i < actions.size(); i++) {
! Thread t = (Thread)actions.get(i);
! t.start();
! }
!
! // wait for threads to finish
! try {
! for(int i = 0; i < actions.size(); i++) {
! Thread t = (Thread)actions.get(i);
! t.join();
! }
! } catch(Exception e) {
! e.printStackTrace();
! }
!
! assertFalse(failed);
! }
/*
***************
*** 58,73 ****
*/
protected void setUp() throws Exception {
! root = DBUtils.setupDB(uri);
! wordList = DBUtils.wordList(root);
! File f = DBUtils.generateXMLFile("testdoc.xml", 1000, 10, wordList);
! Collection c1 = root.getChildCollection(testCol);
! if(c1 != null) {
! CollectionManagementService mgr = DBUtils.getCollectionManagementService(root);
! mgr.removeCollection(testCol);
}
! c1 = DBUtils.addCollection(root, testCol);
! DBUtils.addXMLResource(c1, "R1.xml", f);
}
--- 98,112 ----
*/
protected void setUp() throws Exception {
! rootCol = DBUtils.setupDB(rootColURI);
!
! testCol = rootCol.getChildCollection(testColName);
! if(testCol != null) {
! CollectionManagementService mgr = DBUtils.getCollectionManagementService(rootCol);
! mgr.removeCollection(testColName);
}
! testCol = DBUtils.addCollection(rootCol, testColName);
! assertNotNull(testCol);
}
***************
*** 76,92 ****
*/
protected void tearDown() throws Exception {
! DBUtils.removeCollection(root, testCol);
! DBUtils.shutdownDB(uri);
}
class Runner extends Thread {
private Action action;
private int repeat;
! public Runner(Action action, int repeat) {
super();
this.action = action;
this.repeat = repeat;
}
--- 115,138 ----
*/
protected void tearDown() throws Exception {
! DBUtils.removeCollection(rootCol, testColName);
! DBUtils.shutdownDB(rootColURI);
}
+ /**
+ * Runs the specified Action a number of times.
+ *
+ * @author wolf
+ */
class Runner extends Thread {
private Action action;
private int repeat;
+ private long delay = 0;
! public Runner(Action action, int repeat, long delay) {
super();
this.action = action;
this.repeat = repeat;
+ this.delay = delay;
}
***************
*** 105,108 ****
--- 151,158 ----
failed = action.execute();
+ if(delay > 0)
+ synchronized(this) {
+ wait(delay);
+ }
}
}
Index: ConcurrentXUpdateTest.java
===================================================================
RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xmldb/test/concurrent/ConcurrentXUpdateTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** ConcurrentXUpdateTest.java 18 Sep 2004 10:51:45 -0000 1.1
--- ConcurrentXUpdateTest.java 18 Sep 2004 12:24:23 -0000 1.2
***************
*** 21,25 ****
package org.exist.xmldb.test.concurrent;
! import org.xmldb.api.base.Collection;
/**
--- 21,26 ----
package org.exist.xmldb.test.concurrent;
! import java.io.File;
!
/**
***************
*** 32,63 ****
private final static String URI = "xmldb:exist:///db";
- private Collection root;
- private String[] wordList;
-
- private volatile boolean failed = false;
-
public static void main(String[] args) {
junit.textui.TestRunner.run(ConcurrentXUpdateTest.class);
}
public ConcurrentXUpdateTest(String name) {
super(name, URI, "C1");
}
! public void testConcurrent() throws Exception {
! Thread t1 = new Runner(new RemoveAppendAction(URI + "/C1", "R1.xml", wordList), 50);
! Thread t2 = new Runner(new RemoveAppendAction(URI + "/C1", "R1.xml", wordList), 50);
!
! t1.start();
! t2.start();
! try {
! t1.join();
! t2.join();
! } catch(Exception e) {
! e.printStackTrace();
! }
! assertFalse(failed);
}
}
--- 33,64 ----
private final static String URI = "xmldb:exist:///db";
public static void main(String[] args) {
junit.textui.TestRunner.run(ConcurrentXUpdateTest.class);
}
+ private File tempFile;
+
public ConcurrentXUpdateTest(String name) {
super(name, URI, "C1");
}
! protected void setUp() throws Exception {
! super.setUp();
! String[] wordList = DBUtils.wordList(rootCol);
! tempFile = DBUtils.generateXMLFile(1000, 10, wordList);
! DBUtils.addXMLResource(getTestCollection(), "R1.xml", tempFile);
! addAction(new RemoveAppendAction(URI + "/C1", "R1.xml", wordList), 10, 500);
! addAction(new RemoveAppendAction(URI + "/C1", "R1.xml", wordList), 10, 500);
! addAction(new RetrieveResourceAction(URI + "/C1", "R1.xml"), 10, 1000);
! }
!
! /* (non-Javadoc)
! * @see org.exist.xmldb.test.concurrent.ConcurrentTestBase#tearDown()
! */
! protected void tearDown() throws Exception {
! super.tearDown();
! tempFile.delete();
}
}
--- NEW FILE: AllTests.java ---
/*
* eXist Open Source Native XML Database
* Copyright (C) 2001-04 Wolfgang M. Meier (wolfgang@...)
* and others (see http://exist-db.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: AllTests.java,v 1.1 2004/09/18 12:24:23 wolfgang_m Exp $
*/
package org.exist.xmldb.test.concurrent;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
public class AllTests {
public static Test suite() {
TestSuite suite = new TestSuite("Test suite for org.exist.xmldb.test.concurrent");
//$JUnit-BEGIN$
suite.addTest(new TestSuite(ConcurrentResourceTest.class));
suite.addTest(new TestSuite(ConcurrentXUpdateTest.class));
//$JUnit-END$
return suite;
}
public static void main(String[] args) {
TestRunner.run(suite());
}
}
--- NEW FILE: ReplaceResourceAction.java ---
/*
* eXist Open Source Native XML Database
* Copyright (C) 2001-04 Wolfgang M. Meier (wolfgang@...)
* and others (see http://exist-db.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: ReplaceResourceAction.java,v 1.1 2004/09/18 12:24:23 wolfgang_m Exp $
*/
package org.exist.xmldb.test.concurrent;
import java.io.File;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
/**
* Replace an existing resource.
*
* @author wolf
*/
public class ReplaceResourceAction extends Action {
private String[] wordList;
private File tempFile;
/**
* @param collectionPath
* @param resourceName
*/
public ReplaceResourceAction(String collectionPath, String resourceName, String[] wordList) {
super(collectionPath, resourceName);
this.wordList = wordList;
}
/* (non-Javadoc)
* @see org.exist.xmldb.test.concurrent.Action#execute()
*/
public boolean execute() throws Exception {
Collection col = DatabaseManager.getCollection(collectionPath);
tempFile = DBUtils.generateXMLFile(1000, 10, wordList);
try {
DBUtils.addXMLResource(col, "R1.xml", tempFile);
return false;
} finally {
tempFile.delete();
}
}
}
Index: RemoveAppendAction.java
===================================================================
RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xmldb/test/concurrent/RemoveAppendAction.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** RemoveAppendAction.java 18 Sep 2004 10:51:45 -0000 1.1
--- RemoveAppendAction.java 18 Sep 2004 12:24:23 -0000 1.2
***************
*** 27,32 ****
/**
! * Removes the last element from the resource and inserts a
! * new element at the top.
*
* @author wolf
--- 27,32 ----
/**
! * Removes the 10 last elements from the resource and inserts 10
! * new elements at the top.
*
* @author wolf
***************
*** 60,74 ****
private void remove(XUpdateQueryService service) throws XMLDBException {
! service.update(REMOVE);
}
private void append(XUpdateQueryService service) throws Exception {
! String update =
"<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" +
! "<xu:append select=\"/ROOT-ELEMENT\" child=\"1\">" +
! gen.generateElement() +
"</xu:append>" +
"</xu:modifications>";
! service.update(update);
}
}
\ No newline at end of file
--- 60,80 ----
private void remove(XUpdateQueryService service) throws XMLDBException {
! System.out.println(Thread.currentThread().getName() + ": removing elements ...");
! for(int i = 0; i < 10; i++)
! service.update(REMOVE);
}
private void append(XUpdateQueryService service) throws Exception {
! final String updateOpen =
"<xu:modifications xmlns:xu=\"http://www.xmldb.org/xupdate\" version=\"1.0\">" +
! "<xu:append select=\"/ROOT-ELEMENT\" child=\"1\">";
! final String updateClose =
"</xu:append>" +
"</xu:modifications>";
! System.out.println(Thread.currentThread().getName() + ": inserting elements ...");
! for (int i = 0; i < 10; i++) {
! String update = updateOpen + gen.generateElement() + updateClose;
! service.update(update);
! }
}
}
\ No newline at end of file
--- NEW FILE: ConcurrentResourceTest.java ---
/*
* eXist Open Source Native XML Database
* Copyright (C) 2001-04 Wolfgang M. Meier (wolfgang@...)
* and others (see http://exist-db.org)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: ConcurrentResourceTest.java,v 1.1 2004/09/18 12:24:23 wolfgang_m Exp $
*/
package org.exist.xmldb.test.concurrent;
import java.io.File;
/**
* @author wolf
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ConcurrentResourceTest extends ConcurrentTestBase {
private final static String URI = "xmldb:exist:///db";
public static void main(String[] args) {
junit.textui.TestRunner.run(ConcurrentResourceTest.class);
}
private File tempFile;
/**
* @param name
* @param uri
* @param testCollection
*/
public ConcurrentResourceTest(String name) {
super(name, URI, "C1");
}
/* (non-Javadoc)
* @see org.exist.xmldb.test.concurrent.ConcurrentTestBase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
String[] wordList = DBUtils.wordList(rootCol);
tempFile = DBUtils.generateXMLFile(1000, 10, wordList);
DBUtils.addXMLResource(getTestCollection(), "R1.xml", tempFile);
addAction(new ReplaceResourceAction(URI + "/C1", "R1.xml", wordList), 10, 200);
addAction(new RetrieveResourceAction(URI + "/C1", "R1.xml"), 10, 500);
// TODO: using an addition replace thread generates a deadlock condition !!!
// addAction(new ReplaceResourceAction(URI + "/C1", "R1.xml", wordList), 10, 300);
}
/* (non-Javadoc)
* @see org.exist.xmldb.test.concurrent.ConcurrentTestBase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
tempFile.delete();
}
}
Index: DBUtils.java
===================================================================
RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/xmldb/test/concurrent/DBUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** DBUtils.java 18 Sep 2004 10:51:45 -0000 1.1
--- DBUtils.java 18 Sep 2004 12:24:23 -0000 1.2
***************
*** 65,72 ****
}
! public static File generateXMLFile(String outputFile, int elementCnt, int attrCnt, String[] wordList) throws Exception {
! File file = new File(outputFile);
if(file.exists() && !file.canWrite())
! throw new IllegalArgumentException("Cannot write to output file " + outputFile);
Writer writer = new BufferedWriter(new FileWriter(file));
--- 65,74 ----
}
! public static File generateXMLFile(int elementCnt, int attrCnt, String[] wordList) throws Exception {
! File file = File.createTempFile(Thread.currentThread().getName(), ".xml");
if(file.exists() && !file.canWrite())
! throw new IllegalArgumentException("Cannot write to output file " + file.getAbsolutePath());
!
! System.out.println("Generating XML file " + file.getAbsolutePath());
Writer writer = new BufferedWriter(new FileWriter(file));
|