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