📄 testconcurrentoperations.java
字号:
for (int i = 0; i < count; i++) {
Throwable e = threads[i].getThrowable();
if (e instanceof CmsVfsException) {
// an exception was thrown, so this thread can not have created a resource
c++;
}
if (e instanceof CmsVfsResourceAlreadyExistsException) {
CmsVfsResourceAlreadyExistsException e2 = (CmsVfsResourceAlreadyExistsException)e;
if (e2.getMessageContainer().getKey() == org.opencms.db.generic.Messages.ERR_RESOURCE_WITH_NAME_CURRENTLY_CREATED_1) {
// this is the expected "concurrent creation" exception
ec++;
} else {
// also check the cause, may be nested
e = e2.getCause();
if (e instanceof CmsVfsResourceAlreadyExistsException) {
e2 = (CmsVfsResourceAlreadyExistsException)e;
if (e2.getMessageContainer().getKey() == org.opencms.db.generic.Messages.ERR_RESOURCE_WITH_NAME_CURRENTLY_CREATED_1) {
// this is the expected "concurrent creation" exception
ec++;
}
}
}
}
}
// now read all resources in the folder and check if duplicates have been created
List resources = cms.readResources("/testfolder/sub1/sub2/sub3/", CmsResourceFilter.ALL);
Iterator i = resources.iterator();
Set names = new HashSet();
List duplicates = new ArrayList();
while (i.hasNext()) {
CmsResource res = (CmsResource)i.next();
if (names.contains(res.getRootPath())) {
// found a duplicate name
duplicates.add(res);
} else {
// this is no duplicate
names.add(res.getRootPath());
}
}
i = duplicates.iterator();
int c2 = 0;
while (i.hasNext()) {
// ouput duplicate list to console
c2++;
CmsResource res = (CmsResource)i.next();
System.err.println("Duplicate resource " + c2 + " : " + res.getRootPath() + " - " + res.getStructureId());
}
if (duplicates.size() > 0) {
// ducplicates where found
fail("There where " + duplicates.size() + " duplicate resources created");
}
if (c != (count - value[0].intValue())) {
// not the right number of exception where thrown
fail("Exception count " + c + " id not return the expected result " + (count - value[0].intValue()));
}
if (ec != 0) {
// the "concurrent creation" exception was thrown one or mote times - this must be an error
fail("Did catch concurrent creation exception at least once - no concurrent exceptions expected!");
}
echo("Concurrent folder creation test success: No duplicates created - "
+ ec
+ " concurrent modification exceptions caught");
echo("Total runtime of concurrent test suite: " + CmsStringUtil.formatRuntime(suite.getRuntime()));
}
/**
* Test concurrently publish same project.<p>
*
* @throws Throwable if something goes wrong
*/
public void testConcurrentPublishProject() throws Throwable {
int count = 10;
CmsObject cms = getCmsObject();
echo("Testing concurrently publish same project with " + count + " threads");
String resName = "/";
cms.lockResource(resName);
cms.setDateLastModified(resName, System.currentTimeMillis(), true);
// publish directly with a single resource
String name = "doConcurrentPublishProjectOperation";
Integer[] value = new Integer[1];
value[0] = new Integer(0);
Object[] parameters = new Object[] {
OpenCmsThreadedTestCaseSuite.PARAM_CMSOBJECT,
OpenCmsThreadedTestCaseSuite.PARAM_COUNTER,
value};
OpenCmsThreadedTestCaseSuite suite = new OpenCmsThreadedTestCaseSuite(count, this, name, parameters);
suite.setAllowedRuntime(20000);
OpenCmsThreadedTestCase[] threads = suite.run();
if (suite.getThrowable() != null) {
throw new Exception(suite.getThrowable());
}
OpenCms.getPublishManager().waitWhileRunning();
for (int i = 0; i < count; i++) {
Throwable e = threads[i].getThrowable();
if (e != null) {
throw new Exception(e);
}
}
List pubHistory = OpenCms.getPublishManager().getPublishHistory();
assertEquals(10, pubHistory.size());
CmsPublishJobFinished pubJob = (CmsPublishJobFinished)pubHistory.get(0);
assertEquals(13 + 51, pubJob.getSize());
for (int i = 1; i < 10; i++) {
pubJob = (CmsPublishJobFinished)pubHistory.get(i);
assertEquals("pubJob: " + i, 0, pubJob.getSize());
}
echo("Concurrent publish project test success");
echo("Total runtime of concurrent test suite: " + CmsStringUtil.formatRuntime(suite.getRuntime()));
}
/**
* Test concurrently publish same resource.<p>
*
* @throws Throwable if something goes wrong
*/
public void testConcurrentPublishResource() throws Throwable {
int count = 10;
CmsObject cms = getCmsObject();
echo("Testing concurrently publish same resource with " + count + " threads");
String resName = "index.html";
cms.lockResource(resName);
cms.setDateLastModified(resName, System.currentTimeMillis(), true);
// publish directly with a single resource
String name = "doConcurrentPublishResourceOperation";
Integer[] value = new Integer[1];
value[0] = new Integer(0);
Object[] parameters = new Object[] {
OpenCmsThreadedTestCaseSuite.PARAM_CMSOBJECT,
OpenCmsThreadedTestCaseSuite.PARAM_COUNTER,
value};
OpenCmsThreadedTestCaseSuite suite = new OpenCmsThreadedTestCaseSuite(count, this, name, parameters);
suite.setAllowedRuntime(10000);
OpenCmsThreadedTestCase[] threads = suite.run();
if (suite.getThrowable() != null) {
throw new Exception(suite.getThrowable());
}
OpenCms.getPublishManager().waitWhileRunning();
for (int i = 0; i < count; i++) {
Throwable e = threads[i].getThrowable();
if (e != null) {
throw new Exception(e);
}
}
List pubHistory = OpenCms.getPublishManager().getPublishHistory();
assertEquals(10, pubHistory.size());
CmsPublishJobFinished pubJob = (CmsPublishJobFinished)pubHistory.get(0);
assertEquals(1, pubJob.getSize());
for (int i = 1; i < 10; i++) {
pubJob = (CmsPublishJobFinished)pubHistory.get(i);
assertEquals("pubJob: " + i, 0, pubJob.getSize());
}
echo("Concurrent publish resource test success");
echo("Total runtime of concurrent test suite: " + CmsStringUtil.formatRuntime(suite.getRuntime()));
}
/**
* Test concurrently publish same resource with related resources, this does the same than the GUI.<p>
*
* @throws Throwable if something goes wrong
*/
public void testConcurrentPublishResourceWithRelated() throws Throwable {
int count = 10;
CmsObject cms = getCmsObject();
echo("Testing concurrently publish same resource with related resources and with " + count + " threads");
String resName = "index.html";
// touch everything, so the involved resources can be published
cms.lockResource("/");
cms.setDateLastModified("/", System.currentTimeMillis(), true);
// get the list of involved files
CmsPublishList publishList = OpenCms.getPublishManager().getPublishList(cms, cms.readResource(resName), false);
CmsPublishList relatedList = OpenCms.getPublishManager().getRelatedResourcesToPublish(cms, publishList);
publishList = OpenCms.getPublishManager().mergePublishLists(cms, publishList, relatedList);
// publish directly with a single resource
String name = "doConcurrentPublishResourceWithRelatedOperation";
Integer[] value = new Integer[1];
value[0] = new Integer(0);
Object[] parameters = new Object[] {
OpenCmsThreadedTestCaseSuite.PARAM_CMSOBJECT,
OpenCmsThreadedTestCaseSuite.PARAM_COUNTER,
value};
OpenCmsThreadedTestCaseSuite suite = new OpenCmsThreadedTestCaseSuite(count, this, name, parameters);
suite.setAllowedRuntime(20000);
OpenCmsThreadedTestCase[] threads = suite.run();
if (suite.getThrowable() != null) {
throw new Exception(suite.getThrowable());
}
OpenCms.getPublishManager().waitWhileRunning();
for (int i = 0; i < count; i++) {
Throwable e = threads[i].getThrowable();
if (e != null) {
throw new Exception(e);
}
}
List pubHistory = OpenCms.getPublishManager().getPublishHistory();
assertEquals(10, pubHistory.size());
CmsPublishJobFinished pubJob = (CmsPublishJobFinished)pubHistory.get(0);
assertEquals(publishList.getFileList().size(), pubJob.getSize());
for (int i = 1; i < 10; i++) {
pubJob = (CmsPublishJobFinished)pubHistory.get(i);
assertEquals("pubJob: " + i, 0, pubJob.getSize());
}
echo("Concurrent publish resource test success");
echo("Total runtime of concurrent test suite: " + CmsStringUtil.formatRuntime(suite.getRuntime()));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -