120
120
import java .util .concurrent .TimeUnit ;
121
121
import java .util .concurrent .TimeoutException ;
122
122
import java .util .concurrent .atomic .AtomicBoolean ;
123
+ import java .util .concurrent .atomic .AtomicInteger ;
123
124
import java .util .concurrent .atomic .AtomicReference ;
124
125
import java .util .regex .Pattern ;
125
126
import java .util .stream .Collectors ;
@@ -1842,7 +1843,7 @@ public void testRebalanceException() {
1842
1843
}
1843
1844
1844
1845
@ Test
1845
- public void testReturnRecordsDuringRebalance () {
1846
+ public void testReturnRecordsDuringRebalance () throws InterruptedException {
1846
1847
Time time = new MockTime (1L );
1847
1848
SubscriptionState subscription = new SubscriptionState (new LogContext (), OffsetResetStrategy .EARLIEST );
1848
1849
ConsumerMetadata metadata = createMetadata (subscription );
@@ -1857,15 +1858,13 @@ public void testReturnRecordsDuringRebalance() {
1857
1858
Node node = metadata .fetch ().nodes ().get (0 );
1858
1859
Node coordinator = prepareRebalance (client , node , assignor , Arrays .asList (tp0 , t2p0 ), null );
1859
1860
1860
- // a first poll with zero millisecond would not complete the rebalance
1861
- consumer .poll (Duration .ZERO );
1861
+ // a poll with non-zero milliseconds would complete three round-trips (discover, join, sync)
1862
+ TestUtils .waitForCondition (() -> {
1863
+ consumer .poll (Duration .ofMillis (100L ));
1864
+ return consumer .assignment ().equals (Utils .mkSet (tp0 , t2p0 ));
1865
+ }, "Does not complete rebalance in time" );
1862
1866
1863
1867
assertEquals (Utils .mkSet (topic , topic2 ), consumer .subscription ());
1864
- assertEquals (Collections .emptySet (), consumer .assignment ());
1865
-
1866
- // a second poll with non-zero milliseconds would complete three round-trips (discover, join, sync)
1867
- consumer .poll (Duration .ofMillis (100L ));
1868
-
1869
1868
assertEquals (Utils .mkSet (tp0 , t2p0 ), consumer .assignment ());
1870
1869
1871
1870
// prepare a response of the outstanding fetch so that we have data available on the next poll
@@ -1918,7 +1917,6 @@ public void testReturnRecordsDuringRebalance() {
1918
1917
1919
1918
// mock rebalance responses
1920
1919
client .respondFrom (joinGroupFollowerResponse (assignor , 2 , "memberId" , "leaderId" , Errors .NONE ), coordinator );
1921
- client .prepareResponseFrom (syncGroupResponse (Arrays .asList (tp0 , t3p0 ), Errors .NONE ), coordinator );
1922
1920
1923
1921
// we need to poll 1) for getting the join response, and then send the sync request;
1924
1922
// 2) for getting the sync response
@@ -1934,12 +1932,19 @@ public void testReturnRecordsDuringRebalance() {
1934
1932
fetches1 .put (tp0 , new FetchInfo (3 , 1 ));
1935
1933
client .respondFrom (fetchResponse (fetches1 ), node );
1936
1934
1937
- records = consumer .poll (Duration .ZERO );
1935
+ // now complete the rebalance
1936
+ client .respondFrom (syncGroupResponse (Arrays .asList (tp0 , t3p0 ), Errors .NONE ), coordinator );
1937
+
1938
+ AtomicInteger count = new AtomicInteger (0 );
1939
+ TestUtils .waitForCondition (() -> {
1940
+ ConsumerRecords <String , String > recs = consumer .poll (Duration .ofMillis (100L ));
1941
+ return consumer .assignment ().equals (Utils .mkSet (tp0 , t3p0 )) && count .addAndGet (recs .count ()) == 1 ;
1942
+
1943
+ }, "Does not complete rebalance in time" );
1938
1944
1939
1945
// should have t3 but not sent yet the t3 records
1940
1946
assertEquals (Utils .mkSet (topic , topic3 ), consumer .subscription ());
1941
1947
assertEquals (Utils .mkSet (tp0 , t3p0 ), consumer .assignment ());
1942
- assertEquals (1 , records .count ());
1943
1948
assertEquals (4L , consumer .position (tp0 ));
1944
1949
assertEquals (0L , consumer .position (t3p0 ));
1945
1950
@@ -1948,10 +1953,13 @@ public void testReturnRecordsDuringRebalance() {
1948
1953
fetches1 .put (t3p0 , new FetchInfo (0 , 100 ));
1949
1954
client .respondFrom (fetchResponse (fetches1 ), node );
1950
1955
1951
- records = consumer .poll (Duration .ZERO );
1956
+ count .set (0 );
1957
+ TestUtils .waitForCondition (() -> {
1958
+ ConsumerRecords <String , String > recs = consumer .poll (Duration .ofMillis (100L ));
1959
+ return count .addAndGet (recs .count ()) == 101 ;
1960
+
1961
+ }, "Does not complete rebalance in time" );
1952
1962
1953
- // should have t3 but not sent yet the t3 records
1954
- assertEquals (101 , records .count ());
1955
1963
assertEquals (5L , consumer .position (tp0 ));
1956
1964
assertEquals (100L , consumer .position (t3p0 ));
1957
1965
0 commit comments