具有WindowStore单元测试ClassCastException的Kafka流处理器

时间:2019-06-26 01:38:16

标签: java unit-testing apache-kafka apache-kafka-streams classcastexception

我试图为实现包含xsl:sequence的{​​{1}}接口的类编写单元测试,但是运行测试失败,并且replace()中出现Processor }尝试将WindowStore强制转换为ClassCastException

搜索该问题并没有发现任何有用的信息,也不表示这是已知问题。我正在尝试遵循the documentation中的指导,但也许我仍然缺少一些东西。

我创建了一个带有最小示例的存储库,以便在有人想直接引用它的情况下在Github上重现该问题。

但是否则,处理器类为:

InMemoryWindowStore.init

测试类为:

MockProcessorContext

我将maven作为构建工具InternalProcessorContext输出:

package com.cantgetthistowork;

import org.apache.kafka.streams.processor.Processor;
import org.apache.kafka.streams.processor.ProcessorContext;
import org.apache.kafka.streams.state.WindowStore;

public class InMemWindowProcessor implements Processor<String, String> {

  private ProcessorContext context;
  private WindowStore<String, String> windowStore;

  @Override
  public void init(ProcessorContext context) {
    this.context = context;

    windowStore = (WindowStore<String, String>) context.getStateStore("my-win-store");
  }

  @Override
  public void process(String key, String value) {
  }

  @Override
  public void close() {
  }

}

最后,package com.cantgetthistowork; import java.time.Duration; import java.time.Instant; import org.apache.kafka.common.serialization.Serdes; import org.apache.kafka.streams.processor.MockProcessorContext; import org.apache.kafka.streams.state.Stores; import org.apache.kafka.streams.state.WindowStore; import org.junit.Before; import org.junit.Test; public class InMemWindowProcessorTest { InMemWindowProcessor processor = null; MockProcessorContext context = null; @Before public void setup() { processor = new InMemWindowProcessor(); context = new MockProcessorContext(); WindowStore<String, String> store = Stores.windowStoreBuilder( Stores.inMemoryWindowStore( "my-win-store", Duration.ofMinutes(10), Duration.ofSeconds(10), false ), Serdes.String(), Serdes.String() ) .withLoggingDisabled() .build(); store.init(context, store); context.register(store, null); processor.init(context); } @Test public void testThings() { Instant baseTime = Instant.now(); context.setTimestamp(baseTime.toEpochMilli()); context.setTopic("topic-name"); processor.process("key1", "value1"); } } 输出:

mvn --version

是的,也许我只是缺少一些配置,或者做一些乱序的事情,或者其他愚蠢的事情。同时,我将编写使用Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-03T13:39:06-06:00) Maven home: ~/opt/apache-maven-3.5.0 Java version: 1.8.0_212, vendor: Oracle Corporation Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "4.15.0-52-generic", arch: "amd64", family: "unix" 的测试,并希望能奏效。

我期待人们可能会有任何回应/想法。

0 个答案:

没有答案