@ParameterizedTest
    @ValueSource(booleans = {true, false})
    public void testEligibleLeaderReplicas_ElrCanBeElected(boolean lastKnownLeaderEnabled) {
        short version = 2;
        PartitionRegistration partition = new PartitionRegistration.Builder()
            .setReplicas(new int[] {1, 2, 3, 4})
            .setDirectories(DirectoryId.migratingArray(4))
            .setIsr(new int[] {1})
            .setElr(new int[] {3})
            .setLastKnownElr(lastKnownLeaderEnabled ? new int[] {} : new int[] {2})
            .setLeader(1)
            .setLeaderRecoveryState(LeaderRecoveryState.RECOVERED)
            .setLeaderEpoch(100)
            .setPartitionEpoch(200)
            .build();
        Uuid topicId = Uuid.fromString("FbrrdcfiR-KC2CPSTHaJrg");

        // Make replica 1 offline.
        PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topicId, 0, r -> r != 1,
                metadataVersionForPartitionChangeRecordVersion(version), 3)
            .setElection(Election.PREFERRED)
            .setEligibleLeaderReplicasEnabled(isElrEnabled(version))
            .setUseLastKnownLeaderInBalancedRecovery(lastKnownLeaderEnabled)
            .setDefaultDirProvider(DEFAULT_DIR_PROVIDER);

        builder.setTargetIsr(Collections.emptyList());

        ApiMessageAndVersion expectedRecord = new ApiMessageAndVersion(
            new PartitionChangeRecord()
                .setTopicId(topicId)
                .setPartitionId(0)
                .setIsr(Collections.singletonList(3))
                .setEligibleLeaderReplicas(Collections.singletonList(1))
                .setLeader(3)
                .setLeaderRecoveryState(LeaderRecoveryState.NO_CHANGE),
            version
        );
        assertEquals(Optional.of(expectedRecord), builder.build());
        partition = partition.merge((PartitionChangeRecord) builder.build().get().message());
        assertArrayEquals(new int[]{1}, partition.elr, partition.toString());
        assertArrayEquals(lastKnownLeaderEnabled ? new int[]{} : new int[]{2}, partition.lastKnownElr, partition.toString());
        assertArrayEquals(new int[]{3}, partition.isr, partition.toString());
    }
Regression test for KAFKA-16624. Tests that when targetIsr is the empty list, but we
cannot actually change the ISR, triggerLeaderEpochBumpForIsrShrinkIfNeeded does not engage.
metadata/src/test/java/org/apache/kafka/controller/PartitionChangeBuilderTest.java