Search in sources :

Example 1 with PoolStats

use of org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats in project nifi by apache.

the class ConsumerPoolTest method validatePoolSimpleCreatePollClose.

@Test
@SuppressWarnings("unchecked")
public void validatePoolSimpleCreatePollClose() throws Exception {
    final byte[][] firstPassValues = new byte[][] { "Hello-1".getBytes(StandardCharsets.UTF_8), "Hello-2".getBytes(StandardCharsets.UTF_8), "Hello-3".getBytes(StandardCharsets.UTF_8) };
    final ConsumerRecords<byte[], byte[]> firstRecs = createConsumerRecords("foo", 1, 1L, firstPassValues);
    when(consumer.poll(anyLong())).thenReturn(firstRecs, createConsumerRecords("nifi", 0, 0L, new byte[][] {}));
    try (final ConsumerLease lease = testPool.obtainConsumer(mockSession, mockContext)) {
        lease.poll();
        lease.commit();
    }
    testPool.close();
    verify(mockSession, times(3)).create();
    verify(mockSession, times(1)).commit();
    final PoolStats stats = testPool.getPoolStats();
    assertEquals(1, stats.consumerCreatedCount);
    assertEquals(1, stats.consumerClosedCount);
    assertEquals(1, stats.leasesObtainedCount);
}
Also used : PoolStats(org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats) Test(org.junit.Test)

Example 2 with PoolStats

use of org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats in project nifi by apache.

the class ConsumerPoolTest method validatePoolBatchCreatePollClose.

@Test
@SuppressWarnings("unchecked")
public void validatePoolBatchCreatePollClose() throws Exception {
    final byte[][] firstPassValues = new byte[][] { "Hello-1".getBytes(StandardCharsets.UTF_8), "Hello-2".getBytes(StandardCharsets.UTF_8), "Hello-3".getBytes(StandardCharsets.UTF_8) };
    final ConsumerRecords<byte[], byte[]> firstRecs = createConsumerRecords("foo", 1, 1L, firstPassValues);
    when(consumer.poll(anyLong())).thenReturn(firstRecs, createConsumerRecords("nifi", 0, 0L, new byte[][] {}));
    try (final ConsumerLease lease = testDemarcatedPool.obtainConsumer(mockSession, mockContext)) {
        lease.poll();
        lease.commit();
    }
    testDemarcatedPool.close();
    verify(mockSession, times(1)).create();
    verify(mockSession, times(1)).commit();
    final PoolStats stats = testDemarcatedPool.getPoolStats();
    assertEquals(1, stats.consumerCreatedCount);
    assertEquals(1, stats.consumerClosedCount);
    assertEquals(1, stats.leasesObtainedCount);
}
Also used : PoolStats(org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats) Test(org.junit.Test)

Example 3 with PoolStats

use of org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats in project nifi by apache.

the class ConsumerPoolTest method validatePoolConsumerFails.

@Test
public void validatePoolConsumerFails() throws Exception {
    when(consumer.poll(anyLong())).thenThrow(new KafkaException("oops"));
    try (final ConsumerLease lease = testPool.obtainConsumer(mockSession, mockContext)) {
        try {
            lease.poll();
            fail();
        } catch (final KafkaException ke) {
        }
    }
    testPool.close();
    verify(mockSession, times(0)).create();
    verify(mockSession, times(0)).commit();
    final PoolStats stats = testPool.getPoolStats();
    assertEquals(1, stats.consumerCreatedCount);
    assertEquals(1, stats.consumerClosedCount);
    assertEquals(1, stats.leasesObtainedCount);
}
Also used : KafkaException(org.apache.kafka.common.KafkaException) PoolStats(org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats) Test(org.junit.Test)

Example 4 with PoolStats

use of org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats in project nifi by apache.

the class ConsumerPoolTest method validatePoolSimpleCreateClose.

@Test
public void validatePoolSimpleCreateClose() throws Exception {
    when(consumer.poll(anyLong())).thenReturn(createConsumerRecords("nifi", 0, 0L, new byte[][] {}));
    try (final ConsumerLease lease = testPool.obtainConsumer(mockSession, mockContext)) {
        lease.poll();
    }
    try (final ConsumerLease lease = testPool.obtainConsumer(mockSession, mockContext)) {
        lease.poll();
    }
    try (final ConsumerLease lease = testPool.obtainConsumer(mockSession, mockContext)) {
        lease.poll();
    }
    try (final ConsumerLease lease = testPool.obtainConsumer(mockSession, mockContext)) {
        lease.poll();
    }
    testPool.close();
    verify(mockSession, times(0)).create();
    verify(mockSession, times(0)).commit();
    final PoolStats stats = testPool.getPoolStats();
    assertEquals(1, stats.consumerCreatedCount);
    assertEquals(1, stats.consumerClosedCount);
    assertEquals(4, stats.leasesObtainedCount);
}
Also used : PoolStats(org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats) Test(org.junit.Test)

Example 5 with PoolStats

use of org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats in project nifi by apache.

the class ConsumerPoolTest method validatePoolSimpleBatchCreateClose.

@Test
public void validatePoolSimpleBatchCreateClose() throws Exception {
    when(consumer.poll(anyLong())).thenReturn(createConsumerRecords("nifi", 0, 0L, new byte[][] {}));
    for (int i = 0; i < 100; i++) {
        try (final ConsumerLease lease = testPool.obtainConsumer(mockSession, mockContext)) {
            for (int j = 0; j < 100; j++) {
                lease.poll();
            }
        }
    }
    testPool.close();
    verify(mockSession, times(0)).create();
    verify(mockSession, times(0)).commit();
    final PoolStats stats = testPool.getPoolStats();
    assertEquals(1, stats.consumerCreatedCount);
    assertEquals(1, stats.consumerClosedCount);
    assertEquals(100, stats.leasesObtainedCount);
}
Also used : PoolStats(org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats) Test(org.junit.Test)

Aggregations

PoolStats (org.apache.nifi.processors.kafka.pubsub.ConsumerPool.PoolStats)5 Test (org.junit.Test)5 KafkaException (org.apache.kafka.common.KafkaException)1