1 /* 2 * Copyright 2016 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 org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.when; 21 22 import io.netty.handler.ssl.SslContext; 23 import java.util.concurrent.TimeUnit; 24 import org.junit.Rule; 25 import org.junit.Test; 26 import org.junit.rules.ExpectedException; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 31 /** 32 * Unit tests for {@link NettyServerBuilder}. 33 */ 34 @RunWith(JUnit4.class) 35 public class NettyServerBuilderTest { 36 37 @Rule public final ExpectedException thrown = ExpectedException.none(); 38 39 private NettyServerBuilder builder = NettyServerBuilder.forPort(8080); 40 41 @Test sslContextCanBeNull()42 public void sslContextCanBeNull() { 43 builder.sslContext(null); 44 } 45 46 @Test failIfSslContextIsNotServer()47 public void failIfSslContextIsNotServer() { 48 SslContext sslContext = mock(SslContext.class); 49 when(sslContext.isClient()).thenReturn(true); 50 51 thrown.expect(IllegalArgumentException.class); 52 thrown.expectMessage("Client SSL context can not be used for server"); 53 builder.sslContext(sslContext); 54 } 55 56 @Test failIfKeepAliveTimeNegative()57 public void failIfKeepAliveTimeNegative() { 58 thrown.expect(IllegalArgumentException.class); 59 thrown.expectMessage("keepalive time must be positive"); 60 61 builder.keepAliveTime(-10L, TimeUnit.HOURS); 62 } 63 64 @Test failIfKeepAliveTimeoutNegative()65 public void failIfKeepAliveTimeoutNegative() { 66 thrown.expect(IllegalArgumentException.class); 67 thrown.expectMessage("keepalive timeout must be positive"); 68 69 builder.keepAliveTimeout(-10L, TimeUnit.HOURS); 70 } 71 72 @Test failIfMaxConcurrentCallsPerConnectionNegative()73 public void failIfMaxConcurrentCallsPerConnectionNegative() { 74 thrown.expect(IllegalArgumentException.class); 75 thrown.expectMessage("max must be positive"); 76 77 builder.maxConcurrentCallsPerConnection(0); 78 } 79 80 @Test failIfMaxHeaderListSizeNegative()81 public void failIfMaxHeaderListSizeNegative() { 82 thrown.expect(IllegalArgumentException.class); 83 thrown.expectMessage("maxHeaderListSize must be > 0"); 84 85 builder.maxHeaderListSize(0); 86 } 87 88 @Test failIfMaxConnectionIdleNegative()89 public void failIfMaxConnectionIdleNegative() { 90 thrown.expect(IllegalArgumentException.class); 91 thrown.expectMessage("max connection idle must be positive"); 92 93 builder.maxConnectionIdle(-1, TimeUnit.HOURS); 94 } 95 96 @Test failIfMaxConnectionAgeNegative()97 public void failIfMaxConnectionAgeNegative() { 98 thrown.expect(IllegalArgumentException.class); 99 thrown.expectMessage("max connection age must be positive"); 100 101 builder.maxConnectionAge(-1, TimeUnit.HOURS); 102 } 103 104 @Test failIfMaxConnectionAgeGraceNegative()105 public void failIfMaxConnectionAgeGraceNegative() { 106 thrown.expect(IllegalArgumentException.class); 107 thrown.expectMessage("max connection age grace must be non-negative"); 108 109 builder.maxConnectionAgeGrace(-1, TimeUnit.HOURS); 110 } 111 112 @Test failIfPermitKeepAliveTimeNegative()113 public void failIfPermitKeepAliveTimeNegative() { 114 thrown.expect(IllegalArgumentException.class); 115 thrown.expectMessage("permit keepalive time must be non-negative"); 116 117 builder.permitKeepAliveTime(-1, TimeUnit.HOURS); 118 } 119 } 120