summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlivier Girardot2010-08-10 07:41:51 (EDT)
committer Steve Powell2010-08-10 09:38:39 (EDT)
commit3f475aa14aa9a6b70736345143991471e3400a03 (patch)
tree5a0bf86c2f042b72ae4097e47324c4df6c82dcca
parentb3371b81c9e8558cbd6c06a189a9bedc98e7681b (diff)
downloadorg.eclipse.virgo.kernel-3f475aa14aa9a6b70736345143991471e3400a03.zip
org.eclipse.virgo.kernel-3f475aa14aa9a6b70736345143991471e3400a03.tar.gz
org.eclipse.virgo.kernel-3f475aa14aa9a6b70736345143991471e3400a03.tar.bz2
New BlockingSignalTests contribution. (Bugzilla 321397).
-rw-r--r--org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/core/BlockingSignalTests.java124
1 files changed, 124 insertions, 0 deletions
diff --git a/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/core/BlockingSignalTests.java b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/core/BlockingSignalTests.java
new file mode 100644
index 0000000..362a924
--- /dev/null
+++ b/org.eclipse.virgo.kernel.core/src/test/java/org/eclipse/virgo/kernel/core/BlockingSignalTests.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2010 Olivier Girardot
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Olivier Girardot - initial contribution
+ */
+
+package org.eclipse.virgo.kernel.core;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.Vector;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Test;
+
+/**
+ * This class is for testing the {@link Signal} implementation {@link BlockingSignal}.
+ *
+ */
+public final class BlockingSignalTests {
+
+ @Test
+ public void signalCompletionAfterNoFailure() throws Throwable {
+ final BlockingSignal signal = new BlockingSignal();
+ ExceptionCatcherThread testThread = new ExceptionCatcherThread(new Runnable(){
+ public void run() {
+ try {
+ // awaiting for signal completion flag
+ boolean returnSignal =
+ signal.awaitCompletion(1000, TimeUnit.SECONDS);
+ assertTrue(returnSignal);
+ } catch (FailureSignalledException e) {
+ // this code should not be reached
+ fail();
+ }
+ }
+ });
+ testThread.start();
+ signal.signalSuccessfulCompletion();
+ testThread.join(10);
+ assertFalse("Test thread still alive after delay.", testThread.isAlive());
+ testThread.rethrowUncaughtExceptions();
+ }
+
+ @Test
+ public void signalCompletionFailsAfterWaitingExceeded() throws Throwable {
+ final BlockingSignal signal = new BlockingSignal();
+ ExceptionCatcherThread testThread = new ExceptionCatcherThread(new Runnable(){
+ public void run() {
+ try {
+ // awaiting for signal completion flag
+ boolean returnSignal =
+ signal.awaitCompletion(1, TimeUnit.MILLISECONDS);
+ assertFalse(returnSignal);
+ } catch (FailureSignalledException e) {
+ // this code should not be reached
+ fail();
+ }
+ }
+ });
+ testThread.start();
+ Thread.sleep(100);
+ signal.signalSuccessfulCompletion();
+ testThread.join(10);
+ assertFalse("Test thread still alive after delay.", testThread.isAlive());
+ testThread.rethrowUncaughtExceptions();
+ }
+
+ @Test
+ public void signalCompletionFailsAfterFailureNotifiedToSignal() throws Throwable {
+ final BlockingSignal signal = new BlockingSignal();
+ final Throwable dummyEx = new Exception("Dummy cause");
+ ExceptionCatcherThread testThread = new ExceptionCatcherThread(new Runnable(){
+ public void run() {
+ try {
+ // awaiting for signal completion flag (not storing result, as the code should fail)
+ signal.awaitCompletion(1, TimeUnit.SECONDS);
+
+ // this code should not be reached
+ // an exception being sent before
+ fail();
+ } catch (FailureSignalledException e) {
+ // We'll check that we actually refer to the correct cause
+ assertSame("Signal failure has incorrect cause.", dummyEx, e.getCause());
+ }
+ }
+ });
+ testThread.start();
+ signal.signalFailure(dummyEx);
+ testThread.join(10);
+ assertFalse("Test thread still alive after delay.", testThread.isAlive());
+ testThread.rethrowUncaughtExceptions();
+ }
+
+ /**
+ * Special thread designed to record uncaught exceptions
+ * and re-throw the first of them on demand.
+ */
+ private class ExceptionCatcherThread extends Thread{
+ private final Vector<Throwable> uncaughtExceptions = new Vector<Throwable>();
+
+ public ExceptionCatcherThread(Runnable r) {
+ super(r);
+ this.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
+ public void uncaughtException(Thread t, Throwable e) {
+ uncaughtExceptions.add(e);
+ }
+ });
+ }
+
+ public void rethrowUncaughtExceptions() throws Throwable {
+ if (!uncaughtExceptions.isEmpty())
+ throw uncaughtExceptions.firstElement();
+ }
+ }
+} \ No newline at end of file