Search in sources :

Example 16 with MockInternalProcessorContext

use of org.apache.kafka.test.MockInternalProcessorContext in project kafka by apache.

the class TimeOrderedKeyValueBufferTest method makeContext.

private static MockInternalProcessorContext makeContext() {
    final Properties properties = new Properties();
    properties.setProperty(StreamsConfig.APPLICATION_ID_CONFIG, APP_ID);
    properties.setProperty(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "");
    final TaskId taskId = new TaskId(0, 0);
    final MockInternalProcessorContext context = new MockInternalProcessorContext(properties, taskId, TestUtils.tempDirectory());
    context.setRecordCollector(new MockRecordCollector());
    return context;
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) MockRecordCollector(org.apache.kafka.test.MockRecordCollector) MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) Properties(java.util.Properties)

Example 17 with MockInternalProcessorContext

use of org.apache.kafka.test.MockInternalProcessorContext in project kafka by apache.

the class TimeOrderedKeyValueBufferTest method shouldEvictOldestAndUpdateSizeAndCountAndMinTimestamp.

@Test
public void shouldEvictOldestAndUpdateSizeAndCountAndMinTimestamp() {
    final TimeOrderedKeyValueBuffer<String, String> buffer = bufferSupplier.apply(testName);
    final MockInternalProcessorContext context = makeContext();
    buffer.init((StateStoreContext) context, buffer);
    putRecord(buffer, context, 1L, 0L, "zxcv", "o23i4");
    assertThat(buffer.numRecords(), is(1));
    assertThat(buffer.bufferSize(), is(42L));
    assertThat(buffer.minTimestamp(), is(1L));
    putRecord(buffer, context, 0L, 0L, "asdf", "3ng");
    assertThat(buffer.numRecords(), is(2));
    assertThat(buffer.bufferSize(), is(82L));
    assertThat(buffer.minTimestamp(), is(0L));
    final AtomicInteger callbackCount = new AtomicInteger(0);
    buffer.evictWhile(() -> true, kv -> {
        switch(callbackCount.incrementAndGet()) {
            case 1:
                {
                    assertThat(kv.key(), is("asdf"));
                    assertThat(buffer.numRecords(), is(2));
                    assertThat(buffer.bufferSize(), is(82L));
                    assertThat(buffer.minTimestamp(), is(0L));
                    break;
                }
            case 2:
                {
                    assertThat(kv.key(), is("zxcv"));
                    assertThat(buffer.numRecords(), is(1));
                    assertThat(buffer.bufferSize(), is(42L));
                    assertThat(buffer.minTimestamp(), is(1L));
                    break;
                }
            default:
                {
                    fail("too many invocations");
                    break;
                }
        }
    });
    assertThat(callbackCount.get(), is(2));
    assertThat(buffer.numRecords(), is(0));
    assertThat(buffer.bufferSize(), is(0L));
    assertThat(buffer.minTimestamp(), is(Long.MAX_VALUE));
    cleanup(context, buffer);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) Test(org.junit.Test)

Example 18 with MockInternalProcessorContext

use of org.apache.kafka.test.MockInternalProcessorContext in project kafka by apache.

the class TimeOrderedKeyValueBufferTest method shouldRejectNullValues.

@Test
public void shouldRejectNullValues() {
    final TimeOrderedKeyValueBuffer<String, String> buffer = bufferSupplier.apply(testName);
    final MockInternalProcessorContext context = makeContext();
    buffer.init((StateStoreContext) context, buffer);
    try {
        buffer.put(0, new Record<>("asdf", null, 0L), getContext(0));
        fail("expected an exception");
    } catch (final NullPointerException expected) {
    // expected
    }
    cleanup(context, buffer);
}
Also used : MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) Test(org.junit.Test)

Example 19 with MockInternalProcessorContext

use of org.apache.kafka.test.MockInternalProcessorContext in project kafka by apache.

the class TimeOrderedKeyValueBufferTest method shouldRemoveData.

@Test
public void shouldRemoveData() {
    final TimeOrderedKeyValueBuffer<String, String> buffer = bufferSupplier.apply(testName);
    final MockInternalProcessorContext context = makeContext();
    buffer.init((StateStoreContext) context, buffer);
    putRecord(buffer, context, 0L, 0L, "asdf", "qwer");
    assertThat(buffer.numRecords(), is(1));
    buffer.evictWhile(() -> true, kv -> {
    });
    assertThat(buffer.numRecords(), is(0));
    cleanup(context, buffer);
}
Also used : MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) Test(org.junit.Test)

Example 20 with MockInternalProcessorContext

use of org.apache.kafka.test.MockInternalProcessorContext in project kafka by apache.

the class TimeOrderedKeyValueBufferTest method shouldReturnUndefinedOnPriorValueForNotBufferedKey.

@Test
public void shouldReturnUndefinedOnPriorValueForNotBufferedKey() {
    final TimeOrderedKeyValueBuffer<String, String> buffer = bufferSupplier.apply(testName);
    final MockInternalProcessorContext context = makeContext();
    buffer.init((StateStoreContext) context, buffer);
    assertThat(buffer.priorValueForBuffered("ASDF"), is(Maybe.undefined()));
}
Also used : MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) Test(org.junit.Test)

Aggregations

MockInternalProcessorContext (org.apache.kafka.test.MockInternalProcessorContext)20 Test (org.junit.Test)18 Change (org.apache.kafka.streams.kstream.internals.Change)8 ProcessorRecordContext (org.apache.kafka.streams.processor.internals.ProcessorRecordContext)8 LinkedList (java.util.LinkedList)7 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)7 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)7 RecordBatchingStateRestoreCallback (org.apache.kafka.streams.processor.internals.RecordBatchingStateRestoreCallback)7 Eviction (org.apache.kafka.streams.state.internals.TimeOrderedKeyValueBuffer.Eviction)7 RecordHeader (org.apache.kafka.common.header.internals.RecordHeader)6 Properties (java.util.Properties)3 TaskId (org.apache.kafka.streams.processor.TaskId)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 KeyValue (org.apache.kafka.streams.KeyValue)2 StateStoreContext (org.apache.kafka.streams.processor.StateStoreContext)2 File (java.io.File)1 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 ByteBuffer (java.nio.ByteBuffer)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1