未验证 提交 9b419100 编写于 作者: J Jesse Glick

AtmostOneTaskExecutorTest

上级 a8251f66
......@@ -796,25 +796,6 @@ THE SOFTWARE.
</archive>
</configuration>
</plugin>
<plugin><!-- run unit test in src/test/java -->
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generateTestStubs</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
......
package jenkins.util
import hudson.util.OneShotEvent
import org.junit.Test
import java.util.concurrent.Callable
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger
/**
*
*
* @author Kohsuke Kawaguchi
*/
class AtmostOneTaskExecutorTest {
def counter = new AtomicInteger()
def lock = new OneShotEvent()
@Test
public void doubleBooking() {
def f1,f2;
def base = Executors.newCachedThreadPool()
def es = new AtmostOneTaskExecutor(base,
{ ->
counter.incrementAndGet()
lock.block()
} as Callable);
f1 = es.submit()
while (counter.get() == 0)
; // spin lock until executor gets to the choking point
f2 = es.submit() // this should hang
Thread.sleep(500) // make sure the 2nd task is hanging
assert counter.get() == 1
assert !f2.isDone()
lock.signal() // let the first one go
f1.get(); // first one should complete
// now 2nd one gets going and hits the choke point
f2.get()
assert counter.get()==2
}
}
package jenkins.util;
import hudson.util.OneShotEvent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import static org.junit.Assert.*;
import org.junit.Test;
public class AtmostOneTaskExecutorTest {
@SuppressWarnings("empty-statement")
@Test
public void doubleBooking() throws Exception {
AtomicInteger counter = new AtomicInteger();
OneShotEvent lock = new OneShotEvent();
Future<?> f1, f2;
ExecutorService base = Executors.newCachedThreadPool();
AtmostOneTaskExecutor<?> es = new AtmostOneTaskExecutor<Void>(base, () -> {
counter.incrementAndGet();
try {
lock.block();
} catch (InterruptedException x) {
assert false : x;
}
return null;
});
f1 = es.submit();
while (counter.get() == 0) {
// spin lock until executor gets to the choking point
}
f2 = es.submit(); // this should hang
Thread.sleep(500); // make sure the 2nd task is hanging
assertEquals(1, counter.get());
assertFalse(f2.isDone());
lock.signal(); // let the first one go
f1.get(); // first one should complete
// now 2nd one gets going and hits the choke point
f2.get();
assertEquals(2, counter.get());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册