@@ -2054,8 +2054,14 @@ public void testPauseResume() throws Exception {
2054
2054
ConsumerRecords <Integer , String > consumerRecords = new ConsumerRecords <>(records );
2055
2055
ConsumerRecords <Integer , String > emptyRecords = new ConsumerRecords <>(Collections .emptyMap ());
2056
2056
AtomicBoolean first = new AtomicBoolean (true );
2057
+ AtomicBoolean rebalance = new AtomicBoolean (true );
2058
+ AtomicReference <ConsumerRebalanceListener > rebal = new AtomicReference <>();
2057
2059
given (consumer .poll (any (Duration .class ))).willAnswer (i -> {
2058
2060
Thread .sleep (50 );
2061
+ if (rebalance .getAndSet (false )) {
2062
+ rebal .get ().onPartitionsRevoked (Collections .emptyList ());
2063
+ rebal .get ().onPartitionsAssigned (records .keySet ());
2064
+ }
2059
2065
return first .getAndSet (false ) ? consumerRecords : emptyRecords ;
2060
2066
});
2061
2067
final CountDownLatch commitLatch = new CountDownLatch (2 );
@@ -2064,9 +2070,11 @@ public void testPauseResume() throws Exception {
2064
2070
return null ;
2065
2071
}).given (consumer ).commitSync (anyMap (), any ());
2066
2072
given (consumer .assignment ()).willReturn (records .keySet ());
2067
- final CountDownLatch pauseLatch = new CountDownLatch (2 );
2073
+ final CountDownLatch pauseLatch1 = new CountDownLatch (2 ); // consumer, event publisher
2074
+ final CountDownLatch pauseLatch2 = new CountDownLatch (2 ); // consumer, consumer
2068
2075
willAnswer (i -> {
2069
- pauseLatch .countDown ();
2076
+ pauseLatch1 .countDown ();
2077
+ pauseLatch2 .countDown ();
2070
2078
return null ;
2071
2079
}).given (consumer ).pause (records .keySet ());
2072
2080
given (consumer .paused ()).willReturn (records .keySet ());
@@ -2075,14 +2083,17 @@ public void testPauseResume() throws Exception {
2075
2083
resumeLatch .countDown ();
2076
2084
return null ;
2077
2085
}).given (consumer ).resume (records .keySet ());
2078
- TopicPartitionInitialOffset [] topicPartition = new TopicPartitionInitialOffset [] {
2079
- new TopicPartitionInitialOffset ("foo" , 0 ) };
2080
- ContainerProperties containerProps = new ContainerProperties (topicPartition );
2086
+ willAnswer (invoc -> {
2087
+ rebal .set (invoc .getArgument (1 ));
2088
+ return null ;
2089
+ }).given (consumer ).subscribe (any (Collection .class ), any (ConsumerRebalanceListener .class ));
2090
+ ContainerProperties containerProps = new ContainerProperties ("foo" );
2081
2091
containerProps .setGroupId ("grp" );
2082
2092
containerProps .setAckMode (AckMode .RECORD );
2083
2093
containerProps .setClientId ("clientId" );
2084
2094
containerProps .setIdleEventInterval (100L );
2085
2095
containerProps .setMessageListener ((MessageListener ) r -> { });
2096
+ containerProps .setMissingTopicsFatal (false );
2086
2097
Properties consumerProps = new Properties ();
2087
2098
consumerProps .setProperty (ConsumerConfig .DEFAULT_API_TIMEOUT_MS_CONFIG , "42000" );
2088
2099
containerProps .setConsumerProperties (consumerProps );
@@ -2092,7 +2103,7 @@ public void testPauseResume() throws Exception {
2092
2103
CountDownLatch stopLatch = new CountDownLatch (1 );
2093
2104
container .setApplicationEventPublisher (e -> {
2094
2105
if (e instanceof ConsumerPausedEvent ) {
2095
- pauseLatch .countDown ();
2106
+ pauseLatch1 .countDown ();
2096
2107
}
2097
2108
else if (e instanceof ConsumerResumedEvent ) {
2098
2109
resumeLatch .countDown ();
@@ -2103,16 +2114,19 @@ else if (e instanceof ConsumerStoppedEvent) {
2103
2114
});
2104
2115
container .start ();
2105
2116
assertThat (commitLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
2106
- verify (consumer , times (2 )).commitSync (anyMap (), eq (Duration .ofSeconds (41 )));
2117
+ verify (consumer , times (3 )).commitSync (anyMap (), eq (Duration .ofSeconds (41 )));
2107
2118
assertThat (container .isContainerPaused ()).isFalse ();
2108
2119
container .pause ();
2109
2120
assertThat (container .isPaused ()).isTrue ();
2110
- assertThat (pauseLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
2121
+ assertThat (pauseLatch1 .await (10 , TimeUnit .SECONDS )).isTrue ();
2111
2122
assertThat (container .isContainerPaused ()).isTrue ();
2123
+ rebalance .set (true ); // force a re-pause
2124
+ assertThat (pauseLatch2 .await (10 , TimeUnit .SECONDS )).isTrue ();
2112
2125
container .resume ();
2113
2126
assertThat (resumeLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
2114
2127
container .stop ();
2115
2128
assertThat (stopLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
2129
+ verify (consumer , times (4 )).commitSync (anyMap (), eq (Duration .ofSeconds (41 )));
2116
2130
}
2117
2131
2118
2132
@ SuppressWarnings ({ "unchecked" , "rawtypes" })
0 commit comments