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.internal; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.junit.Assert.assertEquals; 21 22 import io.grpc.Metadata; 23 import io.grpc.ServerStreamTracer; 24 import io.grpc.internal.testing.StatsTestUtils.FakeStatsRecorder; 25 import io.grpc.internal.testing.StatsTestUtils.FakeTagContextBinarySerializer; 26 import io.grpc.internal.testing.StatsTestUtils.FakeTagger; 27 import java.io.File; 28 import java.util.List; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 /** Unit tests for {@link AbstractServerImplBuilder}. */ 34 @RunWith(JUnit4.class) 35 public class AbstractServerImplBuilderTest { 36 37 private static final ServerStreamTracer.Factory DUMMY_USER_TRACER = 38 new ServerStreamTracer.Factory() { 39 @Override 40 public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata headers) { 41 throw new UnsupportedOperationException(); 42 } 43 }; 44 45 private Builder builder = new Builder(); 46 47 @Test 48 public void getTracerFactories_default() { 49 builder.addStreamTracerFactory(DUMMY_USER_TRACER); 50 List<ServerStreamTracer.Factory> factories = builder.getTracerFactories(); 51 assertEquals(3, factories.size()); 52 assertThat(factories.get(0)).isInstanceOf(CensusStatsModule.ServerTracerFactory.class); 53 assertThat(factories.get(1)).isInstanceOf(CensusTracingModule.ServerTracerFactory.class); 54 assertThat(factories.get(2)).isSameAs(DUMMY_USER_TRACER); 55 } 56 57 @Test 58 public void getTracerFactories_disableStats() { 59 builder.addStreamTracerFactory(DUMMY_USER_TRACER); 60 builder.setStatsEnabled(false); 61 List<ServerStreamTracer.Factory> factories = builder.getTracerFactories(); 62 assertEquals(2, factories.size()); 63 assertThat(factories.get(0)).isInstanceOf(CensusTracingModule.ServerTracerFactory.class); 64 assertThat(factories.get(1)).isSameAs(DUMMY_USER_TRACER); 65 } 66 67 @Test 68 public void getTracerFactories_disableTracing() { 69 builder.addStreamTracerFactory(DUMMY_USER_TRACER); 70 builder.setTracingEnabled(false); 71 List<ServerStreamTracer.Factory> factories = builder.getTracerFactories(); 72 assertEquals(2, factories.size()); 73 assertThat(factories.get(0)).isInstanceOf(CensusStatsModule.ServerTracerFactory.class); 74 assertThat(factories.get(1)).isSameAs(DUMMY_USER_TRACER); 75 } 76 77 @Test 78 public void getTracerFactories_disableBoth() { 79 builder.addStreamTracerFactory(DUMMY_USER_TRACER); 80 builder.setTracingEnabled(false); 81 builder.setStatsEnabled(false); 82 List<ServerStreamTracer.Factory> factories = builder.getTracerFactories(); 83 assertThat(factories).containsExactly(DUMMY_USER_TRACER); 84 } 85 86 static class Builder extends AbstractServerImplBuilder<Builder> { 87 Builder() { 88 overrideCensusStatsModule( 89 new CensusStatsModule( 90 new FakeTagger(), 91 new FakeTagContextBinarySerializer(), 92 new FakeStatsRecorder(), 93 GrpcUtil.STOPWATCH_SUPPLIER, 94 true)); 95 } 96 97 @Override 98 protected io.grpc.internal.InternalServer buildTransportServer( 99 List<ServerStreamTracer.Factory> streamTracerFactories) { 100 throw new UnsupportedOperationException(); 101 } 102 103 @Override 104 public Builder useTransportSecurity(File certChain, File privateKey) { 105 throw new UnsupportedOperationException(); 106 } 107 } 108 109 } 110