Search in sources :

Example 61 with MockTime

use of org.apache.kafka.common.utils.MockTime in project kafka by apache.

the class StartAndStopCounterTest method setup.

@Before
public void setup() {
    clock = new MockTime();
    counter = new StartAndStopCounter(clock);
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Before(org.junit.Before)

Example 62 with MockTime

use of org.apache.kafka.common.utils.MockTime in project kafka by apache.

the class ErrorHandlingTaskTest method setup.

@Before
public void setup() {
    time = new MockTime(0, 0, 0);
    metrics = new MockConnectMetrics();
    Map<String, String> workerProps = new HashMap<>();
    workerProps.put("key.converter", "org.apache.kafka.connect.json.JsonConverter");
    workerProps.put("value.converter", "org.apache.kafka.connect.json.JsonConverter");
    workerProps.put("offset.storage.file.filename", "/tmp/connect.offsets");
    workerProps.put(TOPIC_CREATION_ENABLE_CONFIG, String.valueOf(enableTopicCreation));
    pluginLoader = PowerMock.createMock(PluginClassLoader.class);
    workerConfig = new StandaloneConfig(workerProps);
    sourceConfig = new SourceConnectorConfig(plugins, sourceConnectorProps(TOPIC), true);
    errorHandlingMetrics = new ErrorHandlingMetrics(taskId, metrics);
}
Also used : HashMap(java.util.HashMap) ErrorHandlingMetrics(org.apache.kafka.connect.runtime.errors.ErrorHandlingMetrics) StandaloneConfig(org.apache.kafka.connect.runtime.standalone.StandaloneConfig) MockTime(org.apache.kafka.common.utils.MockTime) PluginClassLoader(org.apache.kafka.connect.runtime.isolation.PluginClassLoader) Before(org.junit.Before)

Example 63 with MockTime

use of org.apache.kafka.common.utils.MockTime in project kafka by apache.

the class SenderTest method testNodeNotReady.

/**
 * Tests the code path where the target node to send FindCoordinator or InitProducerId
 * is not ready.
 */
@Test
public void testNodeNotReady() {
    final long producerId = 123456L;
    time = new MockTime(10);
    client = new MockClient(time, metadata);
    TransactionManager transactionManager = new TransactionManager(new LogContext(), "testNodeNotReady", 60000, 100L, new ApiVersions());
    setupWithTransactionState(transactionManager, false, null, true);
    ProducerIdAndEpoch producerIdAndEpoch = new ProducerIdAndEpoch(producerId, (short) 0);
    transactionManager.initializeTransactions();
    sender.runOnce();
    Node node = metadata.fetch().nodes().get(0);
    client.delayReady(node, REQUEST_TIMEOUT + 20);
    prepareFindCoordinatorResponse(Errors.NONE, "testNodeNotReady");
    sender.runOnce();
    sender.runOnce();
    assertNotNull(transactionManager.coordinator(CoordinatorType.TRANSACTION), "Coordinator not found");
    client.throttle(node, REQUEST_TIMEOUT + 20);
    prepareFindCoordinatorResponse(Errors.NONE, "Coordinator not found");
    prepareInitProducerResponse(Errors.NONE, producerIdAndEpoch.producerId, producerIdAndEpoch.epoch);
    waitForProducerId(transactionManager, producerIdAndEpoch);
}
Also used : Node(org.apache.kafka.common.Node) NodeApiVersions(org.apache.kafka.clients.NodeApiVersions) ApiVersions(org.apache.kafka.clients.ApiVersions) LogContext(org.apache.kafka.common.utils.LogContext) ProducerIdAndEpoch(org.apache.kafka.common.utils.ProducerIdAndEpoch) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) Test(org.junit.jupiter.api.Test)

Example 64 with MockTime

use of org.apache.kafka.common.utils.MockTime in project kafka by apache.

the class KafkaProducerTest method testInitTransactionWhileThrottled.

@Test
public void testInitTransactionWhileThrottled() {
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "some.id");
    configs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10000);
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    Time time = new MockTime(1);
    MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("topic", 1));
    ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
    MockClient client = new MockClient(time, metadata);
    client.updateMetadata(initialUpdateResponse);
    Node node = metadata.fetch().nodes().get(0);
    client.throttle(node, 5000);
    client.prepareResponse(FindCoordinatorResponse.prepareResponse(Errors.NONE, "some.id", NODE));
    client.prepareResponse(initProducerIdResponse(1L, (short) 5, Errors.NONE));
    try (Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time)) {
        producer.initTransactions();
    }
}
Also used : ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 65 with MockTime

use of org.apache.kafka.common.utils.MockTime in project kafka by apache.

the class KafkaProducerTest method testCloseIsForcedOnPendingFindCoordinator.

@Test
public void testCloseIsForcedOnPendingFindCoordinator() throws InterruptedException {
    Map<String, Object> configs = new HashMap<>();
    configs.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
    configs.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "this-is-a-transactional-id");
    Time time = new MockTime();
    MetadataResponse initialUpdateResponse = RequestTestUtils.metadataUpdateWith(1, singletonMap("testTopic", 1));
    ProducerMetadata metadata = newMetadata(0, Long.MAX_VALUE);
    metadata.updateWithCurrentRequestVersion(initialUpdateResponse, false, time.milliseconds());
    MockClient client = new MockClient(time, metadata);
    Producer<String, String> producer = kafkaProducer(configs, new StringSerializer(), new StringSerializer(), metadata, client, null, time);
    ExecutorService executorService = Executors.newSingleThreadExecutor();
    CountDownLatch assertionDoneLatch = new CountDownLatch(1);
    executorService.submit(() -> {
        assertThrows(KafkaException.class, producer::initTransactions);
        assertionDoneLatch.countDown();
    });
    client.waitForRequests(1, 2000);
    producer.close(Duration.ofMillis(1000));
    assertionDoneLatch.await(5000, TimeUnit.MILLISECONDS);
}
Also used : ProducerMetadata(org.apache.kafka.clients.producer.internals.ProducerMetadata) HashMap(java.util.HashMap) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) CountDownLatch(java.util.concurrent.CountDownLatch) MetadataResponse(org.apache.kafka.common.requests.MetadataResponse) ExecutorService(java.util.concurrent.ExecutorService) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Aggregations

MockTime (org.apache.kafka.common.utils.MockTime)371 Test (org.junit.Test)158 Test (org.junit.jupiter.api.Test)134 HashMap (java.util.HashMap)103 Time (org.apache.kafka.common.utils.Time)102 Cluster (org.apache.kafka.common.Cluster)74 MockClient (org.apache.kafka.clients.MockClient)73 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)65 Node (org.apache.kafka.common.Node)57 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 Properties (java.util.Properties)34 Metadata (org.apache.kafka.clients.Metadata)33 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