Search in sources :

Example 1 with MockRecordCollector

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

the class SegmentIteratorTest method before.

@SuppressWarnings("rawtypes")
@Before
public void before() {
    final InternalMockProcessorContext context = new InternalMockProcessorContext<>(TestUtils.tempDirectory(), Serdes.String(), Serdes.String(), new MockRecordCollector(), new ThreadCache(new LogContext("testCache "), 0, new MockStreamsMetrics(new Metrics())));
    segmentOne.init((StateStoreContext) context, segmentOne);
    segmentTwo.init((StateStoreContext) context, segmentTwo);
    segmentOne.put(Bytes.wrap("a".getBytes()), "1".getBytes());
    segmentOne.put(Bytes.wrap("b".getBytes()), "2".getBytes());
    segmentTwo.put(Bytes.wrap("c".getBytes()), "3".getBytes());
    segmentTwo.put(Bytes.wrap("d".getBytes()), "4".getBytes());
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockRecordCollector(org.apache.kafka.test.MockRecordCollector) LogContext(org.apache.kafka.common.utils.LogContext) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) InternalMockProcessorContext(org.apache.kafka.test.InternalMockProcessorContext) Before(org.junit.Before)

Example 2 with MockRecordCollector

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

the class AbstractSessionBytesStoreTest method setUp.

@Before
public void setUp() {
    sessionStore = buildSessionStore(RETENTION_PERIOD, Serdes.String(), Serdes.Long());
    recordCollector = new MockRecordCollector();
    context = new InternalMockProcessorContext<>(TestUtils.tempDirectory(), Serdes.String(), Serdes.Long(), recordCollector, new ThreadCache(new LogContext("testCache"), 0, new MockStreamsMetrics(new Metrics())));
    context.setTime(1L);
    sessionStore.init((StateStoreContext) context, sessionStore);
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockRecordCollector(org.apache.kafka.test.MockRecordCollector) LogContext(org.apache.kafka.common.utils.LogContext) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Before(org.junit.Before)

Example 3 with MockRecordCollector

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

the class TimeOrderedKeyValueBufferTest method shouldFlush.

@Test
public void shouldFlush() {
    final TimeOrderedKeyValueBuffer<String, String> buffer = bufferSupplier.apply(testName);
    final MockInternalProcessorContext context = makeContext();
    buffer.init((StateStoreContext) context, buffer);
    putRecord(buffer, context, 2L, 0L, "asdf", "2093j");
    putRecord(buffer, context, 1L, 1L, "zxcv", "3gon4i");
    putRecord(buffer, context, 0L, 2L, "deleteme", "deadbeef");
    // replace "deleteme" with a tombstone
    buffer.evictWhile(() -> buffer.minTimestamp() < 1, kv -> {
    });
    // flush everything to the changelog
    buffer.flush();
    // the buffer should serialize the buffer time and the value as byte[],
    // which we can't compare for equality using ProducerRecord.
    // As a workaround, I'm deserializing them and shoving them in a KeyValue, just for ease of testing.
    final List<ProducerRecord<String, KeyValue<Long, BufferValue>>> collected = ((MockRecordCollector) context.recordCollector()).collected().stream().map(pr -> {
        final KeyValue<Long, BufferValue> niceValue;
        if (pr.value() == null) {
            niceValue = null;
        } else {
            final byte[] serializedValue = (byte[]) pr.value();
            final ByteBuffer valueBuffer = ByteBuffer.wrap(serializedValue);
            final BufferValue contextualRecord = BufferValue.deserialize(valueBuffer);
            final long timestamp = valueBuffer.getLong();
            niceValue = new KeyValue<>(timestamp, contextualRecord);
        }
        return new ProducerRecord<>(pr.topic(), pr.partition(), pr.timestamp(), pr.key().toString(), niceValue, pr.headers());
    }).collect(Collectors.toList());
    assertThat(collected, is(asList(new ProducerRecord<>(APP_ID + "-" + testName + "-changelog", // Producer will assign
    0, null, "deleteme", null, new RecordHeaders()), new ProducerRecord<>(APP_ID + "-" + testName + "-changelog", 0, null, "zxcv", new KeyValue<>(1L, getBufferValue("3gon4i", 1)), CHANGELOG_HEADERS), new ProducerRecord<>(APP_ID + "-" + testName + "-changelog", 0, null, "asdf", new KeyValue<>(2L, getBufferValue("2093j", 0)), CHANGELOG_HEADERS))));
    cleanup(context, buffer);
}
Also used : StreamsConfig(org.apache.kafka.streams.StreamsConfig) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) TaskId(org.apache.kafka.streams.processor.TaskId) ProcessorRecordContext(org.apache.kafka.streams.processor.internals.ProcessorRecordContext) RunWith(org.junit.runner.RunWith) Random(java.util.Random) Eviction(org.apache.kafka.streams.state.internals.TimeOrderedKeyValueBuffer.Eviction) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) Function(java.util.function.Function) RecordBatchingStateRestoreCallback(org.apache.kafka.streams.processor.internals.RecordBatchingStateRestoreCallback) ByteBuffer(java.nio.ByteBuffer) Collections.singletonList(java.util.Collections.singletonList) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) StateStoreContext(org.apache.kafka.streams.processor.StateStoreContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays.asList(java.util.Arrays.asList) Serdes(org.apache.kafka.common.serialization.Serdes) StringSerializer(org.apache.kafka.common.serialization.StringSerializer) Record(org.apache.kafka.streams.processor.api.Record) Assert.fail(org.junit.Assert.fail) MockRecordCollector(org.apache.kafka.test.MockRecordCollector) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LinkedList(java.util.LinkedList) TimestampType(org.apache.kafka.common.record.TimestampType) Parameterized(org.junit.runners.Parameterized) Utils(org.apache.kafka.common.utils.Utils) Properties(java.util.Properties) TestUtils(org.apache.kafka.test.TestUtils) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Collection(java.util.Collection) KeyValue(org.apache.kafka.streams.KeyValue) CHANGELOG_HEADERS(org.apache.kafka.streams.state.internals.InMemoryTimeOrderedKeyValueBuffer.CHANGELOG_HEADERS) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) List(java.util.List) Header(org.apache.kafka.common.header.Header) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Change(org.apache.kafka.streams.kstream.internals.Change) KeyValue(org.apache.kafka.streams.KeyValue) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) MockInternalProcessorContext(org.apache.kafka.test.MockInternalProcessorContext) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 4 with MockRecordCollector

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

the class AbstractRocksDBSegmentedBytesStoreTest method before.

@Before
public void before() {
    if (schema instanceof SessionKeySchema) {
        windows[0] = new SessionWindow(10L, 10L);
        windows[1] = new SessionWindow(500L, 1000L);
        windows[2] = new SessionWindow(1_000L, 1_500L);
        windows[3] = new SessionWindow(30_000L, 60_000L);
        // All four of the previous windows will go into segment 1.
        // The nextSegmentWindow is computed be a high enough time that when it gets written
        // to the segment store, it will advance stream time past the first segment's retention time and
        // expire it.
        nextSegmentWindow = new SessionWindow(segmentInterval + retention, segmentInterval + retention);
    }
    if (schema instanceof WindowKeySchema) {
        windows[0] = timeWindowForSize(10L, windowSizeForTimeWindow);
        windows[1] = timeWindowForSize(500L, windowSizeForTimeWindow);
        windows[2] = timeWindowForSize(1_000L, windowSizeForTimeWindow);
        windows[3] = timeWindowForSize(60_000L, windowSizeForTimeWindow);
        // All four of the previous windows will go into segment 1.
        // The nextSegmentWindow is computed be a high enough time that when it gets written
        // to the segment store, it will advance stream time past the first segment's retention time and
        // expire it.
        nextSegmentWindow = timeWindowForSize(segmentInterval + retention, windowSizeForTimeWindow);
    }
    bytesStore = getBytesStore();
    stateDir = TestUtils.tempDirectory();
    context = new InternalMockProcessorContext<>(stateDir, Serdes.String(), Serdes.Long(), new MockRecordCollector(), new ThreadCache(new LogContext("testCache "), 0, new MockStreamsMetrics(new Metrics())));
    bytesStore.init((StateStoreContext) context, bytesStore);
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockRecordCollector(org.apache.kafka.test.MockRecordCollector) LogContext(org.apache.kafka.common.utils.LogContext) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) SessionWindow(org.apache.kafka.streams.kstream.internals.SessionWindow) Before(org.junit.Before)

Example 5 with MockRecordCollector

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

the class AbstractWindowBytesStoreTest method setup.

@Before
public void setup() {
    windowStore = buildWindowStore(RETENTION_PERIOD, WINDOW_SIZE, false, Serdes.Integer(), Serdes.String());
    recordCollector = new MockRecordCollector();
    context = new InternalMockProcessorContext<>(baseDir, Serdes.String(), Serdes.Integer(), recordCollector, new ThreadCache(new LogContext("testCache"), 0, new MockStreamsMetrics(new Metrics())));
    context.setTime(1L);
    windowStore.init((StateStoreContext) context, windowStore);
}
Also used : MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) MockRecordCollector(org.apache.kafka.test.MockRecordCollector) LogContext(org.apache.kafka.common.utils.LogContext) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Before(org.junit.Before)

Aggregations

MockRecordCollector (org.apache.kafka.test.MockRecordCollector)10 Metrics (org.apache.kafka.common.metrics.Metrics)7 LogContext (org.apache.kafka.common.utils.LogContext)7 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)7 Before (org.junit.Before)7 Properties (java.util.Properties)2 TaskId (org.apache.kafka.streams.processor.TaskId)2 InternalMockProcessorContext (org.apache.kafka.test.InternalMockProcessorContext)2 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Collections.singletonList (java.util.Collections.singletonList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Optional (java.util.Optional)1 Random (java.util.Random)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Function (java.util.function.Function)1