Skip to content

fixed issue when creating custom executor from system property #55 #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sbt.Keys.{javacOptions, scalaVersion}

organization := "org.dmonix.functional"
version := "1.11.0"
version := "1.11.1"
name := "java-scala-utils"

libraryDependencies ++= Seq(
Expand Down
19 changes: 15 additions & 4 deletions src/main/java/javascalautils/concurrent/Executors.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
*/
public final class Executors {

public static final String ExecutorProviderClass = "javascalautils.concurrent.executorprovider";

/** The default executor. */
private static final Executor DefaultExecutor = createDefaultExecutor();

Expand Down Expand Up @@ -86,11 +88,20 @@ public static Executor create(java.util.concurrent.Executor threadPool) {
*
* @return The created executor
*/
private static Executor createDefaultExecutor() {
Try<Executor> t =
ReflectionUtil.newInstance(
System.getProperty("javascalautils.concurrent.executorprovider"));
static Executor createDefaultExecutor() {
Try<Executor> t = ReflectionUtil.newInstance(System.getProperty(ExecutorProviderClass));
return t.getOrElse(
() -> createCachedThreadPoolExecutor(new NamedSequenceThreadFactory("Executors-Default")));
}

/**
* Creates the default {@link Executor} instance. <br>
* Either by getting the provider class from the system property or using the default.
*
* @return The created executor
*/
static Try<Executor> createExecutorFromProvider(String executorProviderClass) {
return ReflectionUtil.<ExecutorProvider>newInstance(executorProviderClass)
.map(ExecutorProvider::create);
}
}
1 change: 0 additions & 1 deletion src/test/java/javascalautils/BaseAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ public static void assertEquals(int[] expected, int[] actual) {
* Assert that a collection is empty.
*
* @param collection
* @param expectedSize
*/
public static void assertIsEmpty(Collection<?> collection) {
assertNotNull(collection);
Expand Down
53 changes: 45 additions & 8 deletions src/test/java/javascalautils/concurrent/TestExecutors.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,82 @@
package javascalautils.concurrent;

import javascalautils.BaseAssert;
import javascalautils.Try;
import javascalautils.TryAsserts;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

import static javascalautils.TryCompanion.Try;
/**
* Test the class {@link Executors}.
*
* @author name Peter Nerg
*/
public class TestExecutors extends BaseAssert {
public class TestExecutors extends BaseAssert implements TryAsserts {
@Test
public void createInstance() throws ReflectiveOperationException {
assertPrivateConstructor(Executors.class);
}

@Test
public void createCachedThreadPoolExecutor() throws InterruptedException {
public void createCachedThreadPoolExecutor() {
Executor executor =
Executors.createCachedThreadPoolExecutor(
r -> new Thread(r, "createCachedThreadPoolExecutor"));
destroyExecutor(executor);
}

@Test
public void createFixedThreadPoolExecutor() throws InterruptedException {
public void createFixedThreadPoolExecutor() {
Executor executor =
Executors.createFixedThreadPoolExecutor(
5, r -> new Thread(r, "createFixedThreadPoolExecutor"));
destroyExecutor(executor);
}

@Test
public void create() throws InterruptedException {
public void create() {
Executor executor = Executors.create(r -> r.run());
destroyExecutor(executor);
}

private void destroyExecutor(Executor executor) throws InterruptedException {
executor.shutdown();
executor.awaitTermination(666, TimeUnit.MILLISECONDS);
@Test
public void createDefaultExecutor_default() {
Executor executor = Executors.createDefaultExecutor();
destroyExecutor(executor);
}

@Test
public void createExecutorFromProvider() {
Try<Executor> t = Executors.createExecutorFromProvider(DummyExecutorProvider.class.getName());
assertIsSuccess(t);
t.forEach(TestExecutors::destroyExecutor);
}

@Test
public void createExecutorFromProvider_illegalclass() {
Try<Executor> t = Executors.createExecutorFromProvider("no-such-class");
assertIsFailure(t);
}

@Test
public void createExecutorFromProvider_null() {
Try<Executor> t = Executors.createExecutorFromProvider(null);
assertIsFailure(t);
}

private static void destroyExecutor(Executor executor) {
Try(
() -> {
executor.shutdown();
executor.awaitTermination(666, TimeUnit.MILLISECONDS);
});
}

static class DummyExecutorProvider implements ExecutorProvider {
@Override
public Executor create() {
return Executors.create(r -> r.run());
}
}
}