Search in sources :

Example 1 with TestUtils.waitForCondition

use of org.apache.kafka.test.TestUtils.waitForCondition in project kafka by apache.

the class AdjustStreamThreadCountTest method shouldAddAndRemoveStreamThreadsWhileKeepingNamesCorrect.

@Test
public void shouldAddAndRemoveStreamThreadsWhileKeepingNamesCorrect() throws Exception {
    try (final KafkaStreams kafkaStreams = new KafkaStreams(builder.build(), properties)) {
        addStreamStateChangeListener(kafkaStreams);
        startStreamsAndWaitForRunning(kafkaStreams);
        int oldThreadCount = kafkaStreams.metadataForLocalThreads().size();
        stateTransitionHistory.clear();
        assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2" }));
        final Optional<String> name = kafkaStreams.addStreamThread();
        assertThat("New thread has index 3", "3".equals(name.get().split("-StreamThread-")[1]));
        TestUtils.waitForCondition(() -> kafkaStreams.metadataForLocalThreads().stream().sequential().map(ThreadMetadata::threadName).anyMatch(t -> t.equals(name.get())), "Stream thread has not been added");
        assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount + 1));
        assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2", "3" }));
        waitForTransitionFromRebalancingToRunning();
        oldThreadCount = kafkaStreams.metadataForLocalThreads().size();
        stateTransitionHistory.clear();
        final Optional<String> removedThread = kafkaStreams.removeStreamThread();
        assertThat(removedThread, not(Optional.empty()));
        assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount - 1));
        waitForTransitionFromRebalancingToRunning();
        stateTransitionHistory.clear();
        final Optional<String> name2 = kafkaStreams.addStreamThread();
        assertThat(name2, not(Optional.empty()));
        TestUtils.waitForCondition(() -> kafkaStreams.metadataForLocalThreads().stream().sequential().map(ThreadMetadata::threadName).anyMatch(t -> t.equals(name2.orElse(""))), "Wait for the thread to be added");
        assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount));
        assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2", "3" }));
        assertThat("the new thread should have received the old threads name", name2.equals(removedThread));
        waitForTransitionFromRebalancingToRunning();
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) IntegrationTestUtils.safeUniqueTestName(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.safeUniqueTestName) After(org.junit.After) Duration(java.time.Duration) Serdes(org.apache.kafka.common.serialization.Serdes) Assert.fail(org.junit.Assert.fail) AfterClass(org.junit.AfterClass) ThreadMetadata(org.apache.kafka.streams.ThreadMetadata) TestUtils(org.apache.kafka.test.TestUtils) Utils.mkObjectProperties(org.apache.kafka.common.utils.Utils.mkObjectProperties) KeyValue(org.apache.kafka.streams.KeyValue) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Category(org.junit.experimental.categories.Category) Executors(java.util.concurrent.Executors) IntegrationTestUtils(org.apache.kafka.streams.integration.utils.IntegrationTestUtils) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) CountDownLatch(java.util.concurrent.CountDownLatch) IntegrationTestUtils.purgeLocalStreamsState(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.purgeLocalStreamsState) List(java.util.List) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) Optional(java.util.Optional) StreamsConfig(org.apache.kafka.streams.StreamsConfig) BeforeClass(org.junit.BeforeClass) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Assert.assertThrows(org.junit.Assert.assertThrows) CoreMatchers.not(org.hamcrest.CoreMatchers.not) IntegrationTest(org.apache.kafka.test.IntegrationTest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KStream(org.apache.kafka.streams.kstream.KStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) EmbeddedKafkaCluster(org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster) TestName(org.junit.rules.TestName) PunctuationType(org.apache.kafka.streams.processor.PunctuationType) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Properties(java.util.Properties) Transformer(org.apache.kafka.streams.kstream.Transformer) TestUtils.waitForCondition(org.apache.kafka.test.TestUtils.waitForCondition) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) StreamThreadExceptionResponse(org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Assert.assertEquals(org.junit.Assert.assertEquals) KafkaStreams(org.apache.kafka.streams.KafkaStreams) ThreadMetadata(org.apache.kafka.streams.ThreadMetadata) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Example 2 with TestUtils.waitForCondition

use of org.apache.kafka.test.TestUtils.waitForCondition in project kafka by apache.

the class AdjustStreamThreadCountTest method shouldAddStreamThread.

@Test
public void shouldAddStreamThread() throws Exception {
    try (final KafkaStreams kafkaStreams = new KafkaStreams(builder.build(), properties)) {
        addStreamStateChangeListener(kafkaStreams);
        startStreamsAndWaitForRunning(kafkaStreams);
        final int oldThreadCount = kafkaStreams.metadataForLocalThreads().size();
        assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2" }));
        stateTransitionHistory.clear();
        final Optional<String> name = kafkaStreams.addStreamThread();
        assertThat(name, not(Optional.empty()));
        TestUtils.waitForCondition(() -> kafkaStreams.metadataForLocalThreads().stream().sequential().map(ThreadMetadata::threadName).anyMatch(t -> t.equals(name.orElse(""))), "Wait for the thread to be added");
        assertThat(kafkaStreams.metadataForLocalThreads().size(), equalTo(oldThreadCount + 1));
        assertThat(kafkaStreams.metadataForLocalThreads().stream().map(t -> t.threadName().split("-StreamThread-")[1]).sorted().toArray(), equalTo(new String[] { "1", "2", "3" }));
        waitForTransitionFromRebalancingToRunning();
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Utils.mkMap(org.apache.kafka.common.utils.Utils.mkMap) IntegrationTestUtils.safeUniqueTestName(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.safeUniqueTestName) After(org.junit.After) Duration(java.time.Duration) Serdes(org.apache.kafka.common.serialization.Serdes) Assert.fail(org.junit.Assert.fail) AfterClass(org.junit.AfterClass) ThreadMetadata(org.apache.kafka.streams.ThreadMetadata) TestUtils(org.apache.kafka.test.TestUtils) Utils.mkObjectProperties(org.apache.kafka.common.utils.Utils.mkObjectProperties) KeyValue(org.apache.kafka.streams.KeyValue) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) Category(org.junit.experimental.categories.Category) Executors(java.util.concurrent.Executors) IntegrationTestUtils(org.apache.kafka.streams.integration.utils.IntegrationTestUtils) ProcessorContext(org.apache.kafka.streams.processor.ProcessorContext) CountDownLatch(java.util.concurrent.CountDownLatch) IntegrationTestUtils.purgeLocalStreamsState(org.apache.kafka.streams.integration.utils.IntegrationTestUtils.purgeLocalStreamsState) List(java.util.List) Utils.mkEntry(org.apache.kafka.common.utils.Utils.mkEntry) Optional(java.util.Optional) StreamsConfig(org.apache.kafka.streams.StreamsConfig) BeforeClass(org.junit.BeforeClass) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Assert.assertThrows(org.junit.Assert.assertThrows) CoreMatchers.not(org.hamcrest.CoreMatchers.not) IntegrationTest(org.apache.kafka.test.IntegrationTest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KStream(org.apache.kafka.streams.kstream.KStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) EmbeddedKafkaCluster(org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster) TestName(org.junit.rules.TestName) PunctuationType(org.apache.kafka.streams.processor.PunctuationType) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) TimeoutException(org.apache.kafka.common.errors.TimeoutException) Properties(java.util.Properties) Transformer(org.apache.kafka.streams.kstream.Transformer) TestUtils.waitForCondition(org.apache.kafka.test.TestUtils.waitForCondition) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) Rule(org.junit.Rule) Assert.assertNull(org.junit.Assert.assertNull) StreamThreadExceptionResponse(org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler.StreamThreadExceptionResponse) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Assert.assertEquals(org.junit.Assert.assertEquals) KafkaStreams(org.apache.kafka.streams.KafkaStreams) ThreadMetadata(org.apache.kafka.streams.ThreadMetadata) IntegrationTest(org.apache.kafka.test.IntegrationTest) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Optional (java.util.Optional)2 Properties (java.util.Properties)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 Executors (java.util.concurrent.Executors)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ConsumerConfig (org.apache.kafka.clients.consumer.ConsumerConfig)2 TimeoutException (org.apache.kafka.common.errors.TimeoutException)2 Serdes (org.apache.kafka.common.serialization.Serdes)2 Utils.mkEntry (org.apache.kafka.common.utils.Utils.mkEntry)2 Utils.mkMap (org.apache.kafka.common.utils.Utils.mkMap)2 Utils.mkObjectProperties (org.apache.kafka.common.utils.Utils.mkObjectProperties)2 KafkaStreams (org.apache.kafka.streams.KafkaStreams)2 KeyValue (org.apache.kafka.streams.KeyValue)2