|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.fs.azurebfs.services; |
| 20 | + |
| 21 | +import java.net.URI; |
| 22 | +import java.net.URL; |
| 23 | +import java.util.Map; |
| 24 | + |
| 25 | +import org.assertj.core.api.Assertions; |
| 26 | +import org.junit.Test; |
| 27 | +import org.mockito.Mockito; |
| 28 | + |
| 29 | +import org.apache.hadoop.conf.Configuration; |
| 30 | +import org.apache.hadoop.fs.azurebfs.AbfsConfiguration; |
| 31 | +import org.apache.hadoop.fs.azurebfs.AbfsCountersImpl; |
| 32 | +import org.apache.hadoop.fs.azurebfs.oauth2.AccessTokenProvider; |
| 33 | +import org.apache.hadoop.fs.azurebfs.utils.Base64; |
| 34 | +import org.apache.hadoop.fs.azurebfs.utils.MetricFormat; |
| 35 | + |
| 36 | +import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_METRIC_ACCOUNT_KEY; |
| 37 | +import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_METRIC_ACCOUNT_NAME; |
| 38 | +import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_METRIC_FORMAT; |
| 39 | +import static org.apache.hadoop.fs.azurebfs.services.AbfsClient.ABFS_CLIENT_TIMER_THREAD_NAME; |
| 40 | + |
| 41 | +/** |
| 42 | + * Unit test cases for the AbfsClient class. |
| 43 | + */ |
| 44 | +public class TestAbfsClient { |
| 45 | + private static final String ACCOUNT_NAME = "bogusAccountName.dfs.core.windows.net"; |
| 46 | + private static final String ACCOUNT_KEY = "testKey"; |
| 47 | + private static final long SLEEP_DURATION_MS = 500; |
| 48 | + |
| 49 | + /** |
| 50 | + * Test the initialization of the AbfsClient timer when metric collection is disabled. |
| 51 | + * In case of metric collection being disabled, the timer should not be initialized. |
| 52 | + * Asserting that the timer is null and the abfs-timer-client thread is not running. |
| 53 | + */ |
| 54 | + @Test |
| 55 | + public void testTimerInitializationWithoutMetricCollection() throws Exception { |
| 56 | + final Configuration configuration = new Configuration(); |
| 57 | + AbfsConfiguration abfsConfiguration = new AbfsConfiguration(configuration, ACCOUNT_NAME); |
| 58 | + |
| 59 | + AbfsCounters abfsCounters = Mockito.spy(new AbfsCountersImpl(new URI("abcd"))); |
| 60 | + AbfsClientContext abfsClientContext = new AbfsClientContextBuilder().withAbfsCounters(abfsCounters).build(); |
| 61 | + |
| 62 | + // Get an instance of AbfsClient. |
| 63 | + AbfsClient client = new AbfsDfsClient(new URL("https://azure.com"), |
| 64 | + null, |
| 65 | + abfsConfiguration, |
| 66 | + (AccessTokenProvider) null, |
| 67 | + null, |
| 68 | + abfsClientContext); |
| 69 | + |
| 70 | + Assertions.assertThat(client.getTimer()) |
| 71 | + .describedAs("Timer should not be initialized") |
| 72 | + .isNull(); |
| 73 | + |
| 74 | + // Check if a thread with the name "abfs-timer-client" exists |
| 75 | + Assertions.assertThat(isThreadRunning(ABFS_CLIENT_TIMER_THREAD_NAME)) |
| 76 | + .describedAs("Expected thread 'abfs-timer-client' not found") |
| 77 | + .isEqualTo(false); |
| 78 | + client.close(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test the initialization of the AbfsClient timer when metric collection is enabled. |
| 83 | + * In case of metric collection being enabled, the timer should be initialized. |
| 84 | + * Asserting that the timer is not null and the abfs-timer-client thread is running. |
| 85 | + * Also, asserting that the thread is removed after closing the client. |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void testTimerInitializationWithMetricCollection() throws Exception { |
| 89 | + final Configuration configuration = new Configuration(); |
| 90 | + configuration.set(FS_AZURE_METRIC_FORMAT, String.valueOf(MetricFormat.INTERNAL_BACKOFF_METRIC_FORMAT)); |
| 91 | + configuration.set(FS_AZURE_METRIC_ACCOUNT_NAME, ACCOUNT_NAME); |
| 92 | + configuration.set(FS_AZURE_METRIC_ACCOUNT_KEY, Base64.encode(ACCOUNT_KEY.getBytes())); |
| 93 | + AbfsConfiguration abfsConfiguration = new AbfsConfiguration(configuration, ACCOUNT_NAME); |
| 94 | + |
| 95 | + AbfsCounters abfsCounters = Mockito.spy(new AbfsCountersImpl(new URI("abcd"))); |
| 96 | + AbfsClientContext abfsClientContext = new AbfsClientContextBuilder().withAbfsCounters(abfsCounters).build(); |
| 97 | + |
| 98 | + // Get an instance of AbfsClient. |
| 99 | + AbfsClient client = new AbfsDfsClient(new URL("https://azure.com"), |
| 100 | + null, |
| 101 | + abfsConfiguration, |
| 102 | + (AccessTokenProvider) null, |
| 103 | + null, |
| 104 | + abfsClientContext); |
| 105 | + |
| 106 | + Assertions.assertThat(client.getTimer()) |
| 107 | + .describedAs("Timer should be initialized") |
| 108 | + .isNotNull(); |
| 109 | + |
| 110 | + // Check if a thread with the name "abfs-timer-client" exists |
| 111 | + Assertions.assertThat(isThreadRunning(ABFS_CLIENT_TIMER_THREAD_NAME)) |
| 112 | + .describedAs("Expected thread 'abfs-timer-client' not found") |
| 113 | + .isEqualTo(true); |
| 114 | + client.close(); |
| 115 | + |
| 116 | + // Check if the thread is removed after closing the client |
| 117 | + Thread.sleep(SLEEP_DURATION_MS); |
| 118 | + Assertions.assertThat(isThreadRunning(ABFS_CLIENT_TIMER_THREAD_NAME)) |
| 119 | + .describedAs("Unexpected thread 'abfs-timer-client' found") |
| 120 | + .isEqualTo(false); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Check if a thread with the specified name is running. |
| 125 | + * |
| 126 | + * @param threadName Name of the thread to check |
| 127 | + * @return true if the thread is running, false otherwise |
| 128 | + */ |
| 129 | + private boolean isThreadRunning(String threadName) { |
| 130 | + // Get all threads and their stack traces |
| 131 | + Map<Thread, StackTraceElement[]> allThreads = Thread.getAllStackTraces(); |
| 132 | + |
| 133 | + // Check if any thread has the specified name |
| 134 | + for (Thread thread : allThreads.keySet()) { |
| 135 | + if (thread.getName().equals(threadName)) { |
| 136 | + return true; |
| 137 | + } |
| 138 | + } |
| 139 | + return false; |
| 140 | + } |
| 141 | +} |
0 commit comments