public Cluster cluster() {
        if (clusterInstance == null) {
            throw new IllegalStateException("Cached Cluster instance should not be null, but was.");
        } else {
            return clusterInstance;
        }
    }
@Test
    public void testMissingLeaderEndpoint() {
        // Although the broker attempts to ensure leader information is available, the
        // client metadata cache may retain partition metadata across multiple responses.
        // For example, separate responses may contain conflicting leader epochs for
        // separate partitions and the client will always retain the highest.

        TopicPartition topicPartition = new TopicPartition("topic", 0);

        MetadataResponse.PartitionMetadata partitionMetadata = new MetadataResponse.PartitionMetadata(
                Errors.NONE,
                topicPartition,
                Optional.of(5),
                Optional.of(10),
                Arrays.asList(5, 6, 7),
                Arrays.asList(5, 6, 7),
                Collections.emptyList());

        Map<Integer, Node> nodesById = new HashMap<>();
        nodesById.put(6, new Node(6, "localhost", 2077));
        nodesById.put(7, new Node(7, "localhost", 2078));
        nodesById.put(8, new Node(8, "localhost", 2079));

        MetadataSnapshot cache = new MetadataSnapshot("clusterId",
                nodesById,
                Collections.singleton(partitionMetadata),
                Collections.emptySet(),
                Collections.emptySet(),
                Collections.emptySet(),
                null,
                Collections.emptyMap());

        Cluster cluster = cache.cluster();
        assertNull(cluster.leaderFor(topicPartition));

        PartitionInfo partitionInfo = cluster.partition(topicPartition);
        Map<Integer, Node> replicas = Arrays.stream(partitionInfo.replicas())
                .collect(Collectors.toMap(Node::id, Function.identity()));
        assertNull(partitionInfo.leader());
        assertEquals(3, replicas.size());
        assertTrue(replicas.get(5).isEmpty());
        assertEquals(nodesById.get(6), replicas.get(6));
        assertEquals(nodesById.get(7), replicas.get(7));
    }