|
45 | 45 | import org.junit.jupiter.api.extension.ExtendWith;
|
46 | 46 | import org.junit.jupiter.params.ParameterizedTest;
|
47 | 47 | import org.junit.jupiter.params.provider.Arguments;
|
| 48 | +import org.junit.jupiter.params.provider.CsvSource; |
48 | 49 | import org.junit.jupiter.params.provider.EnumSource;
|
49 | 50 | import org.junit.jupiter.params.provider.MethodSource;
|
50 | 51 | import org.junit.jupiter.params.provider.ValueSource;
|
@@ -1382,6 +1383,132 @@ private static Stream<Arguments> provideOtherErrors() {
|
1382 | 1383 | .map(Arguments::of);
|
1383 | 1384 | }
|
1384 | 1385 |
|
| 1386 | + @Test |
| 1387 | + public void testPollOnCloseWhenIsNotLeaving() { |
| 1388 | + final StreamsGroupHeartbeatRequestManager heartbeatRequestManager = createStreamsGroupHeartbeatRequestManager(); |
| 1389 | + |
| 1390 | + NetworkClientDelegate.PollResult result = heartbeatRequestManager.pollOnClose(time.milliseconds()); |
| 1391 | + |
| 1392 | + assertEquals(NetworkClientDelegate.PollResult.EMPTY, result); |
| 1393 | + } |
| 1394 | + |
| 1395 | + @Test |
| 1396 | + public void testPollOnCloseWhenIsLeaving() { |
| 1397 | + final StreamsGroupHeartbeatRequestManager heartbeatRequestManager = createStreamsGroupHeartbeatRequestManager(); |
| 1398 | + when(membershipManager.isLeavingGroup()).thenReturn(true); |
| 1399 | + when(membershipManager.groupId()).thenReturn(GROUP_ID); |
| 1400 | + when(membershipManager.memberId()).thenReturn(MEMBER_ID); |
| 1401 | + when(membershipManager.memberEpoch()).thenReturn(LEAVE_GROUP_MEMBER_EPOCH); |
| 1402 | + |
| 1403 | + NetworkClientDelegate.PollResult result = heartbeatRequestManager.pollOnClose(time.milliseconds()); |
| 1404 | + |
| 1405 | + assertEquals(1, result.unsentRequests.size()); |
| 1406 | + final NetworkClientDelegate.UnsentRequest networkRequest = result.unsentRequests.get(0); |
| 1407 | + StreamsGroupHeartbeatRequest streamsRequest = (StreamsGroupHeartbeatRequest) networkRequest.requestBuilder().build(); |
| 1408 | + assertEquals(GROUP_ID, streamsRequest.data().groupId()); |
| 1409 | + assertEquals(MEMBER_ID, streamsRequest.data().memberId()); |
| 1410 | + assertEquals(LEAVE_GROUP_MEMBER_EPOCH, streamsRequest.data().memberEpoch()); |
| 1411 | + } |
| 1412 | + |
| 1413 | + @Test |
| 1414 | + public void testMaximumTimeToWaitPollTimerExpired() { |
| 1415 | + try ( |
| 1416 | + final MockedConstruction<Timer> timerMockedConstruction = mockConstruction(Timer.class, (mock, context) -> { |
| 1417 | + when(mock.isExpired()).thenReturn(true); |
| 1418 | + }); |
| 1419 | + final MockedConstruction<HeartbeatRequestState> heartbeatRequestStateMockedConstruction = mockConstruction( |
| 1420 | + HeartbeatRequestState.class, |
| 1421 | + (mock, context) -> { |
| 1422 | + when(mock.requestInFlight()).thenReturn(false); |
| 1423 | + }) |
| 1424 | + ) { |
| 1425 | + final StreamsGroupHeartbeatRequestManager heartbeatRequestManager = createStreamsGroupHeartbeatRequestManager(); |
| 1426 | + final Timer pollTimer = timerMockedConstruction.constructed().get(0); |
| 1427 | + time.sleep(1234); |
| 1428 | + |
| 1429 | + final long maximumTimeToWait = heartbeatRequestManager.maximumTimeToWait(time.milliseconds()); |
| 1430 | + |
| 1431 | + assertEquals(0, maximumTimeToWait); |
| 1432 | + verify(pollTimer).update(time.milliseconds()); |
| 1433 | + } |
| 1434 | + } |
| 1435 | + |
| 1436 | + @Test |
| 1437 | + public void testMaximumTimeToWaitWhenHeartbeatShouldBeSentImmediately() { |
| 1438 | + try ( |
| 1439 | + final MockedConstruction<Timer> timerMockedConstruction = mockConstruction(Timer.class); |
| 1440 | + final MockedConstruction<HeartbeatRequestState> heartbeatRequestStateMockedConstruction = mockConstruction( |
| 1441 | + HeartbeatRequestState.class, |
| 1442 | + (mock, context) -> { |
| 1443 | + when(mock.requestInFlight()).thenReturn(false); |
| 1444 | + }) |
| 1445 | + ) { |
| 1446 | + final StreamsGroupHeartbeatRequestManager heartbeatRequestManager = createStreamsGroupHeartbeatRequestManager(); |
| 1447 | + final Timer pollTimer = timerMockedConstruction.constructed().get(0); |
| 1448 | + when(membershipManager.shouldNotWaitForHeartbeatInterval()).thenReturn(true); |
| 1449 | + time.sleep(1234); |
| 1450 | + |
| 1451 | + final long maximumTimeToWait = heartbeatRequestManager.maximumTimeToWait(time.milliseconds()); |
| 1452 | + |
| 1453 | + assertEquals(0, maximumTimeToWait); |
| 1454 | + verify(pollTimer).update(time.milliseconds()); |
| 1455 | + } |
| 1456 | + } |
| 1457 | + |
| 1458 | + @ParameterizedTest |
| 1459 | + @CsvSource({"true, false", "false, false", "true, true"}) |
| 1460 | + public void testMaximumTimeToWaitWhenHeartbeatShouldBeNotSentImmediately(final boolean isRequestInFlight, |
| 1461 | + final boolean shouldNotWaitForHeartbeatInterval) { |
| 1462 | + final long remainingMs = 12L; |
| 1463 | + final long timeToNextHeartbeatMs = 6L; |
| 1464 | + try ( |
| 1465 | + final MockedConstruction<Timer> timerMockedConstruction = mockConstruction(Timer.class, (mock, context) -> { |
| 1466 | + when(mock.remainingMs()).thenReturn(remainingMs); |
| 1467 | + }); |
| 1468 | + final MockedConstruction<HeartbeatRequestState> heartbeatRequestStateMockedConstruction = mockConstruction( |
| 1469 | + HeartbeatRequestState.class, |
| 1470 | + (mock, context) -> { |
| 1471 | + when(mock.requestInFlight()).thenReturn(isRequestInFlight); |
| 1472 | + when(mock.timeToNextHeartbeatMs(anyLong())).thenReturn(timeToNextHeartbeatMs); |
| 1473 | + }) |
| 1474 | + ) { |
| 1475 | + final StreamsGroupHeartbeatRequestManager heartbeatRequestManager = createStreamsGroupHeartbeatRequestManager(); |
| 1476 | + final Timer pollTimer = timerMockedConstruction.constructed().get(0); |
| 1477 | + when(membershipManager.shouldNotWaitForHeartbeatInterval()).thenReturn(shouldNotWaitForHeartbeatInterval); |
| 1478 | + time.sleep(1234); |
| 1479 | + |
| 1480 | + final long maximumTimeToWait = heartbeatRequestManager.maximumTimeToWait(time.milliseconds()); |
| 1481 | + |
| 1482 | + assertEquals(timeToNextHeartbeatMs, maximumTimeToWait); |
| 1483 | + verify(pollTimer).update(time.milliseconds()); |
| 1484 | + } |
| 1485 | + } |
| 1486 | + |
| 1487 | + @ParameterizedTest |
| 1488 | + @CsvSource({"12, 5", "10, 6"}) |
| 1489 | + public void testMaximumTimeToWaitSelectingMinimumWaitTime(final long remainingMs, |
| 1490 | + final long timeToNextHeartbeatMs) { |
| 1491 | + try ( |
| 1492 | + final MockedConstruction<Timer> timerMockedConstruction = mockConstruction(Timer.class, (mock, context) -> { |
| 1493 | + when(mock.remainingMs()).thenReturn(remainingMs); |
| 1494 | + }); |
| 1495 | + final MockedConstruction<HeartbeatRequestState> heartbeatRequestStateMockedConstruction = mockConstruction( |
| 1496 | + HeartbeatRequestState.class, |
| 1497 | + (mock, context) -> { |
| 1498 | + when(mock.timeToNextHeartbeatMs(anyLong())).thenReturn(timeToNextHeartbeatMs); |
| 1499 | + }) |
| 1500 | + ) { |
| 1501 | + final StreamsGroupHeartbeatRequestManager heartbeatRequestManager = createStreamsGroupHeartbeatRequestManager(); |
| 1502 | + final Timer pollTimer = timerMockedConstruction.constructed().get(0); |
| 1503 | + time.sleep(1234); |
| 1504 | + |
| 1505 | + final long maximumTimeToWait = heartbeatRequestManager.maximumTimeToWait(time.milliseconds()); |
| 1506 | + |
| 1507 | + assertEquals(5, maximumTimeToWait); |
| 1508 | + verify(pollTimer).update(time.milliseconds()); |
| 1509 | + } |
| 1510 | + } |
| 1511 | + |
1385 | 1512 | private static ConsumerConfig config() {
|
1386 | 1513 | Properties prop = new Properties();
|
1387 | 1514 | prop.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
|
|
0 commit comments