Search in sources :

Example 1 with TestExecutorService

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Statement(org.junit.runners.model.Statement) TestExecutorService(org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService) TestExecutorService(org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 2 with TestExecutorService

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());
}
Also used : Statement(org.junit.runners.model.Statement) TestExecutorService(org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService) TestExecutorService(org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 3 with TestExecutorService

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Statement(org.junit.runners.model.Statement) TestExecutorService(org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService) TestExecutorService(org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 4 with TestExecutorService

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Statement(org.junit.runners.model.Statement) TestExecutorService(org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService) TestExecutorService(org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Aggregations

ExecutorService (java.util.concurrent.ExecutorService)4 TestExecutorService (org.apache.beam.fn.harness.test.TestExecutors.TestExecutorService)4 Test (org.junit.Test)4 Statement (org.junit.runners.model.Statement)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3