Search in sources :

Example 1 with FutureRecordMetadata

use of org.apache.kafka.clients.producer.internals.FutureRecordMetadata in project kafka by apache.

the class MockProducer method send.

/**
     * Adds the record to the list of sent records.
     *
     * @see #history()
     */
@Override
public synchronized Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback callback) {
    int partition = 0;
    if (!this.cluster.partitionsForTopic(record.topic()).isEmpty())
        partition = partition(record, this.cluster);
    TopicPartition topicPartition = new TopicPartition(record.topic(), partition);
    ProduceRequestResult result = new ProduceRequestResult(topicPartition);
    FutureRecordMetadata future = new FutureRecordMetadata(result, 0, Record.NO_TIMESTAMP, 0, 0, 0);
    long offset = nextOffset(topicPartition);
    Completion completion = new Completion(offset, new RecordMetadata(topicPartition, 0, offset, Record.NO_TIMESTAMP, 0, 0, 0), result, callback);
    this.sent.add(record);
    if (autoComplete)
        completion.complete(null);
    else
        this.completions.addLast(completion);
    return future;
}
Also used : FutureRecordMetadata(org.apache.kafka.clients.producer.internals.FutureRecordMetadata) FutureRecordMetadata(org.apache.kafka.clients.producer.internals.FutureRecordMetadata) TopicPartition(org.apache.kafka.common.TopicPartition) ProduceRequestResult(org.apache.kafka.clients.producer.internals.ProduceRequestResult)

Example 2 with FutureRecordMetadata

use of org.apache.kafka.clients.producer.internals.FutureRecordMetadata in project kafka by apache.

the class RecordSendTest method testBlocking.

/**
     * Test that an asynchronous request will eventually return the right offset
     */
@Test
public void testBlocking() throws Exception {
    FutureRecordMetadata future = new FutureRecordMetadata(asyncRequest(baseOffset, null, 50L), relOffset, Record.NO_TIMESTAMP, 0, 0, 0);
    assertEquals(baseOffset + relOffset, future.get().offset());
}
Also used : FutureRecordMetadata(org.apache.kafka.clients.producer.internals.FutureRecordMetadata) Test(org.junit.Test)

Example 3 with FutureRecordMetadata

use of org.apache.kafka.clients.producer.internals.FutureRecordMetadata in project kafka by apache.

the class RecordSendTest method testTimeout.

/**
     * Test that waiting on a request that never completes times out
     */
@Test
public void testTimeout() throws Exception {
    ProduceRequestResult request = new ProduceRequestResult(topicPartition);
    FutureRecordMetadata future = new FutureRecordMetadata(request, relOffset, Record.NO_TIMESTAMP, 0, 0, 0);
    assertFalse("Request is not completed", future.isDone());
    try {
        future.get(5, TimeUnit.MILLISECONDS);
        fail("Should have thrown exception.");
    } catch (TimeoutException e) {
    /* this is good */
    }
    request.set(baseOffset, Record.NO_TIMESTAMP, null);
    request.done();
    assertTrue(future.isDone());
    assertEquals(baseOffset + relOffset, future.get().offset());
}
Also used : FutureRecordMetadata(org.apache.kafka.clients.producer.internals.FutureRecordMetadata) ProduceRequestResult(org.apache.kafka.clients.producer.internals.ProduceRequestResult) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 4 with FutureRecordMetadata

use of org.apache.kafka.clients.producer.internals.FutureRecordMetadata in project kafka by apache.

the class RecordSendTest method testError.

/**
     * Test that an asynchronous request will eventually throw the right exception
     */
@Test(expected = ExecutionException.class)
public void testError() throws Exception {
    FutureRecordMetadata future = new FutureRecordMetadata(asyncRequest(baseOffset, new CorruptRecordException(), 50L), relOffset, Record.NO_TIMESTAMP, 0, 0, 0);
    future.get();
}
Also used : FutureRecordMetadata(org.apache.kafka.clients.producer.internals.FutureRecordMetadata) CorruptRecordException(org.apache.kafka.common.errors.CorruptRecordException) Test(org.junit.Test)

Aggregations

FutureRecordMetadata (org.apache.kafka.clients.producer.internals.FutureRecordMetadata)4 Test (org.junit.Test)3 ProduceRequestResult (org.apache.kafka.clients.producer.internals.ProduceRequestResult)2 TimeoutException (java.util.concurrent.TimeoutException)1 TopicPartition (org.apache.kafka.common.TopicPartition)1 CorruptRecordException (org.apache.kafka.common.errors.CorruptRecordException)1