|
| 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.pulsar.broker; |
| 20 | + |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static org.mockito.ArgumentMatchers.any; |
| 23 | +import static org.mockito.ArgumentMatchers.isNull; |
| 24 | +import static org.mockito.Mockito.doReturn; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.spy; |
| 27 | +import static org.mockito.Mockito.verify; |
| 28 | +import static org.mockito.Mockito.when; |
| 29 | +import io.netty.channel.EventLoopGroup; |
| 30 | +import io.opentelemetry.api.OpenTelemetry; |
| 31 | +import java.util.concurrent.CompletableFuture; |
| 32 | +import org.apache.bookkeeper.client.BookKeeper; |
| 33 | +import org.apache.bookkeeper.mledger.ManagedLedgerFactoryConfig; |
| 34 | +import org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl; |
| 35 | +import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; |
| 36 | +import org.mockito.ArgumentCaptor; |
| 37 | +import org.testng.annotations.DataProvider; |
| 38 | +import org.testng.annotations.Test; |
| 39 | + |
| 40 | +@Test(groups = "broker") |
| 41 | +public class ManagedLedgerClientFactoryTest { |
| 42 | + @DataProvider |
| 43 | + public Object[][] cacheExtensionSettings() { |
| 44 | + return new Object[][] {{null, 5}, {false, 2}, {true, 0}, {true, 5}}; |
| 45 | + } |
| 46 | + |
| 47 | + @Test(dataProvider = "cacheExtensionSettings") |
| 48 | + public void testCacheExtensionSettingsAtStartup(Boolean extendRecentlyAccessed, int maxExtensions) |
| 49 | + throws Exception { |
| 50 | + ServiceConfiguration conf = new ServiceConfiguration(); |
| 51 | + if (extendRecentlyAccessed == null) { |
| 52 | + assertThat(conf.isManagedLedgerCacheEvictionExtendTTLOfRecentlyAccessed()).isFalse(); |
| 53 | + } else { |
| 54 | + conf.setManagedLedgerCacheEvictionExtendTTLOfRecentlyAccessed(extendRecentlyAccessed); |
| 55 | + } |
| 56 | + conf.setManagedLedgerCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes(maxExtensions); |
| 57 | + conf.setBookkeeperClientExposeStatsToPrometheus(false); |
| 58 | + BookKeeperClientFactory bookkeeperProvider = mock(BookKeeperClientFactory.class); |
| 59 | + when(bookkeeperProvider.create(any(), any(), any(), any(), isNull(), any())) |
| 60 | + .thenReturn(CompletableFuture.completedFuture(mock(BookKeeper.class))); |
| 61 | + try (ManagedLedgerClientFactory factory = spy(new ManagedLedgerClientFactory())) { |
| 62 | + doReturn(mock(ManagedLedgerFactoryImpl.class)).when(factory) |
| 63 | + .createManagedLedgerFactory(any(), any(), any(), any(), any()); |
| 64 | + factory.initialize(conf, mock(MetadataStoreExtended.class), bookkeeperProvider, |
| 65 | + mock(EventLoopGroup.class), OpenTelemetry.noop()); |
| 66 | + ArgumentCaptor<ManagedLedgerFactoryConfig> config = |
| 67 | + ArgumentCaptor.forClass(ManagedLedgerFactoryConfig.class); |
| 68 | + verify(factory).createManagedLedgerFactory(any(), any(), any(), config.capture(), any()); |
| 69 | + assertThat(config.getValue().isCacheEvictionExtendTTLOfRecentlyAccessed()) |
| 70 | + .isEqualTo(Boolean.TRUE.equals(extendRecentlyAccessed)); |
| 71 | + assertThat(config.getValue().getCacheEvictionExtendTTLOfEntriesWithRemainingExpectedReadsMaxTimes()) |
| 72 | + .isEqualTo(maxExtensions); |
| 73 | + } |
| 74 | + } |
| 75 | +} |