@Test
    public void testEligibleLeaderReplicas_ElectLastKnownLeader() {
        short version = 2;
        PartitionRegistration partition = new PartitionRegistration.Builder()
                .setReplicas(new int[] {1, 2, 3, 4})
                .setDirectories(DirectoryId.migratingArray(4))
                .setIsr(new int[] {})
                .setElr(new int[] {})
                .setLastKnownElr(new int[] {1})
                .setLeader(-1)
                .setLeaderRecoveryState(LeaderRecoveryState.RECOVERED)
                .setLeaderEpoch(100)
                .setPartitionEpoch(200)
                .build();
        Uuid topicId = Uuid.fromString("FbrrdcfiR-KC2CPSTHaJrg");

        PartitionChangeBuilder builder = new PartitionChangeBuilder(partition, topicId, 0, r -> true,
                metadataVersionForPartitionChangeRecordVersion(version), 3)
            .setElection(Election.PREFERRED)
            .setUseLastKnownLeaderInBalancedRecovery(true)
            .setDefaultDirProvider(DEFAULT_DIR_PROVIDER)
            .setEligibleLeaderReplicasEnabled(true);

        builder.setTargetIsr(Collections.emptyList());

        ApiMessageAndVersion expectedRecord = new ApiMessageAndVersion(
            new PartitionChangeRecord()
                .setTopicId(topicId)
                .setPartitionId(0)
                .setIsr(Collections.singletonList(1))
                .setLeader(1)
                .setLeaderRecoveryState(LeaderRecoveryState.RECOVERING.value())
                .setLastKnownElr(Collections.emptyList()),
            version
        );
        assertEquals(Optional.of(expectedRecord), builder.build());
        partition = partition.merge((PartitionChangeRecord) builder.build().get().message());
        assertArrayEquals(new int[]{}, partition.elr, partition.toString());
        assertArrayEquals(new int[]{}, partition.lastKnownElr, partition.toString());
        assertArrayEquals(new int[]{1}, 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