Search in sources :

Example 1 with TestCondition

use of org.apache.kafka.test.TestCondition in project kafka by apache.

the class GlobalKTableIntegrationTest method shouldKStreamGlobalKTableLeftJoin.

@Test
public void shouldKStreamGlobalKTableLeftJoin() throws Exception {
    final KStream<String, String> streamTableJoin = stream.leftJoin(globalTable, keyMapper, joiner);
    streamTableJoin.foreach(foreachAction);
    produceInitialGlobalTableValues();
    startStreams();
    produceTopicValues(inputStream);
    final Map<String, String> expected = new HashMap<>();
    expected.put("a", "1+A");
    expected.put("b", "2+B");
    expected.put("c", "3+C");
    expected.put("d", "4+D");
    expected.put("e", "5+null");
    TestUtils.waitForCondition(new TestCondition() {

        @Override
        public boolean conditionMet() {
            return results.equals(expected);
        }
    }, 30000L, "waiting for initial values");
    produceGlobalTableValues();
    final ReadOnlyKeyValueStore<Long, String> replicatedStore = kafkaStreams.store(globalStore, QueryableStoreTypes.<Long, String>keyValueStore());
    TestUtils.waitForCondition(new TestCondition() {

        @Override
        public boolean conditionMet() {
            return "J".equals(replicatedStore.get(5L));
        }
    }, 30000, "waiting for data in replicated store");
    produceTopicValues(inputStream);
    expected.put("a", "1+F");
    expected.put("b", "2+G");
    expected.put("c", "3+H");
    expected.put("d", "4+I");
    expected.put("e", "5+J");
    TestUtils.waitForCondition(new TestCondition() {

        @Override
        public boolean conditionMet() {
            return results.equals(expected);
        }
    }, 30000L, "waiting for final values");
}
Also used : HashMap(java.util.HashMap) TestCondition(org.apache.kafka.test.TestCondition) Test(org.junit.Test)

Example 2 with TestCondition

use of org.apache.kafka.test.TestCondition in project kafka by apache.

the class IntegrationTestUtils method waitUntilMinValuesRecordsReceived.

/**
     * Wait until enough data (value records) has been consumed.
     *
     * @param consumerConfig     Kafka Consumer configuration
     * @param topic              Topic to consume from
     * @param expectedNumRecords Minimum number of expected records
     * @param waitTime           Upper bound in waiting time in milliseconds
     * @return All the records consumed, or null if no records are consumed
     * @throws InterruptedException
     * @throws AssertionError       if the given wait time elapses
     */
public static <V> List<V> waitUntilMinValuesRecordsReceived(final Properties consumerConfig, final String topic, final int expectedNumRecords, final long waitTime) throws InterruptedException {
    final List<V> accumData = new ArrayList<>();
    final TestCondition valuesRead = new TestCondition() {

        @Override
        public boolean conditionMet() {
            final List<V> readData = readValues(topic, consumerConfig, waitTime, expectedNumRecords);
            accumData.addAll(readData);
            return accumData.size() >= expectedNumRecords;
        }
    };
    final String conditionDetails = "Expecting " + expectedNumRecords + " records from topic " + topic + " while only received " + accumData.size() + ": " + accumData;
    TestUtils.waitForCondition(valuesRead, waitTime, conditionDetails);
    return accumData;
}
Also used : ArrayList(java.util.ArrayList) TestCondition(org.apache.kafka.test.TestCondition)

Example 3 with TestCondition

use of org.apache.kafka.test.TestCondition in project kafka by apache.

the class RegexSourceIntegrationTest method testRegexMatchesTopicsAWhenDeleted.

@Test
public void testRegexMatchesTopicsAWhenDeleted() throws Exception {
    final Serde<String> stringSerde = Serdes.String();
    final List<String> expectedFirstAssignment = Arrays.asList("TEST-TOPIC-A", "TEST-TOPIC-B");
    final List<String> expectedSecondAssignment = Arrays.asList("TEST-TOPIC-B");
    final StreamsConfig streamsConfig = new StreamsConfig(streamsConfiguration);
    CLUSTER.createTopic("TEST-TOPIC-A");
    CLUSTER.createTopic("TEST-TOPIC-B");
    final KStreamBuilder builder = new KStreamBuilder();
    final KStream<String, String> pattern1Stream = builder.stream(Pattern.compile("TEST-TOPIC-[A-Z]"));
    pattern1Stream.to(stringSerde, stringSerde, DEFAULT_OUTPUT_TOPIC);
    final KafkaStreams streams = new KafkaStreams(builder, streamsConfiguration);
    final Field streamThreadsField = streams.getClass().getDeclaredField("threads");
    streamThreadsField.setAccessible(true);
    final StreamThread[] streamThreads = (StreamThread[]) streamThreadsField.get(streams);
    final StreamThread originalThread = streamThreads[0];
    final TestStreamThread testStreamThread = new TestStreamThread(builder, streamsConfig, new DefaultKafkaClientSupplier(), originalThread.applicationId, originalThread.clientId, originalThread.processId, new Metrics(), Time.SYSTEM);
    streamThreads[0] = testStreamThread;
    final TestCondition bothTopicsAdded = new TestCondition() {

        @Override
        public boolean conditionMet() {
            return testStreamThread.assignedTopicPartitions.equals(expectedFirstAssignment);
        }
    };
    streams.start();
    TestUtils.waitForCondition(bothTopicsAdded, STREAM_TASKS_NOT_UPDATED);
    CLUSTER.deleteTopic("TEST-TOPIC-A");
    final TestCondition oneTopicRemoved = new TestCondition() {

        @Override
        public boolean conditionMet() {
            return testStreamThread.assignedTopicPartitions.equals(expectedSecondAssignment);
        }
    };
    TestUtils.waitForCondition(oneTopicRemoved, STREAM_TASKS_NOT_UPDATED);
    streams.close();
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KafkaStreams(org.apache.kafka.streams.KafkaStreams) DefaultKafkaClientSupplier(org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread) Field(java.lang.reflect.Field) Metrics(org.apache.kafka.common.metrics.Metrics) TestCondition(org.apache.kafka.test.TestCondition) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 4 with TestCondition

use of org.apache.kafka.test.TestCondition in project kafka by apache.

the class RegexSourceIntegrationTest method testRegexMatchesTopicsAWhenCreated.

@Test
public void testRegexMatchesTopicsAWhenCreated() throws Exception {
    final Serde<String> stringSerde = Serdes.String();
    final List<String> expectedFirstAssignment = Arrays.asList("TEST-TOPIC-1");
    final List<String> expectedSecondAssignment = Arrays.asList("TEST-TOPIC-1", "TEST-TOPIC-2");
    final StreamsConfig streamsConfig = new StreamsConfig(streamsConfiguration);
    CLUSTER.createTopic("TEST-TOPIC-1");
    final KStreamBuilder builder = new KStreamBuilder();
    final KStream<String, String> pattern1Stream = builder.stream(Pattern.compile("TEST-TOPIC-\\d"));
    pattern1Stream.to(stringSerde, stringSerde, DEFAULT_OUTPUT_TOPIC);
    final KafkaStreams streams = new KafkaStreams(builder, streamsConfiguration);
    final Field streamThreadsField = streams.getClass().getDeclaredField("threads");
    streamThreadsField.setAccessible(true);
    final StreamThread[] streamThreads = (StreamThread[]) streamThreadsField.get(streams);
    final StreamThread originalThread = streamThreads[0];
    final TestStreamThread testStreamThread = new TestStreamThread(builder, streamsConfig, new DefaultKafkaClientSupplier(), originalThread.applicationId, originalThread.clientId, originalThread.processId, new Metrics(), Time.SYSTEM);
    final TestCondition oneTopicAdded = new TestCondition() {

        @Override
        public boolean conditionMet() {
            return testStreamThread.assignedTopicPartitions.equals(expectedFirstAssignment);
        }
    };
    streamThreads[0] = testStreamThread;
    streams.start();
    TestUtils.waitForCondition(oneTopicAdded, STREAM_TASKS_NOT_UPDATED);
    CLUSTER.createTopic("TEST-TOPIC-2");
    final TestCondition secondTopicAdded = new TestCondition() {

        @Override
        public boolean conditionMet() {
            return testStreamThread.assignedTopicPartitions.equals(expectedSecondAssignment);
        }
    };
    TestUtils.waitForCondition(secondTopicAdded, STREAM_TASKS_NOT_UPDATED);
    streams.close();
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) KafkaStreams(org.apache.kafka.streams.KafkaStreams) DefaultKafkaClientSupplier(org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread) Field(java.lang.reflect.Field) Metrics(org.apache.kafka.common.metrics.Metrics) TestCondition(org.apache.kafka.test.TestCondition) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Test(org.junit.Test)

Example 5 with TestCondition

use of org.apache.kafka.test.TestCondition in project kafka by apache.

the class AbstractCoordinatorTest method awaitFirstHeartbeat.

private void awaitFirstHeartbeat(final AtomicBoolean heartbeatReceived) throws Exception {
    mockTime.sleep(HEARTBEAT_INTERVAL_MS);
    TestUtils.waitForCondition(new TestCondition() {

        @Override
        public boolean conditionMet() {
            return heartbeatReceived.get();
        }
    }, 3000, "Should have received a heartbeat request after joining the group");
}
Also used : TestCondition(org.apache.kafka.test.TestCondition)

Aggregations

TestCondition (org.apache.kafka.test.TestCondition)11 Test (org.junit.Test)7 KafkaStreams (org.apache.kafka.streams.KafkaStreams)5 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)5 Metrics (org.apache.kafka.common.metrics.Metrics)4 Field (java.lang.reflect.Field)3 StreamsConfig (org.apache.kafka.streams.StreamsConfig)3 DefaultKafkaClientSupplier (org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier)3 StreamThread (org.apache.kafka.streams.processor.internals.StreamThread)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Properties (java.util.Properties)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)1 MockTime (org.apache.kafka.common.utils.MockTime)1 KafkaStreamsTest (org.apache.kafka.streams.KafkaStreamsTest)1 KeyValue (org.apache.kafka.streams.KeyValue)1 InvalidStateStoreException (org.apache.kafka.streams.errors.InvalidStateStoreException)1