1 /* 2 * Copyright 2017 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.netty; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static io.grpc.netty.NettyServerTransport.getLogLevel; 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertNull; 23 24 import java.io.IOException; 25 import java.net.SocketException; 26 import java.util.logging.Level; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 @RunWith(JUnit4.class) 32 public class NettyServerTransportTest { 33 @Test unknownException()34 public void unknownException() { 35 assertEquals(Level.INFO, getLogLevel(new Exception())); 36 } 37 38 @Test socketException()39 public void socketException() { 40 assertEquals(Level.FINE, getLogLevel(new SocketException("Connection reset"))); 41 } 42 43 @Test ioException()44 public void ioException() { 45 assertEquals(Level.FINE, getLogLevel(new IOException("Connection reset by peer"))); 46 assertEquals(Level.FINE, getLogLevel(new IOException( 47 "An existing connection was forcibly closed by the remote host"))); 48 } 49 50 @Test ioException_nullMessage()51 public void ioException_nullMessage() { 52 IOException e = new IOException(); 53 assertNull(e.getMessage()); 54 assertEquals(Level.FINE, getLogLevel(e)); 55 } 56 57 @Test extendedIoException()58 public void extendedIoException() { 59 class ExtendedIoException extends IOException {} 60 61 ExtendedIoException e = new ExtendedIoException(); 62 assertThat(e.getMessage()).isNull(); 63 assertThat(getLogLevel(e)).isEqualTo(Level.INFO); 64 } 65 66 @Test fakeNettyNativeIoException()67 public void fakeNettyNativeIoException() { 68 class NativeIoException extends IOException {} 69 70 NativeIoException fakeNativeIoException = new NativeIoException(); 71 72 assertThat(getLogLevel(fakeNativeIoException)).isEqualTo(Level.FINE); 73 } 74 } 75