Search in sources :

Example 26 with Time

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

the class SensorTest method testExpiredSensor.

@Test
public void testExpiredSensor() {
    MetricConfig config = new MetricConfig();
    Time mockTime = new MockTime();
    try (Metrics metrics = new Metrics(config, Arrays.asList(new JmxReporter()), mockTime, true)) {
        long inactiveSensorExpirationTimeSeconds = 60L;
        Sensor sensor = new Sensor(metrics, "sensor", null, config, mockTime, inactiveSensorExpirationTimeSeconds, Sensor.RecordingLevel.INFO);
        assertTrue(sensor.add(metrics.metricName("test1", "grp1"), new Avg()));
        Map<String, String> emptyTags = Collections.emptyMap();
        MetricName rateMetricName = new MetricName("rate", "test", "", emptyTags);
        MetricName totalMetricName = new MetricName("total", "test", "", emptyTags);
        Meter meter = new Meter(rateMetricName, totalMetricName);
        assertTrue(sensor.add(meter));
        mockTime.sleep(TimeUnit.SECONDS.toMillis(inactiveSensorExpirationTimeSeconds + 1));
        assertFalse(sensor.add(metrics.metricName("test3", "grp1"), new Avg()));
        assertFalse(sensor.add(meter));
    }
}
Also used : MetricName(org.apache.kafka.common.MetricName) Avg(org.apache.kafka.common.metrics.stats.Avg) Meter(org.apache.kafka.common.metrics.stats.Meter) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) SystemTime(org.apache.kafka.common.utils.SystemTime) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 27 with Time

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

the class SensorTest method testRecordAndCheckQuotaUseMetricConfigOfEachStat.

@Test
public void testRecordAndCheckQuotaUseMetricConfigOfEachStat() {
    final Time time = new MockTime(0, System.currentTimeMillis(), 0);
    final Metrics metrics = new Metrics(time);
    final Sensor sensor = metrics.sensor("sensor");
    final MeasurableStat stat1 = Mockito.mock(MeasurableStat.class);
    final MetricName stat1Name = metrics.metricName("stat1", "test-group");
    final MetricConfig stat1Config = new MetricConfig().quota(Quota.upperBound(5));
    sensor.add(stat1Name, stat1, stat1Config);
    final MeasurableStat stat2 = Mockito.mock(MeasurableStat.class);
    final MetricName stat2Name = metrics.metricName("stat2", "test-group");
    final MetricConfig stat2Config = new MetricConfig().quota(Quota.upperBound(10));
    sensor.add(stat2Name, stat2, stat2Config);
    sensor.record(10, 1);
    Mockito.verify(stat1).record(stat1Config, 10, 1);
    Mockito.verify(stat2).record(stat2Config, 10, 1);
    sensor.checkQuotas(2);
    Mockito.verify(stat1).measure(stat1Config, 2);
    Mockito.verify(stat2).measure(stat2Config, 2);
    metrics.close();
}
Also used : MetricName(org.apache.kafka.common.MetricName) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) SystemTime(org.apache.kafka.common.utils.SystemTime) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 28 with Time

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

the class RefreshingHttpsJwksTest method testMaybeExpediteRefreshNoDelay.

/**
 * Test that a key previously scheduled for refresh will <b>not</b> be scheduled a second time
 * if it's requested right away.
 */
@Test
public void testMaybeExpediteRefreshNoDelay() throws Exception {
    String keyId = "abc123";
    Time time = new MockTime();
    HttpsJwks httpsJwks = spyHttpsJwks();
    try (RefreshingHttpsJwks refreshingHttpsJwks = getRefreshingHttpsJwks(time, httpsJwks)) {
        refreshingHttpsJwks.init();
        assertTrue(refreshingHttpsJwks.maybeExpediteRefresh(keyId));
        assertFalse(refreshingHttpsJwks.maybeExpediteRefresh(keyId));
    }
}
Also used : HttpsJwks(org.jose4j.jwk.HttpsJwks) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.jupiter.api.Test)

Example 29 with Time

use of org.apache.kafka.common.utils.Time 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 30 with Time

use of org.apache.kafka.common.utils.Time 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

Time (org.apache.kafka.common.utils.Time)125 MockTime (org.apache.kafka.common.utils.MockTime)107 Test (org.junit.jupiter.api.Test)63 MockClient (org.apache.kafka.clients.MockClient)55 HashMap (java.util.HashMap)53 Cluster (org.apache.kafka.common.Cluster)41 Test (org.junit.Test)40 Node (org.apache.kafka.common.Node)39 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)32 MetadataResponse (org.apache.kafka.common.requests.MetadataResponse)31 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)30 Metadata (org.apache.kafka.clients.Metadata)28 ProducerMetadata (org.apache.kafka.clients.producer.internals.ProducerMetadata)25 TopicPartition (org.apache.kafka.common.TopicPartition)22 PartitionAssignor (org.apache.kafka.clients.consumer.internals.PartitionAssignor)21 LogContext (org.apache.kafka.common.utils.LogContext)17 Map (java.util.Map)14 Properties (java.util.Properties)14 MetricName (org.apache.kafka.common.MetricName)14 ExecutionException (java.util.concurrent.ExecutionException)13