Search in sources :

Example 71 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class FutureUtilsTest method testComposeAfterwardsSecondExceptional.

@Test
public void testComposeAfterwardsSecondExceptional() throws InterruptedException {
    final CompletableFuture<Void> inputFuture = new CompletableFuture<>();
    final OneShotLatch composeLatch = new OneShotLatch();
    final FlinkException testException = new FlinkException("Test exception");
    final CompletableFuture<Void> composeFuture = FutureUtils.composeAfterwards(inputFuture, () -> {
        composeLatch.trigger();
        return FutureUtils.completedExceptionally(testException);
    });
    assertThat(composeLatch.isTriggered(), is(false));
    assertThat(composeFuture.isDone(), is(false));
    inputFuture.complete(null);
    assertThat(composeLatch.isTriggered(), is(true));
    assertThat(composeFuture.isDone(), is(true));
    // check that this future is not exceptionally completed
    try {
        composeFuture.get();
        fail("Expected an exceptional completion");
    } catch (ExecutionException ee) {
        assertThat(ExceptionUtils.stripExecutionException(ee), is(testException));
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ExecutionException(java.util.concurrent.ExecutionException) FlinkException(org.apache.flink.util.FlinkException) Test(org.junit.Test)

Example 72 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class FutureUtilsTest method testComposeAfterwardsFirstExceptional.

@Test
public void testComposeAfterwardsFirstExceptional() throws InterruptedException {
    final CompletableFuture<Void> inputFuture = new CompletableFuture<>();
    final OneShotLatch composeLatch = new OneShotLatch();
    final FlinkException testException = new FlinkException("Test exception");
    final CompletableFuture<Void> composeFuture = FutureUtils.composeAfterwards(inputFuture, () -> {
        composeLatch.trigger();
        return CompletableFuture.completedFuture(null);
    });
    assertThat(composeLatch.isTriggered(), is(false));
    assertThat(composeFuture.isDone(), is(false));
    inputFuture.completeExceptionally(testException);
    assertThat(composeLatch.isTriggered(), is(true));
    assertThat(composeFuture.isDone(), is(true));
    // check that this future is not exceptionally completed
    try {
        composeFuture.get();
        fail("Expected an exceptional completion");
    } catch (ExecutionException ee) {
        assertThat(ExceptionUtils.stripExecutionException(ee), is(testException));
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ExecutionException(java.util.concurrent.ExecutionException) FlinkException(org.apache.flink.util.FlinkException) Test(org.junit.Test)

Example 73 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class ArrowSourceFunctionTestBase method testParallelProcessing.

@Test
public void testParallelProcessing() throws Exception {
    Tuple2<List<RowData>, Integer> testData = getTestData();
    final ArrowSourceFunction arrowSourceFunction = createTestArrowSourceFunction(testData.f0, testData.f1);
    final AbstractStreamOperatorTestHarness<RowData> testHarness = new AbstractStreamOperatorTestHarness(new StreamSource<>(arrowSourceFunction), 2, 2, 0);
    testHarness.open();
    final Throwable[] error = new Throwable[2];
    final OneShotLatch latch = new OneShotLatch();
    final AtomicInteger numOfEmittedElements = new AtomicInteger(0);
    final List<RowData> results = Collections.synchronizedList(new ArrayList<>());
    // run the source asynchronously
    Thread runner = new Thread(() -> {
        try {
            arrowSourceFunction.run(new DummySourceContext<RowData>() {

                @Override
                public void collect(RowData element) {
                    results.add(typeSerializer.copy(element));
                    if (numOfEmittedElements.incrementAndGet() == testData.f0.size()) {
                        latch.trigger();
                    }
                }
            });
        } catch (Throwable t) {
            error[0] = t;
        }
    });
    runner.start();
    final ArrowSourceFunction arrowSourceFunction2 = createTestArrowSourceFunction(testData.f0, testData.f1);
    final AbstractStreamOperatorTestHarness<RowData> testHarness2 = new AbstractStreamOperatorTestHarness(new StreamSource<>(arrowSourceFunction2), 2, 2, 1);
    testHarness2.open();
    // run the source asynchronously
    Thread runner2 = new Thread(() -> {
        try {
            arrowSourceFunction2.run(new DummySourceContext<RowData>() {

                @Override
                public void collect(RowData element) {
                    results.add(typeSerializer.copy(element));
                    if (numOfEmittedElements.incrementAndGet() == testData.f0.size()) {
                        latch.trigger();
                    }
                }
            });
        } catch (Throwable t) {
            error[1] = t;
        }
    });
    runner2.start();
    if (!latch.isTriggered()) {
        latch.await();
    }
    runner.join();
    runner2.join();
    testHarness.close();
    testHarness2.close();
    Assert.assertNull(error[0]);
    Assert.assertNull(error[1]);
    Assert.assertEquals(testData.f0.size(), numOfEmittedElements.get());
    checkElementsEquals(results, testData.f0);
}
Also used : AbstractStreamOperatorTestHarness(org.apache.flink.streaming.util.AbstractStreamOperatorTestHarness) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RowData(org.apache.flink.table.data.RowData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 74 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class CliFrontendStopWithSavepointTest method testStopOnlyWithMaxWM.

@Test
public void testStopOnlyWithMaxWM() throws Exception {
    JobID jid = new JobID();
    String[] parameters = { "-d", jid.toString() };
    OneShotLatch stopWithSavepointLatch = new OneShotLatch();
    TestingClusterClient<String> clusterClient = new TestingClusterClient<>();
    clusterClient.setStopWithSavepointFunction((jobID, advanceToEndOfEventTime, savepointDirectory, formatType) -> {
        assertThat(jobID, is(jid));
        assertThat(advanceToEndOfEventTime, is(true));
        assertNull(savepointDirectory);
        stopWithSavepointLatch.trigger();
        return CompletableFuture.completedFuture(savepointDirectory);
    });
    MockedCliFrontend testFrontend = new MockedCliFrontend(clusterClient);
    testFrontend.stop(parameters);
    stopWithSavepointLatch.await();
}
Also used : MockedCliFrontend(org.apache.flink.client.cli.util.MockedCliFrontend) TestingClusterClient(org.apache.flink.client.program.TestingClusterClient) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 75 with OneShotLatch

use of org.apache.flink.core.testutils.OneShotLatch in project flink by apache.

the class CliFrontendStopWithSavepointTest method testStopWithDefaultSavepointDir.

@Test
public void testStopWithDefaultSavepointDir() throws Exception {
    JobID jid = new JobID();
    String[] parameters = { jid.toString() };
    OneShotLatch stopWithSavepointLatch = new OneShotLatch();
    TestingClusterClient<String> clusterClient = new TestingClusterClient<>();
    clusterClient.setStopWithSavepointFunction((jobID, advanceToEndOfEventTime, savepointDirectory, formatType) -> {
        assertThat(jobID, is(jid));
        assertThat(advanceToEndOfEventTime, is(false));
        assertNull(savepointDirectory);
        stopWithSavepointLatch.trigger();
        return CompletableFuture.completedFuture(savepointDirectory);
    });
    MockedCliFrontend testFrontend = new MockedCliFrontend(clusterClient);
    testFrontend.stop(parameters);
    stopWithSavepointLatch.await();
}
Also used : MockedCliFrontend(org.apache.flink.client.cli.util.MockedCliFrontend) TestingClusterClient(org.apache.flink.client.program.TestingClusterClient) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Aggregations

OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)138 Test (org.junit.Test)118 JobID (org.apache.flink.api.common.JobID)41 CompletableFuture (java.util.concurrent.CompletableFuture)38 ExecutionException (java.util.concurrent.ExecutionException)27 Configuration (org.apache.flink.configuration.Configuration)26 IOException (java.io.IOException)24 Before (org.junit.Before)24 FlinkException (org.apache.flink.util.FlinkException)23 TestLogger (org.apache.flink.util.TestLogger)21 File (java.io.File)20 UUID (java.util.UUID)18 TimeoutException (java.util.concurrent.TimeoutException)18 TestingResourceManagerGateway (org.apache.flink.runtime.resourcemanager.utils.TestingResourceManagerGateway)18 Time (org.apache.flink.api.common.time.Time)17 TestingJobMasterGateway (org.apache.flink.runtime.jobmaster.utils.TestingJobMasterGateway)17 Rule (org.junit.Rule)17 Collections (java.util.Collections)16 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)16 RpcUtils (org.apache.flink.runtime.rpc.RpcUtils)16