Skip to content

Commit

Permalink
moved public inner classes and interfaces in OrientDB Test Commons to…
Browse files Browse the repository at this point in the history
… appropriate places
  • Loading branch information
Karl-Philipp Richter committed Mar 6, 2015
1 parent 5e964ae commit 5d01bce
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 69 deletions.
Expand Up @@ -12,6 +12,7 @@
import org.testng.annotations.Test;

import com.orientechnologies.orient.test.ConcurrentTestHelper;
import com.orientechnologies.orient.test.TestFactory;

/**
* Concurrent test for {@link ConcurrentLRUList}.
Expand Down Expand Up @@ -112,7 +113,7 @@ private void consumeCPU(int cycles) {
c = c1;
}

private class AdderFactory implements ConcurrentTestHelper.TestFactory<Integer> {
private class AdderFactory implements TestFactory<Integer> {
private int j = 0;

@Override
Expand All @@ -131,7 +132,7 @@ public Integer call() throws Exception {
}
}

private class RemoveLRUFactory implements ConcurrentTestHelper.TestFactory<Integer> {
private class RemoveLRUFactory implements TestFactory<Integer> {
@Override
public Callable<Integer> createWorker() {
return new Callable<Integer>() {
Expand All @@ -152,7 +153,7 @@ public Integer call() throws Exception {
}
}

private class RandomAdderFactory implements ConcurrentTestHelper.TestFactory<Integer> {
private class RandomAdderFactory implements TestFactory<Integer> {

@Override
public Callable<Integer> createWorker() {
Expand All @@ -171,7 +172,7 @@ public Integer call() throws Exception {
}
}

private class AddSameFactory implements ConcurrentTestHelper.TestFactory<Integer> {
private class AddSameFactory implements TestFactory<Integer> {

@Override
public Callable<Integer> createWorker() {
Expand All @@ -190,7 +191,7 @@ public Integer call() throws Exception {
}
}

private class RandomRemoveFactory implements ConcurrentTestHelper.TestFactory<Integer> {
private class RandomRemoveFactory implements TestFactory<Integer> {
@Override
public Callable<Integer> createWorker() {
return new Callable<Integer>() {
Expand Down
@@ -0,0 +1,69 @@
/*
* Copyright 2015 Orient Technologies.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.orientechnologies.orient.test;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
*
* @author richter
*/
public class CompositeException extends RuntimeException {
private static final long serialVersionUID = 1L;
private final List<Throwable> causes = new ArrayList<Throwable>();

public CompositeException(Collection<? extends Throwable> causes) {
this.causes.addAll(causes);
}

@Override
public void printStackTrace() {
if (causes.isEmpty()) {
super.printStackTrace();
return;
}
for (Throwable cause : causes) {
cause.printStackTrace();
}
}

@Override
public void printStackTrace(PrintStream s) {
if (causes.isEmpty()) {
super.printStackTrace(s);
} else {
for (Throwable cause : causes) {
cause.printStackTrace(s);
}
}
}

@Override
public void printStackTrace(PrintWriter s) {
if (causes.isEmpty()) {
super.printStackTrace(s);
} else {
for (Throwable cause : causes) {
cause.printStackTrace(s);
}
}
}

}
Expand Up @@ -3,8 +3,6 @@
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -17,6 +15,7 @@

/**
* @author Artem Orobets (enisher-at-gmail.com)
* @param <T> see {@link TestFactory}
*/
public class ConcurrentTestHelper<T> {
private final ExecutorService executor;
Expand All @@ -27,15 +26,15 @@ public static <T> Collection<T> test(int threadCount, TestFactory<T> factory) {
return go(callables);
}

private static <T> Collection<T> go(List<Callable<T>> workers) {
protected static <T> Collection<T> go(List<Callable<T>> workers) {
final ConcurrentTestHelper<T> helper = new ConcurrentTestHelper<T>(workers.size());

helper.submit(workers);

return helper.assertSuccess();
}

private static <T> List<Callable<T>> prepareWorkers(int threadCount, TestFactory<T> factory) {
protected static <T> List<Callable<T>> prepareWorkers(int threadCount, TestFactory<T> factory) {
final List<Callable<T>> callables = new ArrayList<Callable<T>>(threadCount);
for (int i = 0; i < threadCount; i++) {
callables.add(factory.createWorker());
Expand Down Expand Up @@ -84,61 +83,4 @@ private ConcurrentTestHelper(int threadCount) {
this.executor = Executors.newFixedThreadPool(threadCount);
}

public interface TestFactory<T> {
Callable<T> createWorker();
}

public static class CompositeException extends RuntimeException {
private final List<Throwable> causes = new ArrayList<Throwable>();

public CompositeException(Collection<? extends Throwable> causes) {
this.causes.addAll(causes);
}

@Override
public void printStackTrace() {
if (causes.isEmpty()) {
super.printStackTrace();
return;
}
for (Throwable cause : causes) {
cause.printStackTrace();
}
}

@Override
public void printStackTrace(PrintStream s) {
if (causes.isEmpty()) {
super.printStackTrace(s);
} else {
for (Throwable cause : causes) {
cause.printStackTrace(s);
}
}
}

@Override
public void printStackTrace(PrintWriter s) {
if (causes.isEmpty()) {
super.printStackTrace(s);
} else {
for (Throwable cause : causes) {
cause.printStackTrace(s);
}
}
}
}

public static class TestBuilder<T> {
private List<Callable<T>> workers = new ArrayList<Callable<T>>();

public TestBuilder<T> add(int threadCount, TestFactory<T> factory) {
workers.addAll(prepareWorkers(threadCount, factory));
return this;
}

public Collection<T> go() {
return ConcurrentTestHelper.go(workers);
}
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2015 Orient Technologies.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.orientechnologies.orient.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;

/**
*
* @author richter
* @param <T> see {@link TestFactory}
*/
public class TestBuilder<T> {
private final List<Callable<T>> workers = new ArrayList<Callable<T>>();

public TestBuilder<T> add(int threadCount, TestFactory<T> factory) {
workers.addAll(ConcurrentTestHelper.prepareWorkers(threadCount, factory));
return this;
}

public Collection<T> go() {
return ConcurrentTestHelper.go(workers);
}

}
@@ -0,0 +1,30 @@
/*
* Copyright 2015 Orient Technologies.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.orientechnologies.orient.test;

import java.util.concurrent.Callable;

/**
*
* @author richter
* @param <T> the type of the {@link Callable} to be created with
* {@link #createWorker() }
*/
public interface TestFactory<T> {

Callable<T> createWorker();

}
Expand Up @@ -19,6 +19,7 @@
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.test.ConcurrentTestHelper;
import com.orientechnologies.orient.test.TestFactory;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Optional;
Expand Down Expand Up @@ -94,7 +95,7 @@ public void init() {
public void concurrentCommands() throws Exception {
// System.out.println("Spanning " + THREADS + " threads...");

ConcurrentTestHelper.test(THREADS, new ConcurrentTestHelper.TestFactory<Void>() {
ConcurrentTestHelper.test(THREADS, new TestFactory<Void>() {
@Override
public Callable<Void> createWorker() {
return new CommandExecutor(url);
Expand Down
Expand Up @@ -23,6 +23,7 @@
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.test.ConcurrentTestHelper;
import com.orientechnologies.orient.test.TestFactory;
import org.testng.Assert;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
Expand Down Expand Up @@ -110,7 +111,7 @@ public ConcurrentSchemaTest(@Optional String url) {
public void concurrentCommands() throws Exception {
// System.out.println("Create classes, spanning " + THREADS + " threads...");

ConcurrentTestHelper.test(THREADS, new ConcurrentTestHelper.TestFactory<Void>() {
ConcurrentTestHelper.test(THREADS, new TestFactory<Void>() {
@Override
public Callable<Void> createWorker() {
return new CreateClassCommandExecutor(url);
Expand All @@ -128,7 +129,7 @@ public Callable<Void> createWorker() {

// System.out.println("Dropping classes, spanning " + THREADS + " threads...");

ConcurrentTestHelper.test(THREADS, new ConcurrentTestHelper.TestFactory<Void>() {
ConcurrentTestHelper.test(THREADS, new TestFactory<Void>() {
@Override
public Callable<Void> createWorker() {
return new DropClassCommandExecutor(url);
Expand Down

0 comments on commit 5d01bce

Please sign in to comment.