@@ -189,7 +189,6 @@ public void testAdminTrimLedgers() throws Exception {
189189 Producer <byte []> producer = pulsarClient .newProducer ()
190190 .topic (partitionedTopic )
191191 .enableBatching (false )
192- .producerName ("producer-name" )
193192 .create ();
194193 @ Cleanup
195194 Consumer <byte []> consumer = pulsarClient .newConsumer ().topic (partitionedTopic )
@@ -218,9 +217,19 @@ public void testAdminTrimLedgers() throws Exception {
218217 consumer .acknowledge (msg );
219218 }
220219 //consumed ledger should be cleaned
221- admin .topics ().trimTopic (partitionedTopic );
222- Awaitility .await ().atMost (30 , TimeUnit .SECONDS ).untilAsserted (() ->
223- Assert .assertEquals (managedLedger .getLedgersInfoAsList ().size (), 1 ));
220+ // After trimming, the managed ledger should have at most 2 ledgers remaining:
221+ // - The ledger containing the last confirmed entry (cannot be trimmed)
222+ // - Possibly an empty active ledger if the last write caused a rollover
223+ // (with maxEntriesPerLedger=2, if partition-0 receives an even number of
224+ // messages, the last ledger is full and a new empty active ledger is created)
225+ // Re-trigger trim inside the loop because trimConsumedLedgersInBackground() is async
226+ // and the mark-delete position may not have been persisted yet on the first call.
227+ Awaitility .await ().atMost (30 , TimeUnit .SECONDS ).untilAsserted (() -> {
228+ managedLedger .trimConsumedLedgersInBackground (CompletableFuture .completedFuture (null ));
229+ Assert .assertTrue (managedLedger .getLedgersInfoAsList ().size () <= 2 ,
230+ "Expected at most 2 ledgers after trim, but found "
231+ + managedLedger .getLedgersInfoAsList ().size ());
232+ });
224233
225234 }
226235
0 commit comments