Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;

import org.junit.Ignore;
import org.junit.Test;

Expand Down Expand Up @@ -116,5 +116,7 @@ public Integer call() {
for (final Future<Integer> future : futures) {
Assert.assertEquals(REPEAT, future.get().intValue());
}
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

Expand Down Expand Up @@ -106,5 +107,7 @@ public Integer call() {
for (final Future<Integer> future : futures) {
future.get();
}
threadPool.shutdown();
threadPool.awaitTermination(1, TimeUnit.SECONDS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package org.apache.commons.lang3.concurrent;

import org.junit.Test;

import static org.junit.Assert.*;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -66,7 +68,7 @@ public void testGetActiveExecutorBeforeStart() {
* Tests whether an external executor is correctly detected.
*/
@Test
public void testGetActiveExecutorExternal() {
public void testGetActiveExecutorExternal() throws InterruptedException {
final ExecutorService exec = Executors.newSingleThreadExecutor();
try {
final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl(
Expand All @@ -76,6 +78,7 @@ public void testGetActiveExecutorExternal() {
checkInitialize(init);
} finally {
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
}
}

Expand Down Expand Up @@ -130,14 +133,18 @@ public void testSetExternalExecutor() {
* @throws org.apache.commons.lang3.concurrent.ConcurrentException because the test implementation may throw it
*/
@Test
public void testSetExternalExecutorAfterStart() throws ConcurrentException {
public void testSetExternalExecutorAfterStart() throws ConcurrentException, InterruptedException {
final BackgroundInitializerTestImpl init = new BackgroundInitializerTestImpl();
init.start();
ExecutorService exec = Executors.newSingleThreadExecutor();
try {
init.setExternalExecutor(Executors.newSingleThreadExecutor());
init.setExternalExecutor(exec);
fail("Could set executor after start()!");
} catch (final IllegalStateException istex) {
init.get();
} finally {
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
}
}

Expand Down Expand Up @@ -235,7 +242,7 @@ public void run() {
getThread.interrupt();
latch1.await();
exec.shutdownNow();
exec.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
exec.awaitTermination(1, TimeUnit.SECONDS);
assertNotNull("No interrupted exception", iex.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

Expand All @@ -47,21 +48,29 @@ public void testInitNullCallable() {
* class.
*/
@Test
public void testInitExecutor() {
public void testInitExecutor() throws InterruptedException {
final ExecutorService exec = Executors.newSingleThreadExecutor();
final CallableBackgroundInitializer<Integer> init = new CallableBackgroundInitializer<Integer>(
new TestCallable(), exec);
assertEquals("Executor not set", exec, init.getExternalExecutor());
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
}

/**
* Tries to pass a null Callable to the constructor that takes an executor.
* This should cause an exception.
*/
@Test(expected=IllegalArgumentException.class)
public void testInitExecutorNullCallable() {
public void testInitExecutorNullCallable() throws InterruptedException {
final ExecutorService exec = Executors.newSingleThreadExecutor();
new CallableBackgroundInitializer<Integer>(null, exec);
try {
new CallableBackgroundInitializer<Integer>(null, exec);
} finally {
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.NoSuchElementException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -152,7 +153,7 @@ public void testInitializeTempExec() throws ConcurrentException {
* @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
*/
@Test
public void testInitializeExternalExec() throws ConcurrentException {
public void testInitializeExternalExec() throws ConcurrentException, InterruptedException {
final ExecutorService exec = Executors.newCachedThreadPool();
try {
initializer = new MultiBackgroundInitializer(exec);
Expand All @@ -162,6 +163,7 @@ public void testInitializeExternalExec() throws ConcurrentException {
assertFalse("Executor was shutdown", exec.isShutdown());
} finally {
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
}
}

Expand All @@ -172,7 +174,7 @@ public void testInitializeExternalExec() throws ConcurrentException {
* @throws org.apache.commons.lang3.concurrent.ConcurrentException so we don't have to catch it
*/
@Test
public void testInitializeChildWithExecutor() throws ConcurrentException {
public void testInitializeChildWithExecutor() throws ConcurrentException, InterruptedException {
final String initExec = "childInitializerWithExecutor";
final ExecutorService exec = Executors.newSingleThreadExecutor();
try {
Expand All @@ -187,6 +189,7 @@ public void testInitializeChildWithExecutor() throws ConcurrentException {
checkChild(c2, exec);
} finally {
exec.shutdown();
exec.awaitTermination(1, TimeUnit.SECONDS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void testCheckDefaults() {
assertEquals(sdf.toPattern(), format.getPattern());

assertEquals(Locale.getDefault(), format.getLocale());
assertEquals(TimeZone.getDefault(), format.getTimeZone());
assertEquals(TimeZone.getDefault(), format.getTimeZone());
}

@Test
Expand All @@ -208,7 +208,7 @@ public void testCheckDifferingStyles() {

assertFalse(shortShort.equals(shortLong));
assertFalse(shortShort.equals(longShort));
assertFalse(shortShort.equals(longLong));
assertFalse(shortShort.equals(longLong));
assertFalse(shortLong.equals(longShort));
assertFalse(shortLong.equals(longLong));
assertFalse(longShort.equals(longLong));
Expand Down Expand Up @@ -308,11 +308,14 @@ public void run() {
e.printStackTrace();
}
}
}
}
});
}
pool.shutdown();
if(!pool.awaitTermination(20, TimeUnit.SECONDS)) {
pool.shutdown();
// depending on the performance of the machine used to run the parsing,
// the tests can run for a while. It should however complete within
// 30 seconds. Might need increase on very slow machines.
if(!pool.awaitTermination(30, TimeUnit.SECONDS)) {
pool.shutdownNow();
fail("did not complete tasks");
}
Expand Down