我正在尝试为Spring Kafka应用程序(Spring Boot 2.0.6,Spring Kafka 2.1.10)编写集成测试,并看到大量INFO org.apache.zookeeper.server.PrepRequestProcessor - Got user-level KeeperException when processing sessionid:0x166e432ebec0001 type:create cxid:0x5e zxid:0x24 txntype:-1 reqpath:n/a Error Path:/brokers/topics/my-topic/partitions Error:KeeperErrorCode = NoNode for /brokers/topics/my-topic/partitions
实例和各种风格的路径({{ 1}},/brokers
等)在Spring应用启动之前在日志中显示。然后,AdminClient关闭,并记录以下消息:
/brokers/topics
我在测试中使用@ClassRule启动选项,如下所示:
DEBUG org.apache.kafka.common.network.Selector - [SocketServer brokerId=0] Connection with /127.0.0.1 disconnected
java.io.EOFException: null
at org.apache.kafka.common.network.NetworkReceive.readFromReadableChannel(NetworkReceive.java:124)
at org.apache.kafka.common.network.NetworkReceive.readFrom(NetworkReceive.java:93)
at org.apache.kafka.common.network.KafkaChannel.receive(KafkaChannel.java:235)
at org.apache.kafka.common.network.KafkaChannel.read(KafkaChannel.java:196)
at org.apache.kafka.common.network.Selector.attemptRead(Selector.java:547)
at org.apache.kafka.common.network.Selector.pollSelectionKeys(Selector.java:483)
at org.apache.kafka.common.network.Selector.poll(Selector.java:412)
at kafka.network.Processor.poll(SocketServer.scala:575)
at kafka.network.Processor.run(SocketServer.scala:492)
at java.lang.Thread.run(Thread.java:748)
,自动装配@ClassRule
@Shared
private KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, true, 'my-topic')
,并根据嵌入的Kafka值设置连接的Spring属性:
KafkaTemplate
一旦Spring应用启动,我就可以再次看到用户级KeeperException消息的实例:def setupSpec() {
System.setProperty('spring.kafka.bootstrap-servers', embeddedKafka.getBrokersAsString());
System.setProperty('spring.cloud.stream.kafka.binder.zkNodes', embeddedKafka.getZookeeperConnectionString());
}
。
知道我在哪里错了吗?我可以提供其他设置信息和日志消息,但只是对最初可能最有用的方法进行了有根据的猜测。
答案 0 :(得分:1)
我对Spock并不熟悉,但是我知道@KafkaListener
方法是在其自己的线程上调用的,因此不能仅仅在then:
块中直接声明它。
您需要确保测试用例中的阻塞等待。
我尝试将BlockingVariable
应用于真实服务而不是模拟服务,并且在日志中看到您的println(message)
。但是,BlockingVariable
仍然对我不起作用:
@DirtiesContext
@SpringBootTest(classes = [KafkaIntTestApplication.class])
@ActiveProfiles('test')
class CustomListenerSpec extends Specification {
@ClassRule
@Shared
public KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, false, 'my-topic')
@Autowired
private KafkaTemplate<String, String> template
@SpyBean
private SimpleService service
final def TOPIC_NAME = 'my-topic'
def setupSpec() {
System.setProperty('spring.kafka.bootstrapServers', embeddedKafka.getBrokersAsString());
}
def 'Sample test'() {
given:
def testMessagePayload = "Test message"
def message = MessageBuilder.withPayload(testMessagePayload).setHeader(KafkaHeaders.TOPIC, TOPIC_NAME).build()
def result = new BlockingVariable<Boolean>(5)
service.handleMessage(_) >> {
result.set(true)
}
when: 'We put a message on the topic'
template.send(message)
then: 'the service should be called'
result.get()
}
}
日志如下:
2018-11-05 13:38:51.089 INFO 8888 --- [ntainer#0-0-C-1] o.s.k.l.KafkaMessageListenerContainer : partitions assigned: [my-topic-0, my-topic-1]
Test message
BlockingVariable.get() timed out after 5,00 seconds
at spock.util.concurrent.BlockingVariable.get(BlockingVariable.java:113)
at com.example.CustomListenerSpec.Sample test(CustomListenerSpec.groovy:54)
2018-11-05 13:38:55.917 INFO 8888 --- [ main] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@11ebb1b6: startup date [Mon Nov 05 13:38:49 EST 2018]; root of context hierarchy
我还必须添加此依赖项:
testImplementation "org.hamcrest:hamcrest-core"
更新
好。真正的问题是,MockConfig
对于测试上下文配置不可见,而@Import(MockConfig.class)
可以解决问题。 @Primary
还在给我们额外的信号,是在测试类中要注入哪种bean。
答案 1 :(得分:0)
@ArtemBilan的回复使我走上了正确的道路,因此感谢他的参与,在查看其他BlockingVariable
文章和示例后,我能够弄清楚这一点。我在模拟的响应中使用了BlockingVariable
而不是将其用作回调。调用模拟的响应时,将其设置为true,然后then
块执行result.get()
并通过测试。
@DirtiesContext
@ActiveProfiles('test')
@SpringBootTest
@Import(MockConfig.class)
class CustomListenerSpec extends TestSpecBase {
@ClassRule
@Shared
private KafkaEmbedded embeddedKafka = new KafkaEmbedded(1, false, TOPIC_NAME)
@Autowired
private KafkaTemplate<String, String> template
@Autowired
private SimpleService service
final def TOPIC_NAME = 'my-topic'
def setupSpec() {
System.setProperty('spring.kafka.bootstrap-servers', embeddedKafka.getBrokersAsString());
}
def 'Sample test'() {
def testMessagePayload = "Test message"
def message = MessageBuilder.withPayload(testMessagePayload).setHeader(KafkaHeaders.TOPIC, TOPIC_NAME).build()
def result = new BlockingVariable<Boolean>(5)
service.handleMessage(_ as String) >> {
result.set(true)
}
when: 'We put a message on the topic'
template.send(message)
then: 'the service should be called'
result.get()
}
}