public static Producer<byte[]> createExclusiveProducerWithRetry(PulsarClient client,
                                                                    String topic,
                                                                    String producerName,
                                                                    Supplier<Boolean> isLeader,
                                                                    int sleepInBetweenMs) throws NotLeaderAnymore {
        try {
            int tries = 0;
            do {
                try {
                    return client.newProducer().topic(topic)
                            .accessMode(ProducerAccessMode.Exclusive)
                            .enableBatching(false)
                            .blockIfQueueFull(true)
                            .compressionType(CompressionType.LZ4)
                            .producerName(producerName)
                            .createAsync().get(10, TimeUnit.SECONDS);
                } catch (Exception e) {
                    log.info("Encountered exception while at creating exclusive producer to topic {}", topic, e);
                }
                tries++;
                if (tries % 6 == 0) {
                    if (log.isDebugEnabled()) {
                        log.debug(
                                "Failed to acquire exclusive producer to topic {} after {} attempts. "
                                        + "Will retry if we are still the leader.", topic, tries);
                    }
                }
                Thread.sleep(sleepInBetweenMs);
            } while (isLeader.get());
        } catch (InterruptedException e) {
            throw new RuntimeException("Failed to create exclusive producer on topic " + topic, e);
        }

        throw new NotLeaderAnymore();
    }
@Test
    public void testCreateExclusiveProducerWithRetry() {
        Producer<byte[]> producer = mock(Producer.class);
        ProducerBuilder<byte[]> builder = mock(ProducerBuilder.class);
        when(builder.topic(anyString())).thenReturn(builder);
        when(builder.producerName(anyString())).thenReturn(builder);
        when(builder.enableBatching(anyBoolean())).thenReturn(builder);
        when(builder.blockIfQueueFull(anyBoolean())).thenReturn(builder);
        when(builder.compressionType(any(CompressionType.class))).thenReturn(builder);
        when(builder.sendTimeout(anyInt(), any(TimeUnit.class))).thenReturn(builder);
        when(builder.accessMode(any())).thenReturn(builder);
        when(builder.createAsync()).thenReturn(CompletableFuture.completedFuture(producer));

        PulsarClient pulsarClient = mock(PulsarClient.class);
        when(pulsarClient.newProducer()).thenReturn(builder);

        Producer<byte[]> p = null;
        try {
            p = WorkerUtils
                    .createExclusiveProducerWithRetry(pulsarClient, "test-topic", "test-producer", () -> true, 0);
        } catch (WorkerUtils.NotLeaderAnymore notLeaderAnymore) {
            fail();
        }
        assertNotNull(p);
        verify(builder, times(1)).topic(eq("test-topic"));
        verify(builder, times(1)).producerName(eq("test-producer"));
        verify(builder, times(1)).accessMode(eq(ProducerAccessMode.Exclusive));

        CompletableFuture completableFuture = new CompletableFuture();
        completableFuture.completeExceptionally(new PulsarClientException.ProducerFencedException("test"));
        when(builder.createAsync()).thenReturn(completableFuture);
        try {
            WorkerUtils.createExclusiveProducerWithRetry(pulsarClient, "test-topic", "test-producer", () -> false, 0);
            fail();
        } catch (WorkerUtils.NotLeaderAnymore notLeaderAnymore) {

        }

        AtomicInteger i = new AtomicInteger();
        try {
            WorkerUtils.createExclusiveProducerWithRetry(pulsarClient, "test-topic", "test-producer",
                    new Supplier<Boolean>() {

                @Override
                public Boolean get() {
                    if (i.getAndIncrement() < 6) {
                        return true;
                    }
                    return false;
                }
            }, 0);
            fail();
        } catch (WorkerUtils.NotLeaderAnymore notLeaderAnymore) {

        }
    }