Search in sources :

Example 26 with MockTime

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

the class KafkaStatusBackingStoreTest method readTaskState.

@Test
public void readTaskState() {
    byte[] value = new byte[0];
    KafkaBasedLog<String, byte[]> kafkaBasedLog = mock(KafkaBasedLog.class);
    Converter converter = mock(Converter.class);
    KafkaStatusBackingStore store = new KafkaStatusBackingStore(new MockTime(), converter, STATUS_TOPIC, kafkaBasedLog);
    Map<String, Object> statusMap = new HashMap<>();
    statusMap.put("worker_id", WORKER_ID);
    statusMap.put("state", "RUNNING");
    statusMap.put("generation", 0L);
    expect(converter.toConnectData(STATUS_TOPIC, value)).andReturn(new SchemaAndValue(null, statusMap));
    replayAll();
    store.read(consumerRecord(0, "status-task-conn-0", value));
    TaskStatus status = new TaskStatus(TASK, TaskStatus.State.RUNNING, WORKER_ID, 0);
    assertEquals(status, store.get(TASK));
    verifyAll();
}
Also used : HashMap(java.util.HashMap) EasyMock.anyObject(org.easymock.EasyMock.anyObject) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) MockTime(org.apache.kafka.common.utils.MockTime) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 27 with MockTime

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

the class KafkaStatusBackingStoreTest method putSafeConnectorIgnoresStaleStatus.

@Test
public void putSafeConnectorIgnoresStaleStatus() {
    byte[] value = new byte[0];
    String otherWorkerId = "anotherhost:8083";
    KafkaBasedLog<String, byte[]> kafkaBasedLog = mock(KafkaBasedLog.class);
    Converter converter = mock(Converter.class);
    KafkaStatusBackingStore store = new KafkaStatusBackingStore(new MockTime(), converter, STATUS_TOPIC, kafkaBasedLog);
    // the persisted came from a different host and has a newer generation
    Map<String, Object> statusMap = new HashMap<>();
    statusMap.put("worker_id", otherWorkerId);
    statusMap.put("state", "RUNNING");
    statusMap.put("generation", 1L);
    expect(converter.toConnectData(STATUS_TOPIC, value)).andReturn(new SchemaAndValue(null, statusMap));
    // we're verifying that there is no call to KafkaBasedLog.send
    replayAll();
    store.read(consumerRecord(0, "status-connector-conn", value));
    store.putSafe(new ConnectorStatus(CONNECTOR, ConnectorStatus.State.UNASSIGNED, WORKER_ID, 0));
    ConnectorStatus status = new ConnectorStatus(CONNECTOR, ConnectorStatus.State.RUNNING, otherWorkerId, 1);
    assertEquals(status, store.get(CONNECTOR));
    verifyAll();
}
Also used : HashMap(java.util.HashMap) ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) EasyMock.anyObject(org.easymock.EasyMock.anyObject) MockTime(org.apache.kafka.common.utils.MockTime) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 28 with MockTime

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

the class AbstractCoordinatorTest method setupCoordinator.

private void setupCoordinator(long retryBackoffMs) {
    this.mockTime = new MockTime();
    this.mockClient = new MockClient(mockTime);
    Metadata metadata = new Metadata(100L, 60 * 60 * 1000L, true);
    this.consumerClient = new ConsumerNetworkClient(new LogContext(), mockClient, metadata, mockTime, retryBackoffMs, REQUEST_TIMEOUT_MS, HEARTBEAT_INTERVAL_MS);
    Metrics metrics = new Metrics();
    Cluster cluster = TestUtils.singletonCluster("topic", 1);
    metadata.update(cluster, Collections.<String>emptySet(), mockTime.milliseconds());
    this.node = cluster.nodes().get(0);
    mockClient.setNode(node);
    this.coordinatorNode = new Node(Integer.MAX_VALUE - node.id(), node.host(), node.port());
    this.coordinator = new DummyCoordinator(consumerClient, metrics, mockTime);
}
Also used : Metrics(org.apache.kafka.common.metrics.Metrics) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) LogContext(org.apache.kafka.common.utils.LogContext) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient)

Example 29 with MockTime

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

the class SensorTest method testExpiredSensor.

@Test
public void testExpiredSensor() {
    MetricConfig config = new MetricConfig();
    Time mockTime = new MockTime();
    Metrics metrics = new Metrics(config, Arrays.asList((MetricsReporter) 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));
    metrics.close();
}
Also used : 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) MetricName(org.apache.kafka.common.MetricName) Avg(org.apache.kafka.common.metrics.stats.Avg) MockTime(org.apache.kafka.common.utils.MockTime) Test(org.junit.Test)

Example 30 with MockTime

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

the class FrequenciesTest method setup.

@Before
public void setup() {
    config = new MetricConfig().eventWindow(50).samples(2);
    time = new MockTime();
    metrics = new Metrics(config, Arrays.asList((MetricsReporter) new JmxReporter()), time, true);
}
Also used : MetricConfig(org.apache.kafka.common.metrics.MetricConfig) Metrics(org.apache.kafka.common.metrics.Metrics) JmxReporter(org.apache.kafka.common.metrics.JmxReporter) MockTime(org.apache.kafka.common.utils.MockTime) Before(org.junit.Before)

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