patterntesting.concurrent.junit.internal.BackgroundRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of patterntesting-concurrent Show documentation
Show all versions of patterntesting-concurrent Show documentation
PatternTesting Concurrent (patterntesting-concurrent) is a collection
of useful thread aspects. It has support for testing, for
sychnronization and for concurrent programming.
Some of the ideas used in this library comes from reading
Brian Goetz's book "Java Concurrency in Practice".
/*
* $Id: BackgroundRunner.java,v 1.6 2014/12/21 17:29:34 oboehm Exp $
*
* Copyright (c) 2012 by Oliver Boehm
*
* 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 orimplied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* (c)reated 30.12.2011 by oliver ([email protected])
*/
package patterntesting.concurrent.junit.internal;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import patterntesting.runtime.junit.SmokeRunner;
import patterntesting.runtime.util.Environment;
/**
* The Class BackgroundRunner will run the wrapped Runner in background,
* record the results und plays them when they are needed.
* But not if threads are not allowed, e.g. if the system property
* {@link Environment#DISABLE_THREADS} is set. In this case the tests
* are started normal.
*
* @author oliver
* @since 1.2 (30.12.2011)
*/
public final class BackgroundRunner extends Runner {
private static final Logger log = LoggerFactory.getLogger(BackgroundRunner.class);
private static int threadNumber = 1;
private final Runner wrapped;
private final RecordingRunNotifier recorder = new RecordingRunNotifier();
private Thread thread;
private long threadStart;
/**
* Instantiates a new background runner.
*
* @param runner the runner
*/
public BackgroundRunner(final Runner runner) {
this.wrapped = runner;
}
/**
* Instantiates a new background runner.
*
* @param testClass the test class
* @throws InitializationError the initialization error
*/
public BackgroundRunner(final Class> testClass) throws InitializationError {
this(new SmokeRunner(testClass));
}
/**
* Gets the description.
*
* @return the description
* @see org.junit.runner.Runner#getDescription()
*/
@Override
public Description getDescription() {
return this.wrapped.getDescription();
}
/**
* Replays the recorded events.
*
* @param notifier the notifier
* @see org.junit.runner.Runner#run(org.junit.runner.notification.RunNotifier)
*/
@Override
public void run(final RunNotifier notifier) {
if (this.recorder.isEmpty()) {
log.trace("Running {} direct.", this.wrapped);
this.wrapped.run(notifier);
} else {
this.recorder.replay(notifier, this.thread);
log.info("{} ({} ms)", this.getDescription(), System.currentTimeMillis() - threadStart);
}
}
/**
* Starts the wrapped Runner in Background and records the events.
*/
public void startRunner() {
if (Environment.areThreadsAllowed()) {
Runnable work = new Runnable() {
public void run() {
log.info("Starting {} in background...", wrapped.getDescription());
wrapped.run(recorder);
}
};
this.threadStart = System.currentTimeMillis();
this.thread = startThread(work);
}
}
private static Thread startThread(final Runnable work) {
Thread t = new Thread(work, "bg-" + Integer.toHexString(threadNumber++));
t.start();
log.trace("{} started.", t);
return t;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy