|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.bookkeeper.mledger.impl; |
| 20 | + |
| 21 | +import static org.testng.Assert.assertEquals; |
| 22 | +import static org.apache.pulsar.broker.intercept.MangedLedgerInterceptorImplTest.TestPayloadProcessor; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Set; |
| 25 | +import lombok.extern.slf4j.Slf4j; |
| 26 | +import org.apache.bookkeeper.client.LedgerHandle; |
| 27 | +import org.apache.bookkeeper.mledger.ManagedLedgerConfig; |
| 28 | +import org.apache.bookkeeper.mledger.intercept.ManagedLedgerInterceptor; |
| 29 | +import org.apache.bookkeeper.test.MockedBookKeeperTestCase; |
| 30 | +import org.apache.pulsar.broker.intercept.ManagedLedgerInterceptorImpl; |
| 31 | +import org.apache.pulsar.broker.intercept.MangedLedgerInterceptorImplTest; |
| 32 | +import org.apache.pulsar.common.intercept.ManagedLedgerPayloadProcessor; |
| 33 | +import org.awaitility.Awaitility; |
| 34 | +import org.testng.annotations.Test; |
| 35 | + |
| 36 | +/*** |
| 37 | + * Differ to {@link MangedLedgerInterceptorImplTest}, this test can call {@link ManagedLedgerImpl}'s methods modified |
| 38 | + * by "default". |
| 39 | + */ |
| 40 | +@Slf4j |
| 41 | +@Test(groups = "broker") |
| 42 | +public class MangedLedgerInterceptorImplTest2 extends MockedBookKeeperTestCase { |
| 43 | + |
| 44 | + public static void switchLedgerManually(ManagedLedgerImpl ledger){ |
| 45 | + LedgerHandle originalLedgerHandle = ledger.currentLedger; |
| 46 | + ledger.ledgerClosed(ledger.currentLedger); |
| 47 | + ledger.createLedgerAfterClosed(); |
| 48 | + Awaitility.await().until(() -> { |
| 49 | + return ledger.state == ManagedLedgerImpl.State.LedgerOpened && ledger.currentLedger != originalLedgerHandle; |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testCurrentLedgerSizeCorrectIfHasInterceptor() throws Exception { |
| 55 | + final String mlName = "ml1"; |
| 56 | + final String cursorName = "cursor1"; |
| 57 | + |
| 58 | + // Registry interceptor. |
| 59 | + ManagedLedgerConfig config = new ManagedLedgerConfig(); |
| 60 | + Set<ManagedLedgerPayloadProcessor> processors = new HashSet(); |
| 61 | + processors.add(new TestPayloadProcessor()); |
| 62 | + ManagedLedgerInterceptor interceptor = new ManagedLedgerInterceptorImpl(new HashSet(), processors); |
| 63 | + config.setManagedLedgerInterceptor(interceptor); |
| 64 | + config.setMaxEntriesPerLedger(100); |
| 65 | + |
| 66 | + // Add one entry. |
| 67 | + ManagedLedgerImpl ledger = (ManagedLedgerImpl) factory.open(mlName, config); |
| 68 | + ManagedCursorImpl cursor = (ManagedCursorImpl) ledger.openCursor(cursorName); |
| 69 | + ledger.addEntry(new byte[1]); |
| 70 | + |
| 71 | + // Mark "currentLedgerSize" and switch ledger. |
| 72 | + long currentLedgerSize = ledger.getCurrentLedgerSize(); |
| 73 | + switchLedgerManually(ledger); |
| 74 | + |
| 75 | + // verify. |
| 76 | + assertEquals(currentLedgerSize, MangedLedgerInterceptorImplTest.calculatePreciseSize(ledger)); |
| 77 | + |
| 78 | + // cleanup. |
| 79 | + cursor.close(); |
| 80 | + ledger.close(); |
| 81 | + factory.getEntryCacheManager().clear(); |
| 82 | + factory.shutdown(); |
| 83 | + config.setManagedLedgerInterceptor(null); |
| 84 | + } |
| 85 | +} |
0 commit comments