Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Status

Current state: Draft Merged (target: 1.2: https://github.com/apache/kafka/pull/4736)

Discussion thread[DISCUSS] KIP-267: Add Processor Unit Test Support to Kafka Streams Test Utils

JIRA

Jira
serverASF JIRA
columnskey,summary,type,created,updated,due,assignee,reporter,priority,status,resolution
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyKAFKA-6473

Released: target version 1.2.0

Please keep the discussion on the mailing list rather than commenting on the wiki (wiki discussions get unwieldy fast).

...

Code Block
languagejava
package org.apache.kafka.streams.processor;

public class MockProcessorContext extends ProcessorContext {
    public MockProcessorContext(final String applicationId, final TaskId taskId, final Properties config);
    public MockProcessorContext(final String applicationId, final TaskId taskId, final Properties config, final File stateDir);
 
    @Override StreamsMetrics metrics(); // return a StreamsMetrics instance for the processor and test code to use

    // high level metadata inherited from ProcessorContext (set in constructor) ===
 
    @Override String applicationId();
    @Override TaskId taskId();
    @Override Map<String, Object> appConfigs();
    @Override Map<String, Object> appConfigsWithPrefix(String prefix);
    @Override Serde<?> keySerde();
    @Override Serde<?> valueSerde();
    @Override File stateDir();

    // record metadata ============================================================

    // setters provided for test code to use
    void setRecordMetadata(String topic, int partition, long offset, long timestamp);
    void setRecordMetadataTopic(String topic);
    void setRecordMetadataPartition(int partition);
    void setRecordMetadataOffset(long offset);
    void setRecordMetadataTimestamp(long timestamp);

    // getters inherited from ProcessorContext
    @Override String topic();
    @Override int partition();
    @Override long offset();
    @Override long timestamp();

    // mocked methods =============================================================
 
    // StateStore setter and getter
    @Override void register(StateStore store, boolean loggingEnabledIsDeprecatedAndIgnored, StateRestoreCallback stateRestoreCallback);
    @Override StateStore getStateStore(String name);

 
    // Punctuator capture: NOTE: punctuators will not be triggered automatically. Instead, they'll be captured so test code can retrieve and trigger them if desired.
    @Override Cancellable schedule(long intervalMs, PunctuationType type, Punctuator callback);
    @Override void schedule(long interval); // throw UnsupportedOperationException
    List<CapturedPunctuator> scheduledPunctuators();

    // captures forward() data
    @Override <K, V> void forward(K key, V value);
    @Override <K, V> void forward(K key, V value, int childIndex);
    @Override <K, V> void forward(K key, V value, String childName);

    // returns captured forward data
    <K, V> List<KeyValue<K, V>> forwarded();
    <K, V> List<KeyValue<K, V>> forwarded(int childIndex);
    <K, V> List<KeyValue<K, V>> forwarded(String childName);

    // clears captured forward data
    void resetForwards();

    // captures whether commit() gets called
    @Override void commit();

    // true iff commit() has been called
    boolean committed();

    // resets whether commit() has been called
    void resetCommits();

	// structure for capturing punctuators in to schedule()
    public static class CapturedPunctuator {
        public CapturedPunctuator(final long intervalMs, final PunctuationType punctuationType, final Punctuator punctuator) {}

        public long getIntervalMs();
        public PunctuationType getPunctuationType();
        public Punctuator getPunctuator();
    }
}

...

  • void schedule(long interval): it's not possible to implement it without providing a handle on the processor to the context. This is a little tricky, since the mocked ProcessorContext may be used to test not just a Processor, but also a Transformer or a ValueTransformer. Since this method is deprecated in the interface, we feel it's reasonable to update client code to use Punctuators instead.

...

  •  This method will throw an UnsupportedOperationException.

Also note that we won't automatically trigger sheduled punctuators. Instead, we'll just capture them so test code can retrieve and trigger them if desired. This keeps the mock truly a "mock" and not a "driver", which maintains the simplicity of the code and enables users to write linear and sensible tests. If folks want to have their punctuators triggered automatically, they can use the TopologyTestDriver, which does that. This is a potential sharp edge, so I'm planning to make sure it's well documented in both the javadoc and html docs.

 

Example Usage

This mocked ProcessorContext would enable unit tests like the following:

...