尽管测试通过了,但在KafkaTest期间仍获得异常

时间:2019-10-04 10:26:31

标签: spring-boot apache-kafka spring-kafka spring-kafka-test

我正在使用EmbeddedKafka运行Junit测试,我能够生产和消费EmbeddedKafka代理,并成功断言我已发送的数据。

但是我可以在断言完成之后的堆栈跟踪中看到很多异常。

1)

java.io.IOException:在读取响应之前已断开与0的连接     在org.apache.kafka.clients.NetworkClientUtils.sendAndReceive(NetworkClientUtils.java:97)〜[kafka-clients-2.0.1.jar:na]     在kafka.controller.RequestSendThread.doWork(ControllerChannelManager.scala:240)〜[kafka_2.11-2.0.1.jar:na]     在kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:82)[kafka_2.11-2.0.1.jar:na]

2)

2019-10-04 15:49:27.123警告1812 --- [r-0-发送线程] kafka.controller.RequestSendThread:[RequestSendThread controllerId = 0]控制器0与代理localhost:54745的连接(id: 0机架:空)未成功

java.net.SocketTimeoutException:在1000毫秒内无法连接     在kafka.controller.RequestSendThread.brokerReady(ControllerChannelManager.scala:280)[kafka_2.11-2.0.1.jar:na]     在kafka.controller.RequestSendThread.doWork(ControllerChannelManager.scala:233)[kafka_2.11-2.0.1.jar:na]     在kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:82)[kafka_2.11-2.0.1.jar:na]

3)

java.io.IOException:连接到本地主机:54745(标识:0机架:空)失败。     在org.apache.kafka.clients.NetworkClientUtils.awaitReady(NetworkClientUtils.java:70)〜[kafka-clients-2.0.1.jar:na]     在kafka.controller.RequestSendThread.brokerReady(ControllerChannelManager.scala:279)[kafka_2.11-2.0.1.jar:na]     在kafka.controller.RequestSendThread.doWork(ControllerChannelManager.scala:233)[kafka_2.11-2.0.1.jar:na]     在kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:82)[kafka_2.11-2.0.1.jar:na]

4)

java.lang.InterruptedException:空     在java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(未知来源)〜[na:1.8.0_181]     在java.util.concurrent.CountDownLatch.await(未知来源)〜[na:1.8.0_181]     在kafka.utils.ShutdownableThread.pause(ShutdownableThread.scala:69)[kafka_2.11-2.0.1.jar:na]     在kafka.controller.RequestSendThread.backoff $ 1(ControllerChannelManager.scala:221)〜[kafka_2.11-2.0.1.jar:na]     在kafka.controller.RequestSendThread.doWork(ControllerChannelManager.scala:235)〜[kafka_2.11-2.0.1.jar:na]     在kafka.utils.ShutdownableThread.run(ShutdownableThread.scala:82)[kafka_2.11-2.0.1.jar:na]

我的测试:

 import org.apache.kafka.clients.consumer.Consumer;
    import org.apache.kafka.clients.consumer.ConsumerConfig;
    import org.apache.kafka.clients.consumer.ConsumerRecord;
    import org.apache.kafka.common.TopicPartition;
    import org.apache.kafka.common.header.Header;
    import org.apache.kafka.common.header.Headers;
    import org.apache.kafka.common.serialization.StringDeserializer;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
    import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
    import org.springframework.kafka.support.serializer.JsonDeserializer;
    import org.springframework.kafka.test.context.EmbeddedKafka;
    import org.springframework.kafka.test.rule.EmbeddedKafkaRule;

    @RunWith(SpringRunner.class)
    @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, classes = TestApplication.class)
    @ActiveProfiles("test")
    @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
    //@EmbeddedKafka(controlledShutdown = true)
    public class KafkaUtilTest {

        private static String TOPIC = "PE";

        @Autowired
        KafkaUtil kafkaUtil;

        @Autowired
        ConfigurationProperties configProperties;

        SchedulingCallRequestDTO request;
        ScheduleOrderResponseDTOv2 response;
        Consumer<String, Object> consumer;
        HashMap<String, String> expectedHeaderValueMap;

        @ClassRule
        public static EmbeddedKafkaRule embeddedKafkarule = new EmbeddedKafkaRule(1, true, TOPIC);

        @BeforeClass
        public static void setUpBeforeClass() throws Exception {
            System.setProperty("spring.kafka.producer.bootstrap-servers",
                    embeddedKafkarule.getEmbeddedKafka().getBrokersAsString());
        }

        @AfterClass
        public static void tearDown() {
            embeddedKafkarule.getEmbeddedKafka().getKafkaServers().forEach(b -> b.shutdown());
            embeddedKafkarule.getEmbeddedKafka().getKafkaServers().forEach(b -> b.awaitShutdown());
        }

        @Before
        public void init() {
            readFile("0211");
            expectedHeaderValueMap = getExpectedHeaderValueMap();
            Map<String, Object> consumerConfigs = new HashMap<>(
                    KafkaTestUtils.consumerProps("consumer", "true", embeddedKafkarule.getEmbeddedKafka()));
            consumerConfigs.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
            consumerConfigs.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);
            consumerConfigs.put(JsonDeserializer.TRUSTED_PACKAGES, "com.adessa.promiseengine.dto.kafka");

            consumer = new DefaultKafkaConsumerFactory<String, Object>(consumerConfigs).createConsumer();

            List<String> topics = new ArrayList<>();
            topics.add(TOPIC);

            TopicPartition topicPartition1 = new TopicPartition(TOPIC, 0);
            TopicPartition topicPartition2 = new TopicPartition(TOPIC, 1);
            List<TopicPartition> topicPartitions = new ArrayList<TopicPartition>();
            topicPartitions.add(topicPartition1);
            topicPartitions.add(topicPartition2);
            consumer.assign(topicPartitions);
            consumer.seekToBeginning(topicPartitions);

        }

        @Test
        public void testPublish() throws Exception {
            kafkaUtil.publishToKafka(request, response);
            kafkaUtil.kafkaTemplate.flush();
            ConsumerRecord<String, Object> consumedRecord = KafkaTestUtils.getSingleRecord(consumer, TOPIC);
            assertRecord(consumedRecord);
        }
        private void readFile(String testSequenceNo) {}

    }

为什么会有这些例外?在什么过程中它们正在发生?请帮助

1 个答案:

答案 0 :(得分:1)

您正在使用类规则,并且在测试完成时经纪人被拆除,但是您让使用者保持打开状态;您需要在测试结束时关闭使用者。