Search in sources :

Example 6 with MockProcessorContext

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

the class WordCountTransformerTest method test.

@Test
public void test() {
    final MockProcessorContext<String, String> context = new MockProcessorContext<>();
    // Create and initialize the transformer under test; including its provided store
    final WordCountTransformerDemo.MyTransformerSupplier supplier = new WordCountTransformerDemo.MyTransformerSupplier();
    for (final StoreBuilder<?> storeBuilder : supplier.stores()) {
        final StateStore store = storeBuilder.withLoggingDisabled().build();
        store.init(context.getStateStoreContext(), store);
        context.getStateStoreContext().register(store, null);
    }
    final Transformer<String, String, KeyValue<String, String>> transformer = supplier.get();
    transformer.init(new org.apache.kafka.streams.processor.MockProcessorContext() {

        @Override
        public <S extends StateStore> S getStateStore(final String name) {
            return context.getStateStore(name);
        }

        @Override
        public <K, V> void forward(final K key, final V value) {
            context.forward(new Record<>((String) key, (String) value, 0L));
        }

        @Override
        public Cancellable schedule(final Duration interval, final PunctuationType type, final Punctuator callback) {
            return context.schedule(interval, type, callback);
        }
    });
    // send a record to the transformer
    transformer.transform("key", "alpha beta\tgamma\n\talpha");
    // note that the transformer does not forward during transform()
    assertTrue(context.forwarded().isEmpty());
    // now, we trigger the punctuator, which iterates over the state store and forwards the contents.
    context.scheduledPunctuators().get(0).getPunctuator().punctuate(0L);
    // finally, we can verify the output.
    final List<MockProcessorContext.CapturedForward<? extends String, ? extends String>> capturedForwards = context.forwarded();
    final List<MockProcessorContext.CapturedForward<? extends String, ? extends String>> expected = asList(new MockProcessorContext.CapturedForward<>(new Record<>("alpha", "2", 0L)), new MockProcessorContext.CapturedForward<>(new Record<>("beta", "1", 0L)), new MockProcessorContext.CapturedForward<>(new Record<>("gamma", "1", 0L)));
    assertThat(capturedForwards, is(expected));
}
Also used : KeyValue(org.apache.kafka.streams.KeyValue) Cancellable(org.apache.kafka.streams.processor.Cancellable) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) Record(org.apache.kafka.streams.processor.api.Record) Punctuator(org.apache.kafka.streams.processor.Punctuator) StateStore(org.apache.kafka.streams.processor.StateStore) Duration(java.time.Duration) PunctuationType(org.apache.kafka.streams.processor.PunctuationType) Test(org.junit.jupiter.api.Test)

Example 7 with MockProcessorContext

use of org.apache.kafka.streams.processor.api.MockProcessorContext 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)

Example 8 with MockProcessorContext

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

the class MockProcessorContextAPITest method shouldCapturePunctuator.

@Test
public void shouldCapturePunctuator() {
    final Processor<String, Long, Void, Void> processor = new Processor<String, Long, Void, Void>() {

        @Override
        public void init(final ProcessorContext<Void, Void> context) {
            context.schedule(Duration.ofSeconds(1L), PunctuationType.WALL_CLOCK_TIME, timestamp -> context.commit());
        }

        @Override
        public void process(final Record<String, Long> record) {
        }
    };
    final MockProcessorContext<Void, Void> context = new MockProcessorContext<>();
    processor.init(context);
    final MockProcessorContext.CapturedPunctuator capturedPunctuator = context.scheduledPunctuators().get(0);
    assertThat(capturedPunctuator.getInterval(), is(Duration.ofMillis(1000L)));
    assertThat(capturedPunctuator.getType(), is(PunctuationType.WALL_CLOCK_TIME));
    assertThat(capturedPunctuator.cancelled(), is(false));
    final Punctuator punctuator = capturedPunctuator.getPunctuator();
    assertThat(context.committed(), is(false));
    punctuator.punctuate(1234L);
    assertThat(context.committed(), is(true));
}
Also used : Processor(org.apache.kafka.streams.processor.api.Processor) Punctuator(org.apache.kafka.streams.processor.Punctuator) Record(org.apache.kafka.streams.processor.api.Record) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) ProcessorContext(org.apache.kafka.streams.processor.api.ProcessorContext) Test(org.junit.jupiter.api.Test)

Example 9 with MockProcessorContext

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

the class MockProcessorContextAPITest method fullConstructorShouldSetAllExpectedAttributes.

@Test
public void fullConstructorShouldSetAllExpectedAttributes() {
    final Properties config = new Properties();
    config.put(StreamsConfig.APPLICATION_ID_CONFIG, "testFullConstructor");
    config.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "");
    config.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    config.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.Long().getClass());
    final File dummyFile = new File("");
    final MockProcessorContext<Void, Void> context = new MockProcessorContext<>(config, new TaskId(1, 1), dummyFile);
    assertThat(context.applicationId(), is("testFullConstructor"));
    assertThat(context.taskId(), is(new TaskId(1, 1)));
    assertThat(context.appConfigs().get(StreamsConfig.APPLICATION_ID_CONFIG), is("testFullConstructor"));
    assertThat(context.appConfigsWithPrefix("application.").get("id"), is("testFullConstructor"));
    assertThat(context.keySerde().getClass(), is(Serdes.String().getClass()));
    assertThat(context.valueSerde().getClass(), is(Serdes.Long().getClass()));
    assertThat(context.stateDir(), is(dummyFile));
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) Utils.mkProperties(org.apache.kafka.common.utils.Utils.mkProperties) Properties(java.util.Properties) File(java.io.File) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) Test(org.junit.jupiter.api.Test)

Example 10 with MockProcessorContext

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

the class MockProcessorContextStateStoreTest method shouldEitherInitOrThrow.

@ParameterizedTest(name = "builder = {0}, timestamped = {1}, caching = {2}, logging = {3}")
@MethodSource(value = "parameters")
public void shouldEitherInitOrThrow(final StoreBuilder<StateStore> builder, final boolean timestamped, final boolean caching, final boolean logging) {
    final File stateDir = TestUtils.tempDirectory();
    try {
        final MockProcessorContext<Void, Void> context = new MockProcessorContext<>(mkProperties(mkMap(mkEntry(StreamsConfig.APPLICATION_ID_CONFIG, ""), mkEntry(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, ""))), new TaskId(0, 0), stateDir);
        final StateStore store = builder.build();
        if (caching || logging) {
            assertThrows(IllegalArgumentException.class, () -> store.init(context.getStateStoreContext(), store));
        } else {
            store.init(context.getStateStoreContext(), store);
            store.close();
        }
    } finally {
        try {
            Utils.delete(stateDir);
        } catch (final IOException e) {
        // Failed to clean up the state dir. The JVM hooks will try again later.
        }
    }
}
Also used : TaskId(org.apache.kafka.streams.processor.TaskId) StateStore(org.apache.kafka.streams.processor.StateStore) IOException(java.io.IOException) File(java.io.File) MockProcessorContext(org.apache.kafka.streams.processor.api.MockProcessorContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

MockProcessorContext (org.apache.kafka.streams.processor.api.MockProcessorContext)10 Record (org.apache.kafka.streams.processor.api.Record)5 Test (org.junit.jupiter.api.Test)5 Properties (java.util.Properties)4 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)4 TaskId (org.apache.kafka.streams.processor.TaskId)4 LogCaptureAppender (org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender)4 Test (org.junit.Test)4 File (java.io.File)3 Processor (org.apache.kafka.streams.processor.api.Processor)3 Utils.mkProperties (org.apache.kafka.common.utils.Utils.mkProperties)2 Punctuator (org.apache.kafka.streams.processor.Punctuator)2 StateStore (org.apache.kafka.streams.processor.StateStore)2 CapturedForward (org.apache.kafka.streams.processor.api.MockProcessorContext.CapturedForward)2 ProcessorContext (org.apache.kafka.streams.processor.api.ProcessorContext)2 IOException (java.io.IOException)1 Duration (java.time.Duration)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1