Search in sources :

Example 1 with CapturedForward

use of org.apache.kafka.streams.processor.api.MockProcessorContext.CapturedForward in project kafka by apache.

the class WindowedWordCountProcessorTest method shouldWorkWithPersistentStore.

@Test
public void shouldWorkWithPersistentStore() throws IOException {
    final File stateDir = TestUtils.tempDirectory();
    try {
        final MockProcessorContext<String, String> context = new MockProcessorContext<>(new Properties(), new TaskId(0, 0), stateDir);
        // Create, initialize, and register the state store.
        final WindowStore<String, Integer> store = Stores.windowStoreBuilder(Stores.persistentWindowStore("WindowedCounts", Duration.ofDays(24), Duration.ofMillis(100), false), Serdes.String(), Serdes.Integer()).withLoggingDisabled().withCachingDisabled().build();
        store.init(context.getStateStoreContext(), store);
        context.getStateStoreContext().register(store, null);
        // Create and initialize the processor under test
        final Processor<String, String, String, String> processor = new WindowedWordCountProcessorSupplier().get();
        processor.init(context);
        // send a record to the processor
        processor.process(new Record<>("key", "alpha beta gamma alpha", 101L));
        // send a record to the processor in a new window
        processor.process(new Record<>("key", "gamma delta", 221L));
        // note that the processor does not forward during process()
        assertThat(context.forwarded().isEmpty(), is(true));
        // now, we trigger the punctuator, which iterates over the state store and forwards the contents.
        context.scheduledPunctuators().get(0).getPunctuator().punctuate(1_000L);
        // finally, we can verify the output.
        final List<CapturedForward<? extends String, ? extends String>> capturedForwards = context.forwarded();
        final List<CapturedForward<? extends String, ? extends String>> expected = asList(new CapturedForward<>(new Record<>("[alpha@100/200]", "2", 1_000L)), new CapturedForward<>(new Record<>("[beta@100/200]", "1", 1_000L)), new CapturedForward<>(new Record<>("[delta@200/300]", "1", 1_000L)), new CapturedForward<>(new Record<>("[gamma@100/200]", "1", 1_000L)), new CapturedForward<>(new Record<>("[gamma@200/300]", "1", 1_000L)));
        assertThat(capturedForwards, is(expected));
        store.close();
    } finally {
        Utils.delete(stateDir);
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Properties(java.util.Properties) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) CapturedForward(org.apache.kafka.streams.processor.api.MockProcessorContext.CapturedForward) Record(org.apache.kafka.streams.processor.api.Record) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 2 with CapturedForward

use of org.apache.kafka.streams.processor.api.MockProcessorContext.CapturedForward in project kafka by apache.

the class MockProcessorContextAPITest method shouldCaptureApplicationAndRecordMetadata.

@Test
public void shouldCaptureApplicationAndRecordMetadata() {
    final Properties config = mkProperties(mkMap(mkEntry(StreamsConfig.APPLICATION_ID_CONFIG, "testMetadata"), mkEntry(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "")));
    final Processor<String, Object, String, Object> processor = new Processor<String, Object, String, Object>() {

        private ProcessorContext<String, Object> context;

        @Override
        public void init(final ProcessorContext<String, Object> context) {
            this.context = context;
        }

        @Override
        public void process(final Record<String, Object> record) {
            context.forward(new Record<String, Object>("appId", context.applicationId(), 0L));
            context.forward(new Record<String, Object>("taskId", context.taskId(), 0L));
            if (context.recordMetadata().isPresent()) {
                final RecordMetadata recordMetadata = context.recordMetadata().get();
                context.forward(new Record<String, Object>("topic", recordMetadata.topic(), 0L));
                context.forward(new Record<String, Object>("partition", recordMetadata.partition(), 0L));
                context.forward(new Record<String, Object>("offset", recordMetadata.offset(), 0L));
            }
            context.forward(new Record<String, Object>("record", record, 0L));
        }
    };
    final MockProcessorContext<String, Object> context = new MockProcessorContext<>(config);
    processor.init(context);
    processor.process(new Record<>("foo", 5L, 0L));
    {
        final List<CapturedForward<? extends String, ?>> forwarded = context.forwarded();
        final List<CapturedForward<? extends String, ?>> expected = asList(new CapturedForward<>(new Record<>("appId", "testMetadata", 0L)), new CapturedForward<>(new Record<>("taskId", new TaskId(0, 0), 0L)), new CapturedForward<>(new Record<>("record", new Record<>("foo", 5L, 0L), 0L)));
        assertThat(forwarded, is(expected));
    }
    context.resetForwards();
    context.setRecordMetadata("t1", 0, 0L);
    processor.process(new Record<>("foo", 5L, 0L));
    {
        final List<CapturedForward<? extends String, ?>> forwarded = context.forwarded();
        final List<CapturedForward<? extends String, ?>> expected = asList(new CapturedForward<>(new Record<>("appId", "testMetadata", 0L)), new CapturedForward<>(new Record<>("taskId", new TaskId(0, 0), 0L)), new CapturedForward<>(new Record<>("topic", "t1", 0L)), new CapturedForward<>(new Record<>("partition", 0, 0L)), new CapturedForward<>(new Record<>("offset", 0L, 0L)), new CapturedForward<>(new Record<>("record", new Record<>("foo", 5L, 0L), 0L)));
        assertThat(forwarded, is(expected));
    }
}
Also used : Processor(org.apache.kafka.streams.processor.api.Processor) TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkProperties(org.apache.kafka.common.utils.Utils.mkProperties) Properties(java.util.Properties) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) ProcessorContext(org.apache.kafka.streams.processor.api.ProcessorContext) RecordMetadata(org.apache.kafka.streams.processor.api.RecordMetadata) CapturedForward(org.apache.kafka.streams.processor.api.MockProcessorContext.CapturedForward) Record(org.apache.kafka.streams.processor.api.Record) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

Properties (java.util.Properties)2 TaskId (org.apache.kafka.streams.processor.TaskId)2 MockProcessorContext (org.apache.kafka.streams.processor.api.MockProcessorContext)2 CapturedForward (org.apache.kafka.streams.processor.api.MockProcessorContext.CapturedForward)2 Record (org.apache.kafka.streams.processor.api.Record)2 Test (org.junit.jupiter.api.Test)2 File (java.io.File)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Utils.mkProperties (org.apache.kafka.common.utils.Utils.mkProperties)1 Processor (org.apache.kafka.streams.processor.api.Processor)1 ProcessorContext (org.apache.kafka.streams.processor.api.ProcessorContext)1 RecordMetadata (org.apache.kafka.streams.processor.api.RecordMetadata)1