11/*
2- * Copyright 2016-2018 the original author or authors.
2+ * Copyright 2016-2019 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
@@ -1961,22 +1961,26 @@ public void testInitialSeek() throws Exception {
19611961 @ Test
19621962 public void testExceptionWhenCommitAfterRebalance () throws Exception {
19631963 final CountDownLatch rebalanceLatch = new CountDownLatch (2 );
1964- final CountDownLatch consumeLatch = new CountDownLatch (7 );
1964+ final CountDownLatch consumeFirstLatch = new CountDownLatch (1 );
1965+ final CountDownLatch consumeLatch = new CountDownLatch (2 );
19651966
19661967 Map <String , Object > props = KafkaTestUtils .consumerProps ("test19" , "false" , embeddedKafka );
19671968 props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "latest" );
1968- props .put (ConsumerConfig .MAX_POLL_INTERVAL_MS_CONFIG , 15000 );
1969+ props .put (ConsumerConfig .MAX_POLL_INTERVAL_MS_CONFIG , 3_000 );
19691970 DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
19701971 ContainerProperties containerProps = new ContainerProperties (topic19 );
1971- containerProps .setMessageListener ((MessageListener <Integer , String >) messages -> {
1972- logger .info ("listener: " + messages );
1973- consumeLatch .countDown ();
1974- try {
1975- Thread .sleep (3000 );
1976- }
1977- catch (InterruptedException e ) {
1978- Thread .currentThread ().interrupt ();
1972+ containerProps .setMessageListener ((MessageListener <Integer , String >) message -> {
1973+ logger .warn ("listener: " + message );
1974+ consumeFirstLatch .countDown ();
1975+ if (consumeLatch .getCount () > 1 ) {
1976+ try {
1977+ Thread .sleep (5_000 );
1978+ }
1979+ catch (InterruptedException e1 ) {
1980+ Thread .currentThread ().interrupt ();
1981+ }
19791982 }
1983+ consumeLatch .countDown ();
19801984 });
19811985 containerProps .setSyncCommits (true );
19821986 containerProps .setAckMode (AckMode .BATCH );
@@ -1996,7 +2000,7 @@ public void onPartitionsRevoked(Collection<TopicPartition> partitions) {
19962000
19972001 @ Override
19982002 public void onPartitionsAssigned (Collection <TopicPartition > partitions ) {
1999- logger .info ("rebalance occurred." );
2003+ logger .warn ("rebalance occurred." );
20002004 rebalanceLatch .countDown ();
20012005 }
20022006 });
@@ -2007,16 +2011,19 @@ public void onPartitionsAssigned(Collection<TopicPartition> partitions) {
20072011 container .setErrorHandler (new SeekToCurrentErrorHandler ());
20082012 container .start ();
20092013 ContainerTestUtils .waitForAssignment (container , embeddedKafka .getPartitionsPerTopic ());
2010- container .pause ();
2011-
2012- for (int i = 0 ; i < 6 ; i ++) {
2014+ template .sendDefault (0 , 0 , "a" );
2015+ assertThat (consumeFirstLatch .await (60 , TimeUnit .SECONDS )).isTrue ();
2016+ // should be rebalanced and consume again
2017+ boolean rebalancedForTooLongBetweenPolls = rebalanceLatch .await (60 , TimeUnit .SECONDS );
2018+ int n = 0 ;
2019+ while (!rebalancedForTooLongBetweenPolls & n ++ < 3 ) {
2020+ // try a few times in case the rebalance was delayed
20132021 template .sendDefault (0 , 0 , "a" );
2022+ rebalancedForTooLongBetweenPolls = rebalanceLatch .await (60 , TimeUnit .SECONDS );
2023+ }
2024+ if (!rebalancedForTooLongBetweenPolls ) {
2025+ logger .error ("Rebalance did not occur - perhaps the CI server is too busy, don't fail the test" );
20142026 }
2015- template .flush ();
2016-
2017- container .resume ();
2018- // should be rebalanced and consume again
2019- assertThat (rebalanceLatch .await (60 , TimeUnit .SECONDS )).isTrue ();
20202027 assertThat (consumeLatch .await (60 , TimeUnit .SECONDS )).isTrue ();
20212028 container .stop ();
20222029 }
0 commit comments