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.Assert.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.HBaseClassTestRule;
031import org.apache.hadoop.hbase.net.Address;
032import org.apache.hadoop.hbase.testclassification.ClientTests;
033import org.apache.hadoop.hbase.testclassification.SmallTests;
034import org.junit.After;
035import org.junit.Before;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.mockito.invocation.InvocationOnMock;
040import org.mockito.stubbing.Answer;
041
042@Category({ ClientTests.class, SmallTests.class })
043public class TestFailedServersLog {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestFailedServersLog.class);
048
049  static final int TEST_PORT = 9999;
050
051  private Address addr;
052
053  private org.apache.logging.log4j.core.Appender mockAppender;
054
055  @Before
056  public void setup() {
057    mockAppender = mock(org.apache.logging.log4j.core.Appender.class);
058    when(mockAppender.getName()).thenReturn("mockAppender");
059    when(mockAppender.isStarted()).thenReturn(true);
060    ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
061      .getLogger(FailedServers.class)).addAppender(mockAppender);
062
063  }
064
065  @After
066  public void teardown() {
067    ((org.apache.logging.log4j.core.Logger) org.apache.logging.log4j.LogManager
068      .getLogger(FailedServers.class)).removeAppender(mockAppender);
069  }
070
071  @Test
072  public void testAddToFailedServersLogging() {
073    AtomicReference<org.apache.logging.log4j.Level> level = new AtomicReference<>();
074    AtomicReference<String> msg = new AtomicReference<String>();
075    doAnswer(new Answer<Void>() {
076
077      @Override
078      public Void answer(InvocationOnMock invocation) throws Throwable {
079        org.apache.logging.log4j.core.LogEvent logEvent =
080          invocation.getArgument(0, org.apache.logging.log4j.core.LogEvent.class);
081        level.set(logEvent.getLevel());
082        msg.set(logEvent.getMessage().getFormattedMessage());
083        return null;
084      }
085    }).when(mockAppender).append(any(org.apache.logging.log4j.core.LogEvent.class));
086
087    Throwable nullException = new NullPointerException();
088    FailedServers fs = new FailedServers(new Configuration());
089    addr = Address.fromParts("localhost", TEST_PORT);
090
091    fs.addToFailedServers(addr, nullException);
092
093    verify(mockAppender, times(1)).append(any(org.apache.logging.log4j.core.LogEvent.class));
094    assertEquals(org.apache.logging.log4j.Level.DEBUG, level.get());
095    assertEquals("Added failed server with address " + addr.toString() + " to list caused by "
096      + nullException.toString(), msg.get());
097  }
098}