001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.ipc; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.mockito.ArgumentMatchers.any; 022import static org.mockito.Mockito.doAnswer; 023import static org.mockito.Mockito.mock; 024import static org.mockito.Mockito.times; 025import static org.mockito.Mockito.verify; 026import static org.mockito.Mockito.when; 027 028import java.util.concurrent.atomic.AtomicReference; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.net.Address; 031import org.apache.hadoop.hbase.testclassification.ClientTests; 032import org.apache.hadoop.hbase.testclassification.SmallTests; 033import org.junit.jupiter.api.AfterEach; 034import org.junit.jupiter.api.BeforeEach; 035import org.junit.jupiter.api.Tag; 036import org.junit.jupiter.api.Test; 037import org.mockito.invocation.InvocationOnMock; 038import org.mockito.stubbing.Answer; 039 040@Tag(ClientTests.TAG) 041@Tag(SmallTests.TAG) 042public class TestFailedServersLog { 043 044 static final int TEST_PORT = 9999; 045 046 private Address addr; 047 048 private org.apache.logging.log4j.core.Appender mockAppender; 049 050 @BeforeEach 051 public void setup() { 052 mockAppender = mock(org.apache.logging.log4j.core.Appender.class); 053 when(mockAppender.getName()).thenReturn("mockAppender"); 054 when(mockAppender.isStarted()).thenReturn(true); 055 ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager 056 .getLogger(FailedServers.class)).addAppender(mockAppender); 057 058 } 059 060 @AfterEach 061 public void teardown() { 062 ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager 063 .getLogger(FailedServers.class)).removeAppender(mockAppender); 064 } 065 066 @Test 067 public void testAddToFailedServersLogging() { 068 AtomicReference<org.apache.logging.log4j.Level> level = new AtomicReference<>(); 069 AtomicReference<String> msg = new AtomicReference<String>(); 070 doAnswer(new Answer<Void>() { 071 072 @Override 073 public Void answer(InvocationOnMock invocation) throws Throwable { 074 org.apache.logging.log4j.core.LogEvent logEvent = 075 invocation.getArgument(0, org.apache.logging.log4j.core.LogEvent.class); 076 level.set(logEvent.getLevel()); 077 msg.set(logEvent.getMessage().getFormattedMessage()); 078 return null; 079 } 080 }).when(mockAppender).append(any(org.apache.logging.log4j.core.LogEvent.class)); 081 082 Throwable nullException = new NullPointerException(); 083 FailedServers fs = new FailedServers(new Configuration()); 084 addr = Address.fromParts("localhost", TEST_PORT); 085 086 fs.addToFailedServers(addr, nullException); 087 088 verify(mockAppender, times(1)).append(any(org.apache.logging.log4j.core.LogEvent.class)); 089 assertEquals(org.apache.logging.log4j.Level.DEBUG, level.get()); 090 assertEquals("Added failed server with address " + addr.toString() + " to list caused by " 091 + nullException.toString(), msg.get()); 092 } 093}