Search in sources :

Example 36 with CheckedThread

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

the class DefaultLeaderElectionServiceTest method testServiceShutDownWithSynchronizedDriver.

/**
 * Tests that we can shut down the DefaultLeaderElectionService if the used LeaderElectionDriver
 * holds an internal lock. See FLINK-20008 for more details.
 */
@Test
public void testServiceShutDownWithSynchronizedDriver() throws Exception {
    final TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory testingLeaderElectionDriverFactory = new TestingLeaderElectionDriver.TestingLeaderElectionDriverFactory();
    final DefaultLeaderElectionService leaderElectionService = new DefaultLeaderElectionService(testingLeaderElectionDriverFactory);
    final TestingContender testingContender = new TestingContender(TEST_URL, leaderElectionService);
    leaderElectionService.start(testingContender);
    final TestingLeaderElectionDriver currentLeaderDriver = Preconditions.checkNotNull(testingLeaderElectionDriverFactory.getCurrentLeaderDriver());
    final CheckedThread isLeaderThread = new CheckedThread() {

        @Override
        public void go() {
            currentLeaderDriver.isLeader();
        }
    };
    isLeaderThread.start();
    leaderElectionService.stop();
    isLeaderThread.sync();
}
Also used : CheckedThread(org.apache.flink.core.testutils.CheckedThread) Test(org.junit.Test)

Example 37 with CheckedThread

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

the class DefaultJobLeaderServiceTest method handlesConcurrentJobAdditionsAndLeaderChanges.

/**
 * Tests that we can concurrently modify the JobLeaderService and complete the leader retrieval
 * operation. See FLINK-16373.
 */
@Test
public void handlesConcurrentJobAdditionsAndLeaderChanges() throws Exception {
    final JobLeaderService jobLeaderService = new DefaultJobLeaderService(new LocalUnresolvedTaskManagerLocation(), RetryingRegistrationConfiguration.defaultConfiguration());
    final TestingJobLeaderListener jobLeaderListener = new TestingJobLeaderListener();
    final int numberOperations = 20;
    final BlockingQueue<SettableLeaderRetrievalService> instantiatedLeaderRetrievalServices = new ArrayBlockingQueue<>(numberOperations);
    final HighAvailabilityServices haServices = new TestingHighAvailabilityServicesBuilder().setJobMasterLeaderRetrieverFunction(leaderForJobId -> {
        final SettableLeaderRetrievalService leaderRetrievalService = new SettableLeaderRetrievalService();
        instantiatedLeaderRetrievalServices.offer(leaderRetrievalService);
        return leaderRetrievalService;
    }).build();
    jobLeaderService.start("foobar", rpcServiceResource.getTestingRpcService(), haServices, jobLeaderListener);
    final CheckedThread addJobAction = new CheckedThread() {

        @Override
        public void go() throws Exception {
            for (int i = 0; i < numberOperations; i++) {
                final JobID jobId = JobID.generate();
                jobLeaderService.addJob(jobId, "foobar");
                Thread.yield();
                jobLeaderService.removeJob(jobId);
            }
        }
    };
    addJobAction.start();
    for (int i = 0; i < numberOperations; i++) {
        final SettableLeaderRetrievalService leaderRetrievalService = instantiatedLeaderRetrievalServices.take();
        leaderRetrievalService.notifyListener("foobar", UUID.randomUUID());
    }
    addJobAction.sync();
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) FlinkException(org.apache.flink.util.FlinkException) TimeoutException(java.util.concurrent.TimeoutException) JMTMRegistrationSuccess(org.apache.flink.runtime.jobmaster.JMTMRegistrationSuccess) CompletableFuture(java.util.concurrent.CompletableFuture) TestingJobMasterGateway(org.apache.flink.runtime.jobmaster.utils.TestingJobMasterGateway) JobMasterGateway(org.apache.flink.runtime.jobmaster.JobMasterGateway) Assert.assertThat(org.junit.Assert.assertThat) CheckedThread(org.apache.flink.core.testutils.CheckedThread) SettableLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.SettableLeaderRetrievalService) TestLogger(org.apache.flink.util.TestLogger) TestingJobMasterGatewayBuilder(org.apache.flink.runtime.jobmaster.utils.TestingJobMasterGatewayBuilder) Assert.fail(org.junit.Assert.fail) TestingHighAvailabilityServicesBuilder(org.apache.flink.runtime.highavailability.TestingHighAvailabilityServicesBuilder) RetryingRegistrationConfiguration(org.apache.flink.runtime.registration.RetryingRegistrationConfiguration) JMTMRegistrationRejection(org.apache.flink.runtime.jobmaster.JMTMRegistrationRejection) HighAvailabilityServices(org.apache.flink.runtime.highavailability.HighAvailabilityServices) LocalUnresolvedTaskManagerLocation(org.apache.flink.runtime.taskmanager.LocalUnresolvedTaskManagerLocation) JobMasterId(org.apache.flink.runtime.jobmaster.JobMasterId) Test(org.junit.Test) BlockingQueue(java.util.concurrent.BlockingQueue) UUID(java.util.UUID) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CountDownLatch(java.util.concurrent.CountDownLatch) JobID(org.apache.flink.api.common.JobID) Rule(org.junit.Rule) TestingRpcServiceResource(org.apache.flink.runtime.rpc.TestingRpcServiceResource) TestingHighAvailabilityServices(org.apache.flink.runtime.highavailability.TestingHighAvailabilityServices) CheckedThread(org.apache.flink.core.testutils.CheckedThread) TestingHighAvailabilityServicesBuilder(org.apache.flink.runtime.highavailability.TestingHighAvailabilityServicesBuilder) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) SettableLeaderRetrievalService(org.apache.flink.runtime.leaderretrieval.SettableLeaderRetrievalService) HighAvailabilityServices(org.apache.flink.runtime.highavailability.HighAvailabilityServices) TestingHighAvailabilityServices(org.apache.flink.runtime.highavailability.TestingHighAvailabilityServices) LocalUnresolvedTaskManagerLocation(org.apache.flink.runtime.taskmanager.LocalUnresolvedTaskManagerLocation) JobID(org.apache.flink.api.common.JobID) Test(org.junit.Test)

Example 38 with CheckedThread

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

the class BlobServerPutTest method testServerContentAddressableGetStorageLocationConcurrent.

private void testServerContentAddressableGetStorageLocationConcurrent(@Nullable final JobID jobId) throws Exception {
    final Configuration config = new Configuration();
    try (BlobServer server = new BlobServer(config, temporaryFolder.newFolder(), new VoidBlobStore())) {
        server.start();
        BlobKey key1 = new TransientBlobKey();
        BlobKey key2 = new PermanentBlobKey();
        CheckedThread[] threads = new CheckedThread[] { new ContentAddressableGetStorageLocation(server, jobId, key1), new ContentAddressableGetStorageLocation(server, jobId, key1), new ContentAddressableGetStorageLocation(server, jobId, key1), new ContentAddressableGetStorageLocation(server, jobId, key2), new ContentAddressableGetStorageLocation(server, jobId, key2), new ContentAddressableGetStorageLocation(server, jobId, key2) };
        checkedThreadSimpleTest(threads);
    }
}
Also used : Configuration(org.apache.flink.configuration.Configuration) CheckedThread(org.apache.flink.core.testutils.CheckedThread)

Example 39 with CheckedThread

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

the class NetworkBufferPoolTest method testRequestMemorySegmentsInterruptable2.

/**
 * Tests {@link NetworkBufferPool#requestUnpooledMemorySegments(int)}, verifying it may be
 * aborted and remains in a defined state even if the waiting is interrupted.
 */
@Test
public void testRequestMemorySegmentsInterruptable2() throws Exception {
    final int numBuffers = 10;
    NetworkBufferPool globalPool = new NetworkBufferPool(numBuffers, 128);
    MemorySegment segment = globalPool.requestPooledMemorySegment();
    assertNotNull(segment);
    final OneShotLatch isRunning = new OneShotLatch();
    CheckedThread asyncRequest = new CheckedThread() {

        @Override
        public void go() throws IOException {
            isRunning.trigger();
            globalPool.requestUnpooledMemorySegments(10);
        }
    };
    asyncRequest.start();
    // We want the destroy call inside the blocking part of the
    // globalPool.requestMemorySegments()
    // call above. We cannot guarantee this though but make it highly probable:
    isRunning.await();
    Thread.sleep(10);
    asyncRequest.interrupt();
    globalPool.recyclePooledMemorySegment(segment);
    try {
        asyncRequest.sync();
    } catch (IOException e) {
        assertThat(e, hasProperty("cause", instanceOf(InterruptedException.class)));
        // test indirectly for NetworkBufferPool#numTotalRequiredBuffers being correct:
        // -> creating a new buffer pool should not fail
        globalPool.createBufferPool(10, 10);
    } finally {
        globalPool.destroy();
    }
}
Also used : OneShotLatch(org.apache.flink.core.testutils.OneShotLatch) IOException(java.io.IOException) CheckedThread(org.apache.flink.core.testutils.CheckedThread) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Example 40 with CheckedThread

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

the class NetworkBufferPoolTest method testRequestMemorySegmentsTimeout.

/**
 * Tests {@link NetworkBufferPool#requestUnpooledMemorySegments(int)} and verifies it will end
 * exceptionally when failing to acquire all the segments in the specific timeout.
 */
@Test
public void testRequestMemorySegmentsTimeout() throws Exception {
    final int numBuffers = 10;
    final int numberOfSegmentsToRequest = 2;
    final Duration requestSegmentsTimeout = Duration.ofMillis(50L);
    NetworkBufferPool globalPool = new NetworkBufferPool(numBuffers, 128, requestSegmentsTimeout);
    BufferPool localBufferPool = globalPool.createBufferPool(1, numBuffers);
    for (int i = 0; i < numBuffers; ++i) {
        localBufferPool.requestBuffer();
    }
    assertEquals(0, globalPool.getNumberOfAvailableMemorySegments());
    CheckedThread asyncRequest = new CheckedThread() {

        @Override
        public void go() throws Exception {
            globalPool.requestUnpooledMemorySegments(numberOfSegmentsToRequest);
        }
    };
    asyncRequest.start();
    try {
        Exception ex = assertThrows(IOException.class, asyncRequest::sync);
        assertTrue(ex.getMessage().contains("Timeout"));
    } finally {
        globalPool.destroy();
    }
}
Also used : Duration(java.time.Duration) CheckedThread(org.apache.flink.core.testutils.CheckedThread) CancelTaskException(org.apache.flink.runtime.execution.CancelTaskException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

CheckedThread (org.apache.flink.core.testutils.CheckedThread)45 Test (org.junit.Test)41 SimpleStringSchema (org.apache.flink.api.common.serialization.SimpleStringSchema)12 HashMap (java.util.HashMap)8 LinkedList (java.util.LinkedList)8 OneInputStreamOperatorTestHarness (org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness)8 TestableKinesisDataFetcher (org.apache.flink.streaming.connectors.kinesis.testutils.TestableKinesisDataFetcher)7 KinesisStreamShardState (org.apache.flink.streaming.connectors.kinesis.model.KinesisStreamShardState)6 IOException (java.io.IOException)5 Map (java.util.Map)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 OneShotLatch (org.apache.flink.core.testutils.OneShotLatch)5 SequenceNumber (org.apache.flink.streaming.connectors.kinesis.model.SequenceNumber)5 Shard (com.amazonaws.services.kinesis.model.Shard)4 File (java.io.File)4 Random (java.util.Random)4 UserRecordResult (com.amazonaws.services.kinesis.producer.UserRecordResult)3 ArrayList (java.util.ArrayList)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Configuration (org.apache.flink.configuration.Configuration)3