public static CompletableFuture<Position> asyncGetLastValidPosition(final ManagedLedgerImpl ledger,
                                                                        final Predicate<Entry> predicate,
                                                                        final Position startPosition) {
        CompletableFuture<Position> future = new CompletableFuture<>();
        internalAsyncReverseFindPositionOneByOne(ledger, predicate, startPosition, future);
        return future;
    }
@Test
    public void testGetLastValidPosition() throws Exception {
        final int maxEntriesPerLedger = 5;

        ManagedLedgerConfig managedLedgerConfig = new ManagedLedgerConfig();
        managedLedgerConfig.setMaxEntriesPerLedger(maxEntriesPerLedger);
        managedLedgerConfig.setRetentionSizeInMB(10);
        managedLedgerConfig.setRetentionTime(5, TimeUnit.MINUTES);
        ManagedLedger ledger = factory.open("testReverseFindPositionOneByOne", managedLedgerConfig);

        String matchEntry = "match-entry";
        String noMatchEntry = "nomatch-entry";
        Predicate<Entry> predicate = entry -> {
            String entryValue = entry.getDataBuffer().toString(UTF_8);
            return matchEntry.equals(entryValue);
        };

        // New ledger will return the last position, regardless of whether the conditions are met or not.
        Position position = ManagedLedgerImplUtils.asyncGetLastValidPosition((ManagedLedgerImpl) ledger,
                predicate, ledger.getLastConfirmedEntry()).get();
        assertEquals(ledger.getLastConfirmedEntry(), position);

        for (int i = 0; i < maxEntriesPerLedger - 1; i++) {
            ledger.addEntry(matchEntry.getBytes(StandardCharsets.UTF_8));
        }
        Position lastMatchPosition = ledger.addEntry(matchEntry.getBytes(StandardCharsets.UTF_8));
        for (int i = 0; i < maxEntriesPerLedger; i++) {
            ledger.addEntry(noMatchEntry.getBytes(StandardCharsets.UTF_8));
        }

        // Returns last position of entry is "match-entry"
        position = ManagedLedgerImplUtils.asyncGetLastValidPosition((ManagedLedgerImpl) ledger,
                predicate, ledger.getLastConfirmedEntry()).get();
        assertEquals(position, lastMatchPosition);

        ledger.close();
    }