简体   繁体   中英

spring-cloud-contract-verifier cleanup messages in camel kafka topics

During contract test I run main FLOW , which produces 2 events into different kafka topics ( TOPIC_1 and TOPIC_2 ). And I have two different tests to check sending of this events ( TEST_1 for TOPIC_1 and TEST_2 for TOPIC_2 ). So both TEST_1 and TEST_2 runs the same FLOW , and for TEST_1 I have side effect of sending event into TOPIC_2 , and for TEST_2 - TOPIC_1 . Consider example, where I run TEST_1 and then TEST_2 . During TEST_2 I will have 2 events inside TOPIC_2 - one produced by TEST_1 and the second produced by TEST_2 . And of course, my TEST_2 will fail, because during the verification it supposes to receive message, produced by himself, nothing else.

So, that's why I need to skip all old messages in all topics before each test. How does it possible to do with usage of org.springframework.cloud.contract.verifier.messaging.internal.ContractVerifierMessaging

I found out a solution where I poll all messages from each endpoint inside CamelContext

@Autowired
private org.apache.camel.CamelContext camelContext;

@org.junit.Before
public void cleanUpCamelEndpoints() {
    for (Endpoint endpoint : camelContext.getEndpoints()) {
        try {
            PollingConsumer pollingConsumer = endpoint.createPollingConsumer();
            Exchange exchangeToSkip;
            while ((exchangeToSkip = pollingConsumer.receiveNoWait()) != null) {
                log.debug("Skipped side effect exchange: {}", exchangeToSkip);
            }
        } catch (Exception exception) {
            log.debug("Exception while receive exchange to skip: " + exception);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM