Search in sources :

Example 11 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project scheduling by ow2-proactive.

the class TestNodeSourceThreadPool method testNodeSourceDeploymentThreadsDoNotLeakWithInfiniteRetry.

@Test
public /*
     * This setup makes LOST nodes on one host and FREE nodes on another host.
     * We use a configuration of two threads for the nodes deployment, and an
     * infrastructure with infinite retry number for deploying. After cycling
     * two redeployment phases thanks to the policy, we expect the situation
     * to be:
     * - one thread being always taken by the infinite retry on the fake host
     * - one thread available to deploy with success/redeploy on the localhost
     * A timeout is thrown if a thread leak prevents the redeployment to happen
     */
void testNodeSourceDeploymentThreadsDoNotLeakWithInfiniteRetry() throws Exception {
    String rmConfig = new File(RMTHelper.class.getResource(SIX_THREADS_CONFIG_FILE_PATH).toURI()).getAbsolutePath();
    this.rmHelper.startRM(rmConfig);
    this.resourceManager = this.rmHelper.getResourceManager();
    this.resourceManager.defineNodeSource(NODE_SOURCE_NAME, SSHInfrastructureV2.class.getName(), getInfiniteRetryInfrastructureParameters(), RestartDownNodesPolicy.class.getName(), getRestartDownNodesPolicyParameters(), NODES_NOT_RECOVERABLE);
    this.resourceManager.deployNodeSource(NODE_SOURCE_NAME);
    RMTHelper.log("Waiting for the RM to have one free node");
    while (this.resourceManager.getState().getFreeNodesNumber() != ONE_NODE_PER_HOST) {
        Thread.sleep(ACTIVE_WAITING_PERIOD);
    }
    assertEquals(1, this.resourceManager.getState().getAliveNodes().size());
    for (int i = 0; i < 2; i++) {
        String freeNodeUrl = this.resourceManager.getState().getFreeNodes().iterator().next();
        RMTHelper.killRuntime(freeNodeUrl);
        RMMonitorsHandler monitor = this.rmHelper.getMonitorsHandler();
        monitor.flushEvents();
        RMTHelper.log("Waiting for the RM to detect the down node");
        RMNodeEvent nodeEvent = RMTHelper.waitForNodeEvent(RMEventType.NODE_STATE_CHANGED, freeNodeUrl, NODE_STATE_CHANGED_TIMEOUT, monitor);
        assertEquals(NodeState.DOWN, nodeEvent.getNodeState());
        RMTHelper.log("Waiting for the RM to redeploy the down node");
        int totalWaitingTime = 0;
        while (this.resourceManager.getState().getFreeNodesNumber() != ONE_NODE_PER_HOST) {
            Thread.sleep(ACTIVE_WAITING_PERIOD);
            totalWaitingTime += ACTIVE_WAITING_PERIOD;
            if (totalWaitingTime > NODE_STATE_CHANGED_TIMEOUT) {
                throw new TestTimedOutException(NODE_STATE_CHANGED_TIMEOUT, TimeUnit.MILLISECONDS);
            }
        }
    }
    assertEquals(1, this.resourceManager.getState().getAliveNodes().size());
}
Also used : RestartDownNodesPolicy(org.ow2.proactive.resourcemanager.nodesource.policy.RestartDownNodesPolicy) RMTHelper(functionaltests.utils.RMTHelper) SSHInfrastructureV2(org.ow2.proactive.resourcemanager.nodesource.infrastructure.SSHInfrastructureV2) File(java.io.File) RMNodeEvent(org.ow2.proactive.resourcemanager.common.event.RMNodeEvent) TestTimedOutException(org.junit.runners.model.TestTimedOutException) RMMonitorsHandler(functionaltests.monitor.RMMonitorsHandler) Test(org.junit.Test) RMFunctionalTest(functionaltests.utils.RMFunctionalTest)

Example 12 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project spf4j by zolyfarkas.

the class FailOnTimeout method createTimeoutException.

private Exception createTimeoutException(Thread thread) {
    StackTraceElement[] stackTrace = thread.getStackTrace();
    final Thread stuckThread = lookForStuckThread ? getStuckThread(thread) : null;
    Exception currThreadException = new TestTimedOutException(timeout, timeUnit);
    if (stackTrace != null) {
        currThreadException.setStackTrace(stackTrace);
        thread.interrupt();
    }
    if (stuckThread != null) {
        Exception stuckThreadException = new Exception("Appears to be stuck in thread " + stuckThread.getName());
        stuckThreadException.setStackTrace(getStackTrace(stuckThread));
        return new MultipleFailureException(Arrays.<Throwable>asList(currThreadException, stuckThreadException));
    } else {
        return currThreadException;
    }
}
Also used : MultipleFailureException(org.junit.runners.model.MultipleFailureException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) ExecutionException(java.util.concurrent.ExecutionException)

Example 13 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project indy by Commonjava.

the class ThreadDumper method timeoutRule.

public static TestRule timeoutRule(int timeout, TimeUnit units) {
    return (base, description) -> new Statement() {

        public void evaluate() throws Throwable {
            System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
            AtomicReference<Throwable> error = new AtomicReference<>();
            CountDownLatch latch = new CountDownLatch(1);
            FutureTask<Void> task = new FutureTask<>(() -> {
                try {
                    latch.countDown();
                    base.evaluate();
                } catch (Throwable t) {
                    error.set(t);
                }
                return null;
            });
            ThreadGroup tg = new ThreadGroup("Test Timeout Group");
            Thread t = new Thread(tg, task, "Test Timeout Thread");
            t.setDaemon(true);
            t.start();
            try {
                System.out.println("Waiting for test to start.");
                latch.await();
            } catch (InterruptedException e) {
                error.set(e);
            }
            if (error.get() == null) {
                try {
                    System.out.println("Waiting for test to complete (or timeout)");
                    task.get(timeout, units);
                } catch (InterruptedException e) {
                    error.set(e);
                } catch (ExecutionException e) {
                    error.set(e.getCause());
                } catch (TimeoutException e) {
                    System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
                    dumpThreads();
                    StackTraceElement[] stackTrace = t.getStackTrace();
                    Exception currThreadException = new TestTimedOutException(timeout, units);
                    if (stackTrace != null) {
                        currThreadException.setStackTrace(stackTrace);
                        t.interrupt();
                    }
                    throw currThreadException;
                }
            }
            Throwable throwable = error.get();
            if (throwable != null) {
                throw throwable;
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) TestRule(org.junit.rules.TestRule) MonitorInfo(java.lang.management.MonitorInfo) TestTimedOutException(org.junit.runners.model.TestTimedOutException) FutureTask(java.util.concurrent.FutureTask) TimeoutException(java.util.concurrent.TimeoutException) ThreadMXBean(java.lang.management.ThreadMXBean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) StringUtils.join(org.apache.commons.lang3.StringUtils.join) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadInfo(java.lang.management.ThreadInfo) Stream(java.util.stream.Stream) ManagementFactory(java.lang.management.ManagementFactory) Statement(org.junit.runners.model.Statement) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) FutureTask(java.util.concurrent.FutureTask) ExecutionException(java.util.concurrent.ExecutionException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 14 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project incubator-ratis by apache.

the class JUnitRunListener method main.

public static void main(String[] args) {
    final JUnitRunListener listener = new JUnitRunListener();
    listener.out.println("TIMEOUT_EXCEPTION_PREFIX = '" + TIMEOUT_EXCEPTION_PREFIX + "'");
    TIMEOUT_EXCEPTION.printStackTrace(listener.out);
    listener.testFailure(new Failure(null, new TestTimedOutException(999, TimeUnit.MILLISECONDS)));
}
Also used : TestTimedOutException(org.junit.runners.model.TestTimedOutException) Failure(org.junit.runner.notification.Failure)

Example 15 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project robolectric by robolectric.

the class TimeLimitedStatement method evaluate.

@Override
public void evaluate() throws Throwable {
    Thread testThread = Thread.currentThread();
    Thread timeoutThread = new Thread(() -> {
        try {
            Thread.sleep(timeout);
            testThread.interrupt();
        } catch (InterruptedException e) {
        // ok
        }
    }, "Robolectric time-limited test");
    timeoutThread.start();
    try {
        delegate.evaluate();
    } catch (InterruptedException e) {
        Exception e2 = new TestTimedOutException(timeout, TimeUnit.MILLISECONDS);
        e2.setStackTrace(e.getStackTrace());
        throw e2;
    } finally {
        timeoutThread.interrupt();
        timeoutThread.join();
    }
}
Also used : TestTimedOutException(org.junit.runners.model.TestTimedOutException) TestTimedOutException(org.junit.runners.model.TestTimedOutException)

Aggregations

TestTimedOutException (org.junit.runners.model.TestTimedOutException)19 ExecutionException (java.util.concurrent.ExecutionException)8 TimeoutException (java.util.concurrent.TimeoutException)8 Test (org.junit.Test)5 ManagementFactory (java.lang.management.ManagementFactory)4 MonitorInfo (java.lang.management.MonitorInfo)4 ThreadInfo (java.lang.management.ThreadInfo)4 ThreadMXBean (java.lang.management.ThreadMXBean)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 FutureTask (java.util.concurrent.FutureTask)4 TimeUnit (java.util.concurrent.TimeUnit)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Stream (java.util.stream.Stream)4 TestRule (org.junit.rules.TestRule)4 Statement (org.junit.runners.model.Statement)4 Failure (org.junit.runner.notification.Failure)3 MultipleFailureException (org.junit.runners.model.MultipleFailureException)3 StringUtils.join (org.apache.commons.lang.StringUtils.join)2 StringUtils.join (org.apache.commons.lang3.StringUtils.join)2 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)2