Search in sources :

Example 1 with Time

use of org.apache.kafka.common.utils.Time in project cruise-control by linkedin.

the class SessionManagerTest method prepareRequests.

private TestContext prepareRequests(boolean expectSessionInvalidation, int numRequests) {
    Time time = new MockTime();
    List<HttpServletRequest> requests = new ArrayList<>(numRequests);
    List<HttpSession> sessions = new ArrayList<>();
    for (int i = 0; i < numRequests; i++) {
        HttpServletRequest request = EasyMock.mock(HttpServletRequest.class);
        HttpSession session = EasyMock.mock(HttpSession.class);
        requests.add(request);
        sessions.add(session);
        EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
        EasyMock.expect(request.getSession(false)).andReturn(session).anyTimes();
        EasyMock.expect(request.getMethod()).andReturn("GET").anyTimes();
        EasyMock.expect(request.getRequestURI()).andReturn("/test").anyTimes();
        EasyMock.expect(request.getParameterMap()).andReturn(Collections.emptyMap()).anyTimes();
        EasyMock.expect(session.getLastAccessedTime()).andReturn(time.milliseconds()).anyTimes();
        if (expectSessionInvalidation) {
            session.invalidate();
            EasyMock.expectLastCall().once();
        }
    }
    EasyMock.replay(requests.toArray());
    EasyMock.replay(sessions.toArray());
    return new TestContext(requests, time);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) MockTime(org.apache.kafka.common.utils.MockTime)

Example 2 with Time

use of org.apache.kafka.common.utils.Time in project cruise-control by linkedin.

the class BrokerFailureDetectorTest method testLoadFailedBrokersFromZK.

@Test
public void testLoadFailedBrokersFromZK() throws Exception {
    Time mockTime = getMockTime();
    Queue<Anomaly> anomalies = new ConcurrentLinkedQueue<>();
    BrokerFailureDetector detector = createBrokerFailureDetector(anomalies, mockTime);
    try {
        detector.startDetection();
        int brokerId = 0;
        killBroker(brokerId);
        long start = System.currentTimeMillis();
        while (anomalies.isEmpty() && System.currentTimeMillis() < start + 30000) {
        // Wait for the anomalies to be drained.
        }
        assertEquals(Collections.singletonMap(brokerId, 100L), detector.failedBrokers());
        // shutdown, advance the clock and create a new detector.
        detector.shutdown();
        mockTime.sleep(100L);
        detector = createBrokerFailureDetector(anomalies, mockTime);
        // start the newly created detector and the broker down time should remain previous time.
        detector.startDetection();
        assertEquals(Collections.singletonMap(brokerId, 100L), detector.failedBrokers());
    } finally {
        detector.shutdown();
    }
}
Also used : MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.Test)

Example 3 with Time

use of org.apache.kafka.common.utils.Time in project kafka-connect-cdc-mssql by jcustenborder.

the class QueryServiceIT method queryTable.

private void queryTable(ChangeKey input) throws SQLException, IOException {
    List<Change> expectedChanges;
    String fileName = String.format("%s.%s.json", input.schemaName, input.tableName);
    String resourceName = String.format("query/table/%s/%s", input.databaseName, fileName);
    long timestamp = 0L;
    try (InputStream stream = this.getClass().getResourceAsStream(resourceName)) {
        Preconditions.checkNotNull(stream, "Could not find resource %s.", resourceName);
        log.info("Loading expected changes from {}", resourceName);
        expectedChanges = loadChanges(stream);
        for (Change change : expectedChanges) {
            timestamp = change.timestamp();
            break;
        }
    }
    OffsetStorageReader offsetStorageReader = mock(OffsetStorageReader.class);
    TableMetadataProvider tableMetadataProvider = new MsSqlTableMetadataProvider(config, offsetStorageReader);
    Time time = mock(Time.class);
    ChangeWriter changeWriter = mock(ChangeWriter.class);
    List<Change> actualChanges = new ArrayList<>(1000);
    doAnswer(invocationOnMock -> {
        Change change = invocationOnMock.getArgument(0);
        actualChanges.add(change);
        return null;
    }).when(changeWriter).addChange(any());
    QueryService queryService = new QueryService(time, tableMetadataProvider, config, changeWriter);
    when(time.milliseconds()).thenReturn(timestamp);
    queryService.queryTable(changeWriter, input);
    verify(offsetStorageReader, only()).offset(anyMap());
    verify(time, atLeastOnce()).milliseconds();
    if (log.isDebugEnabled()) {
        log.trace("Found {} change(s).", actualChanges.size());
    }
    assertFalse(actualChanges.isEmpty(), "Changes should have been returned.");
    assertEquals(expectedChanges.size(), actualChanges.size(), "The number of actualChanges returned is not the expect count.");
    for (int i = 0; i < expectedChanges.size(); i++) {
        Change expectedChange = expectedChanges.get(i);
        Change actualChange = actualChanges.get(i);
        assertChange(expectedChange, actualChange);
    }
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Time(org.apache.kafka.common.utils.Time) Change(com.github.jcustenborder.kafka.connect.cdc.Change) ChangeAssertions.assertChange(com.github.jcustenborder.kafka.connect.cdc.ChangeAssertions.assertChange) TableMetadataProvider(com.github.jcustenborder.kafka.connect.cdc.TableMetadataProvider) OffsetStorageReader(org.apache.kafka.connect.storage.OffsetStorageReader) ChangeWriter(com.github.jcustenborder.kafka.connect.cdc.ChangeWriter)

Example 4 with Time

use of org.apache.kafka.common.utils.Time 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);
    }
}
Also used : Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) Cluster(org.apache.kafka.common.Cluster) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.Test)

Example 5 with Time

use of org.apache.kafka.common.utils.Time 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();
    }
}
Also used : Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) MockTime(org.apache.kafka.common.utils.MockTime) Time(org.apache.kafka.common.utils.Time) Properties(java.util.Properties) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) MockTime(org.apache.kafka.common.utils.MockTime) MockClient(org.apache.kafka.clients.MockClient) InterruptException(org.apache.kafka.common.errors.InterruptException) Cluster(org.apache.kafka.common.Cluster) AtomicReference(java.util.concurrent.atomic.AtomicReference) KafkaException(org.apache.kafka.common.KafkaException) InterruptException(org.apache.kafka.common.errors.InterruptException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) ConfigException(org.apache.kafka.common.config.ConfigException) MockPartitioner(org.apache.kafka.test.MockPartitioner) ExecutorService(java.util.concurrent.ExecutorService) TestCondition(org.apache.kafka.test.TestCondition) PrepareOnlyThisForTest(org.powermock.core.classloader.annotations.PrepareOnlyThisForTest) Test(org.junit.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