Search in sources :

Example 16 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class ThrottleTest method testThrottle.

@Test
public void testThrottle() throws Exception {
    MockTime time = new MockTime(0, 0, 0);
    ThrottleMock throttle = new ThrottleMock(time, 3);
    Assert.assertFalse(throttle.increment());
    Assert.assertEquals(0, time.milliseconds());
    Assert.assertFalse(throttle.increment());
    Assert.assertEquals(0, time.milliseconds());
    Assert.assertFalse(throttle.increment());
    Assert.assertEquals(0, time.milliseconds());
    Assert.assertTrue(throttle.increment());
    Assert.assertEquals(100, time.milliseconds());
    time.sleep(50);
    Assert.assertFalse(throttle.increment());
    Assert.assertEquals(150, time.milliseconds());
    Assert.assertFalse(throttle.increment());
    Assert.assertEquals(150, time.milliseconds());
    Assert.assertTrue(throttle.increment());
    Assert.assertEquals(200, time.milliseconds());
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 17 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class KafkaProducerTest method testInitTransactionTimeout.

@Test(expected = TimeoutException.class)
public void testInitTransactionTimeout() {
    Properties props = new Properties();
    props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "bad-transaction");
    props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 5);
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    Time time = new MockTime();
    Cluster cluster = TestUtils.singletonCluster("topic", 1);
    Node node = cluster.nodes().get(0);
    Metadata metadata = new Metadata(0, Long.MAX_VALUE, true);
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    Producer<String, String> producer = new KafkaProducer<>(new ProducerConfig(ProducerConfig.addSerializerToConfig(props, new StringSerializer(), new StringSerializer())), new StringSerializer(), new StringSerializer(), metadata, client);
    try {
        producer.initTransactions();
        fail("initTransactions() should have raised TimeoutException");
    } finally {
        producer.close(0, TimeUnit.MILLISECONDS);
    }
}
Also used : Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 18 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class KafkaProducerTest method shouldCloseProperlyAndThrowIfInterrupted.

@Test
public void shouldCloseProperlyAndThrowIfInterrupted() throws Exception {
    Properties props = new Properties();
    props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    props.setProperty(ProducerConfig.PARTITIONER_CLASS_CONFIG, MockPartitioner.class.getName());
    props.setProperty(ProducerConfig.BATCH_SIZE_CONFIG, "1");
    Time time = new MockTime();
    Cluster cluster = TestUtils.singletonCluster("topic", 1);
    Node node = cluster.nodes().get(0);
    Metadata metadata = new Metadata(0, Long.MAX_VALUE, true);
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    final Producer<String, String> producer = new KafkaProducer<>(new ProducerConfig(ProducerConfig.addSerializerToConfig(props, new StringSerializer(), new StringSerializer())), new StringSerializer(), new StringSerializer(), metadata, client);
    ExecutorService executor = Executors.newSingleThreadExecutor();
    final AtomicReference<Exception> closeException = new AtomicReference<>();
    try {
        Future<?> future = executor.submit(new Runnable() {

            @Override
            public void run() {
                producer.send(new ProducerRecord<>("topic", "key", "value"));
                try {
                    producer.close();
                    fail("Close should block and throw.");
                } catch (Exception e) {
                    closeException.set(e);
                }
            }
        });
        // Close producer should not complete until send succeeds
        try {
            future.get(100, TimeUnit.MILLISECONDS);
            fail("Close completed without waiting for send");
        } catch (java.util.concurrent.TimeoutException expected) {
        /* ignore */
        }
        // Ensure send has started
        client.waitForRequests(1, 1000);
        assertTrue("Close terminated prematurely", future.cancel(true));
        TestUtils.waitForCondition(new TestCondition() {

            @Override
            public boolean conditionMet() {
                return closeException.get() != null;
            }
        }, "InterruptException did not occur within timeout.");
        assertTrue("Expected exception not thrown " + closeException, closeException.get() instanceof InterruptException);
    } finally {
        executor.shutdownNow();
    }
}
Also used : Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) InterruptException(org.apache.kafka.common.errors.InterruptException) Cluster(org.apache.kafka.common.Cluster) AtomicReference(java.util.concurrent.atomic.AtomicReference) KafkaException(org.apache.kafka.common.KafkaException) InterruptException(org.apache.kafka.common.errors.InterruptException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ConfigException(org.apache.kafka.common.config.ConfigException) MockPartitioner(org.apache.kafka.test.MockPartitioner) ExecutorService(java.util.concurrent.ExecutorService) TestCondition(org.apache.kafka.test.TestCondition) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 19 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class KafkaProducerTest method testOnlyCanExecuteCloseAfterInitTransactionsTimeout.

@Test(expected = KafkaException.class)
public void testOnlyCanExecuteCloseAfterInitTransactionsTimeout() {
    Properties props = new Properties();
    props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "bad-transaction");
    props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 5);
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    Time time = new MockTime();
    Cluster cluster = TestUtils.singletonCluster("topic", 1);
    Node node = cluster.nodes().get(0);
    Metadata metadata = new Metadata(0, Long.MAX_VALUE, true);
    metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    client.setNode(node);
    Producer<String, String> producer = new KafkaProducer<>(new ProducerConfig(ProducerConfig.addSerializerToConfig(props, new StringSerializer(), new StringSerializer())), new StringSerializer(), new StringSerializer(), metadata, client);
    try {
        producer.initTransactions();
    } catch (TimeoutException e) {
    // expected
    }
    // other transactional operations should not be allowed if we catch the error after initTransactions failed
    try {
        producer.beginTransaction();
    } finally {
        producer.close(0, TimeUnit.MILLISECONDS);
    }
}
Also used : Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) TimeoutException(org.apache.kafka.common.errors.TimeoutException) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 20 with MockTime

use of org.apache.kafka.common.utils.MockTime in project apache-kafka-on-k8s by banzaicloud.

the class KafkaProducerTest method testTopicRefreshInMetadata.

@Test
public void testTopicRefreshInMetadata() throws Exception {
    Properties props = new Properties();
    props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
    props.setProperty(ProducerConfig.MAX_BLOCK_MS_CONFIG, "600000");
    KafkaProducer<String, String> producer = new KafkaProducer<>(props, new StringSerializer(), new StringSerializer());
    long refreshBackoffMs = 500L;
    long metadataExpireMs = 60000L;
    final Metadata metadata = new Metadata(refreshBackoffMs, metadataExpireMs, true, true, new ClusterResourceListeners());
    final Time time = new MockTime();
    MemberModifier.field(KafkaProducer.class, "metadata").set(producer, metadata);
    MemberModifier.field(KafkaProducer.class, "time").set(producer, time);
    final String topic = "topic";
    Thread t = new Thread() {

        @Override
        public void run() {
            long startTimeMs = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {
                while (!metadata.updateRequested() && System.currentTimeMillis() - startTimeMs < 1000) yield();
                metadata.update(Cluster.empty(), Collections.singleton(topic), time.milliseconds());
                time.sleep(60 * 1000L);
            }
        }
    };
    t.start();
    try {
        producer.partitionsFor(topic);
        fail("Expect TimeoutException");
    } catch (TimeoutException e) {
    // skip
    }
    Assert.assertTrue("Topic should still exist in metadata", metadata.containsTopic(topic));
}
Also used : ClusterResourceListeners(org.apache.kafka.common.internals.ClusterResourceListeners) Metadata(org.apache.kafka.clients.Metadata) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) TimeoutException(org.apache.kafka.common.errors.TimeoutException) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Aggregations

MockTime (org.apache.kafka.common.utils.MockTime)363 Test (org.junit.Test)156 Test (org.junit.jupiter.api.Test)134 HashMap (java.util.HashMap)104 Time (org.apache.kafka.common.utils.Time)98 MockClient (org.apache.kafka.clients.MockClient)73 Cluster (org.apache.kafka.common.Cluster)73 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)65 Node (org.apache.kafka.common.Node)56 Metrics (org.apache.kafka.common.metrics.Metrics)56 TopicPartition (org.apache.kafka.common.TopicPartition)55 LogContext (org.apache.kafka.common.utils.LogContext)50 StreamsConfig (org.apache.kafka.streams.StreamsConfig)49 Before (org.junit.Before)38 Metadata (org.apache.kafka.clients.Metadata)32 MockScheduler (org.apache.kafka.common.utils.MockScheduler)31 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)30 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)30 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)29 Properties (java.util.Properties)27