@Test
    public void testEnsureIdempotentClose() {
        AtomicInteger onCloseCounter = new AtomicInteger();
        AtomicInteger onPreviousCloseCounter = new AtomicInteger();

        IdempotentCloser ic = new IdempotentCloser();

        // Verify initial invariants.
        assertFalse(ic.isClosed());
        assertEquals(0, onCloseCounter.get());
        assertEquals(0, onPreviousCloseCounter.get());

        // Our first close passes in both callbacks. As a result, our onClose callback should be run but our
        // onPreviousClose callback should not be invoked.
        ic.close(onCloseCounter::getAndIncrement, onPreviousCloseCounter::getAndIncrement);
        assertTrue(ic.isClosed());
        assertEquals(1, onCloseCounter.get());
        assertEquals(0, onPreviousCloseCounter.get());

        // Our second close again passes in both callbacks. As this is the second close, our onClose callback
        // should not be run but our onPreviousClose callback should be executed.
        ic.close(onCloseCounter::getAndIncrement, onPreviousCloseCounter::getAndIncrement);
        assertTrue(ic.isClosed());
        assertEquals(1, onCloseCounter.get());
        assertEquals(1, onPreviousCloseCounter.get());

        // Our third close yet again passes in both callbacks. As before, our onClose callback should not be run
        // but our onPreviousClose callback should be run again.
        ic.close(onCloseCounter::getAndIncrement, onPreviousCloseCounter::getAndIncrement);
        assertTrue(ic.isClosed());
        assertEquals(1, onCloseCounter.get());
        assertEquals(2, onPreviousCloseCounter.get());
    }
Tests that the onClose callback is only invoked once, while the onPreviousClose callback can be invoked
a variable number of times.
clients/src/test/java/org/apache/kafka/common/internals/IdempotentCloserTest.java