|
| 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 | +package org.apache.hadoop.hbase.client; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.lang.reflect.InvocationTargetException; |
| 22 | +import java.lang.reflect.Method; |
| 23 | +import java.lang.reflect.UndeclaredThrowableException; |
| 24 | +import org.apache.commons.lang3.reflect.FieldUtils; |
| 25 | +import org.apache.hadoop.conf.Configuration; |
| 26 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 27 | +import org.apache.hadoop.hbase.IntegrationTestingUtility; |
| 28 | +import org.apache.hadoop.hbase.security.UserProvider; |
| 29 | +import org.apache.hadoop.hbase.testclassification.ClientTests; |
| 30 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 31 | +import org.junit.AfterClass; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.ClassRule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.experimental.categories.Category; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | +import org.mockito.Mockito; |
| 39 | +import org.mockito.junit.MockitoJUnitRunner; |
| 40 | + |
| 41 | +@Category({ ClientTests.class, MediumTests.class }) |
| 42 | +@RunWith(MockitoJUnitRunner.class) |
| 43 | +public class TestConnectionImplementation { |
| 44 | + @ClassRule |
| 45 | + public static final HBaseClassTestRule CLASS_RULE = |
| 46 | + HBaseClassTestRule.forClass(TestConnectionImplementation.class); |
| 47 | + private static final IntegrationTestingUtility TEST_UTIL = new IntegrationTestingUtility(); |
| 48 | + |
| 49 | + @BeforeClass |
| 50 | + public static void beforeClass() throws Exception { |
| 51 | + TEST_UTIL.startMiniCluster(1); |
| 52 | + } |
| 53 | + |
| 54 | + @AfterClass |
| 55 | + public static void afterClass() throws Exception { |
| 56 | + TEST_UTIL.shutdownMiniCluster(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testGetMaster_noCachedMasterState() throws IOException, IllegalAccessException { |
| 61 | + Configuration conf = TEST_UTIL.getConfiguration(); |
| 62 | + conf.setLong(ConnectionImplementation.MASTER_STATE_CACHE_TIMEOUT_SEC, 0L); |
| 63 | + ConnectionImplementation conn = |
| 64 | + new ConnectionImplementation(conf, null, UserProvider.instantiate(conf).getCurrent()); |
| 65 | + ConnectionImplementation.MasterServiceState masterServiceState = spyMasterServiceState(conn); |
| 66 | + conn.getMaster(); // This initializes the stubs but don't call isMasterRunning |
| 67 | + conn.getMaster(); // Calls isMasterRunning since stubs are initialized. Invocation 1 |
| 68 | + conn.getMaster(); // Calls isMasterRunning since stubs are initialized. Invocation 2 |
| 69 | + Mockito.verify(masterServiceState, Mockito.times(2)).isMasterRunning(); |
| 70 | + conn.close(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testGetMaster_masterStateCacheHit() throws IOException, IllegalAccessException { |
| 75 | + Configuration conf = TEST_UTIL.getConfiguration(); |
| 76 | + conf.setLong(ConnectionImplementation.MASTER_STATE_CACHE_TIMEOUT_SEC, 15L); |
| 77 | + ConnectionImplementation conn = |
| 78 | + new ConnectionImplementation(conf, null, UserProvider.instantiate(conf).getCurrent()); |
| 79 | + ConnectionImplementation.MasterServiceState masterServiceState = spyMasterServiceState(conn); |
| 80 | + conn.getMaster(); // This initializes the stubs but don't call isMasterRunning |
| 81 | + conn.getMaster(); // Uses cached value, don't call isMasterRunning |
| 82 | + conn.getMaster(); // Uses cached value, don't call isMasterRunning |
| 83 | + Mockito.verify(masterServiceState, Mockito.times(0)).isMasterRunning(); |
| 84 | + conn.close(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testGetMaster_masterStateCacheMiss() |
| 89 | + throws IOException, InterruptedException, IllegalAccessException { |
| 90 | + Configuration conf = TEST_UTIL.getConfiguration(); |
| 91 | + conf.setLong(ConnectionImplementation.MASTER_STATE_CACHE_TIMEOUT_SEC, 5L); |
| 92 | + ConnectionImplementation conn = |
| 93 | + new ConnectionImplementation(conf, null, UserProvider.instantiate(conf).getCurrent()); |
| 94 | + ConnectionImplementation.MasterServiceState masterServiceState = spyMasterServiceState(conn); |
| 95 | + conn.getMaster(); // This initializes the stubs but don't call isMasterRunning |
| 96 | + conn.getMaster(); // Uses cached value, don't call isMasterRunning |
| 97 | + conn.getMaster(); // Uses cached value, don't call isMasterRunning |
| 98 | + Thread.sleep(10000); |
| 99 | + conn.getMaster(); // Calls isMasterRunning after cache expiry. Invocation 1 |
| 100 | + Mockito.verify(masterServiceState, Mockito.times(1)).isMasterRunning(); |
| 101 | + conn.close(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testIsKeepAliveMasterConnectedAndRunning_UndeclaredThrowableException() |
| 106 | + throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { |
| 107 | + Configuration conf = TEST_UTIL.getConfiguration(); |
| 108 | + conf.setLong(ConnectionImplementation.MASTER_STATE_CACHE_TIMEOUT_SEC, 0); |
| 109 | + ConnectionImplementation conn = |
| 110 | + new ConnectionImplementation(conf, null, UserProvider.instantiate(conf).getCurrent()); |
| 111 | + conn.getMaster(); // Initializes stubs |
| 112 | + |
| 113 | + ConnectionImplementation.MasterServiceState masterServiceState = spyMasterServiceState(conn); |
| 114 | + Mockito.doThrow(new UndeclaredThrowableException(new Exception("DUMMY EXCEPTION"))) |
| 115 | + .when(masterServiceState).isMasterRunning(); |
| 116 | + |
| 117 | + // Verify that masterState is "false" because of to injected exception |
| 118 | + boolean isKeepAliveMasterRunning = |
| 119 | + (boolean) getIsKeepAliveMasterConnectedAndRunningMethod().invoke(conn); |
| 120 | + Assert.assertFalse(isKeepAliveMasterRunning); |
| 121 | + conn.close(); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testIsKeepAliveMasterConnectedAndRunning_IOException() |
| 126 | + throws IOException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { |
| 127 | + Configuration conf = TEST_UTIL.getConfiguration(); |
| 128 | + conf.setLong(ConnectionImplementation.MASTER_STATE_CACHE_TIMEOUT_SEC, 0); |
| 129 | + ConnectionImplementation conn = |
| 130 | + new ConnectionImplementation(conf, null, UserProvider.instantiate(conf).getCurrent()); |
| 131 | + conn.getMaster(); |
| 132 | + |
| 133 | + ConnectionImplementation.MasterServiceState masterServiceState = spyMasterServiceState(conn); |
| 134 | + Mockito.doThrow(new IOException("DUMMY EXCEPTION")).when(masterServiceState).isMasterRunning(); |
| 135 | + |
| 136 | + boolean isKeepAliveMasterRunning = |
| 137 | + (boolean) getIsKeepAliveMasterConnectedAndRunningMethod().invoke(conn); |
| 138 | + |
| 139 | + // Verify that masterState is "false" because of to injected exception |
| 140 | + Assert.assertFalse(isKeepAliveMasterRunning); |
| 141 | + conn.close(); |
| 142 | + } |
| 143 | + |
| 144 | + // Spy the masterServiceState object using reflection |
| 145 | + private ConnectionImplementation.MasterServiceState |
| 146 | + spyMasterServiceState(ConnectionImplementation conn) throws IllegalAccessException { |
| 147 | + ConnectionImplementation.MasterServiceState spiedMasterServiceState = |
| 148 | + Mockito.spy(conn.getMasterServiceState()); |
| 149 | + FieldUtils.writeDeclaredField(conn, "masterServiceState", spiedMasterServiceState, true); |
| 150 | + return spiedMasterServiceState; |
| 151 | + } |
| 152 | + |
| 153 | + // Get isKeepAliveMasterConnectedAndRunning using reflection |
| 154 | + private Method getIsKeepAliveMasterConnectedAndRunningMethod() throws NoSuchMethodException { |
| 155 | + Method method = |
| 156 | + ConnectionImplementation.class.getDeclaredMethod("isKeepAliveMasterConnectedAndRunning"); |
| 157 | + method.setAccessible(true); |
| 158 | + return method; |
| 159 | + } |
| 160 | +} |
0 commit comments