tjiuming commented on code in PR #17398: URL: https://github.com/apache/pulsar/pull/17398#discussion_r991838423
########## managed-ledger/src/test/java/org/apache/bookkeeper/mledger/impl/OffloadPrefixTest.java: ########## @@ -787,33 +790,77 @@ private static byte[] buildEntry(int size, String pattern) { return entry; } - @Test - public void testAutoTriggerOffload() throws Exception { + @DataProvider(name = "testAutoTriggerOffload") + public Object[][] testAutoTriggerOffloadProvider() { + return new Object[][]{ + {null, 0L}, + {100L, null}, + {-1L, null}, + {null, null}, + {-1L, -1L}, + {1L, 1L} + }; + } + + @Test(dataProvider = "testAutoTriggerOffload") + public void testAutoTriggerOffload(Long sizeThreshold, Long timeThreshold) throws Exception { MockLedgerOffloader offloader = new MockLedgerOffloader(); ManagedLedgerConfig config = new ManagedLedgerConfig(); config.setMaxEntriesPerLedger(10); config.setRetentionTime(10, TimeUnit.MINUTES); config.setRetentionSizeInMB(10); - offloader.getOffloadPolicies().setManagedLedgerOffloadThresholdInBytes(100L); + offloader.getOffloadPolicies().setManagedLedgerOffloadThresholdInBytes(sizeThreshold); + offloader.getOffloadPolicies().setManagedLedgerOffloadThresholdInSeconds(timeThreshold); config.setLedgerOffloader(offloader); - ManagedLedgerImpl ledger = (ManagedLedgerImpl)factory.open("my_test_ledger", config); + ManagedLedgerImpl ledger = (ManagedLedgerImpl)factory.open("my_test_ledger" + UUID.randomUUID(), config); // Ledger will roll twice, offload will run on first ledger after second closed for (int i = 0; i < 25; i++) { + Thread.sleep(10); ledger.addEntry(buildEntry(10, "entry-" + i)); } assertEquals(ledger.getLedgersInfoAsList().size(), 3); - // offload should eventually be triggered - assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 1); - assertEquals(offloader.offloadedLedgers(), - Set.of(ledger.getLedgersInfoAsList().get(0).getLedgerId())); + if (sizeThreshold == null && timeThreshold != null && timeThreshold == 0L) { + // All the inactive ledgers will be offloaded + assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 2); + List<Long> allLedgerIds = ledger.getLedgersInfoAsList().stream().map(LedgerInfo::getLedgerId).toList(); + assertEquals(offloader.offloadedLedgers(), Set.of(allLedgerIds.get(0), allLedgerIds.get(1))); + } else if (sizeThreshold != null && sizeThreshold == 100L && timeThreshold == null) { + // The last 2 ledgers won't be offloaded + assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 1); + List<Long> allLedgerIds = ledger.getLedgersInfoAsList().stream().map(LedgerInfo::getLedgerId).toList(); + assertEquals(offloader.offloadedLedgers(), Set.of(allLedgerIds.get(0))); + } else if (sizeThreshold != null && sizeThreshold == -1L && timeThreshold == null) { + // Offloading is disabled, no ledgers will be offloaded. + assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 0); + assertEquals(offloader.offloadedLedgers().size(), 0); + } else if (sizeThreshold == null && timeThreshold == null) { + // Offloading is disabled, no ledgers will be offloaded. + assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 0); + assertEquals(offloader.offloadedLedgers().size(), 0); + } else if (sizeThreshold != null && sizeThreshold == 1L && timeThreshold != null && timeThreshold == 1L) { + // All the inactive ledgers will be offloaded + assertEventuallyTrue(() -> offloader.offloadedLedgers().size() == 2); + List<Long> allLedgerIds = ledger.getLedgersInfoAsList().stream().map(LedgerInfo::getLedgerId).toList(); + assertEquals(offloader.offloadedLedgers(), Set.of(allLedgerIds.get(0), allLedgerIds.get(1))); + } } - @Test - public void manualTriggerWhileAutoInProgress() throws Exception { + @DataProvider(name = "manualTriggerWhileAutoInProgress") + public Object[][] manualTriggerWhileAutoInProgressProvider() { Review Comment: Yes, but for the test, it needs to auto-trigger offload first, the provider is for auto-trigger -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@pulsar.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org