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());
}
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);
}
}
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();
}
}
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);
}
}
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));
}
Aggregations