use of org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService in project beam by apache.
the class TestExecutorsTest method testSuccessfulTermination.
@Test
public void testSuccessfulTermination() throws Throwable {
ExecutorService service = Executors.newSingleThreadExecutor();
final TestExecutorService testService = TestExecutors.from(() -> service);
final AtomicBoolean taskRan = new AtomicBoolean();
testService.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
testService.submit(() -> taskRan.set(true));
}
}, null).evaluate();
assertTrue(service.isTerminated());
assertTrue(taskRan.get());
}
use of org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService in project beam by apache.
the class TestExecutorsTest method testStatementFailurePropagatedCleanly.
@Test
public void testStatementFailurePropagatedCleanly() throws Throwable {
ExecutorService service = Executors.newSingleThreadExecutor();
final TestExecutorService testService = TestExecutors.from(() -> service);
final RuntimeException exceptionToThrow = new RuntimeException();
try {
testService.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
throw exceptionToThrow;
}
}, null).evaluate();
fail();
} catch (RuntimeException thrownException) {
assertSame(exceptionToThrow, thrownException);
}
assertTrue(service.isShutdown());
}
use of org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService in project beam by apache.
the class TestExecutorsTest method testStatementFailurePropagatedWhenExecutorServiceFailingToTerminate.
@Test
public void testStatementFailurePropagatedWhenExecutorServiceFailingToTerminate() throws Throwable {
ExecutorService service = Executors.newSingleThreadExecutor();
final TestExecutorService testService = TestExecutors.from(() -> service);
final AtomicBoolean taskStarted = new AtomicBoolean();
final AtomicBoolean taskWasInterrupted = new AtomicBoolean();
final RuntimeException exceptionToThrow = new RuntimeException();
try {
testService.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
testService.submit(this::taskToRun);
throw exceptionToThrow;
}
private void taskToRun() {
taskStarted.set(true);
try {
while (true) {
Thread.sleep(10000);
}
} catch (InterruptedException e) {
taskWasInterrupted.set(true);
return;
}
}
}, null).evaluate();
fail();
} catch (RuntimeException thrownException) {
assertSame(exceptionToThrow, thrownException);
assertEquals(1, exceptionToThrow.getSuppressed().length);
assertEquals(IllegalStateException.class, exceptionToThrow.getSuppressed()[0].getClass());
assertEquals("Test executor failed to shutdown cleanly.", exceptionToThrow.getSuppressed()[0].getMessage());
}
assertTrue(service.isShutdown());
}
use of org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService in project beam by apache.
the class TestExecutorsTest method testTaskBlocksForeverCausesFailure.
@Test
public void testTaskBlocksForeverCausesFailure() throws Throwable {
ExecutorService service = Executors.newSingleThreadExecutor();
final TestExecutorService testService = TestExecutors.from(() -> service);
final AtomicBoolean taskStarted = new AtomicBoolean();
final AtomicBoolean taskWasInterrupted = new AtomicBoolean();
try {
testService.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
testService.submit(this::taskToRun);
}
private void taskToRun() {
taskStarted.set(true);
try {
while (true) {
Thread.sleep(10000);
}
} catch (InterruptedException e) {
taskWasInterrupted.set(true);
return;
}
}
}, null).evaluate();
fail();
} catch (IllegalStateException e) {
assertEquals(IllegalStateException.class, e.getClass());
assertEquals("Test executor failed to shutdown cleanly.", e.getMessage());
}
assertTrue(service.isShutdown());
}
Aggregations