|
| 1 | +/* |
| 2 | + * Copyright 2022 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; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static org.junit.Assert.fail; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | +import java.util.regex.Pattern; |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public class GlobalInterceptorsTest { |
| 29 | + |
| 30 | + private final StaticTestingClassLoader classLoader = |
| 31 | + new StaticTestingClassLoader( |
| 32 | + getClass().getClassLoader(), Pattern.compile("io\\.grpc\\.[^.]+")); |
| 33 | + |
| 34 | + @Test |
| 35 | + public void setInterceptorsTracers() throws Exception { |
| 36 | + Class<?> runnable = classLoader.loadClass(StaticTestingClassLoaderSet.class.getName()); |
| 37 | + ((Runnable) runnable.getDeclaredConstructor().newInstance()).run(); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void setGlobalInterceptorsTracers_twice() throws Exception { |
| 42 | + Class<?> runnable = classLoader.loadClass(StaticTestingClassLoaderSetTwice.class.getName()); |
| 43 | + ((Runnable) runnable.getDeclaredConstructor().newInstance()).run(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void getBeforeSet_clientInterceptors() throws Exception { |
| 48 | + Class<?> runnable = |
| 49 | + classLoader.loadClass( |
| 50 | + StaticTestingClassLoaderGetBeforeSetClientInterceptor.class.getName()); |
| 51 | + ((Runnable) runnable.getDeclaredConstructor().newInstance()).run(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void getBeforeSet_serverInterceptors() throws Exception { |
| 56 | + Class<?> runnable = |
| 57 | + classLoader.loadClass( |
| 58 | + StaticTestingClassLoaderGetBeforeSetServerInterceptor.class.getName()); |
| 59 | + ((Runnable) runnable.getDeclaredConstructor().newInstance()).run(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void getBeforeSet_serverStreamTracerFactories() throws Exception { |
| 64 | + Class<?> runnable = |
| 65 | + classLoader.loadClass( |
| 66 | + StaticTestingClassLoaderGetBeforeSetServerStreamTracerFactory.class.getName()); |
| 67 | + ((Runnable) runnable.getDeclaredConstructor().newInstance()).run(); |
| 68 | + } |
| 69 | + |
| 70 | + // UsedReflectively |
| 71 | + public static final class StaticTestingClassLoaderSet implements Runnable { |
| 72 | + @Override |
| 73 | + public void run() { |
| 74 | + List<ClientInterceptor> clientInterceptorList = |
| 75 | + new ArrayList<>(Arrays.asList(new NoopClientInterceptor())); |
| 76 | + List<ServerInterceptor> serverInterceptorList = |
| 77 | + new ArrayList<>(Arrays.asList(new NoopServerInterceptor())); |
| 78 | + List<ServerStreamTracer.Factory> serverStreamTracerFactoryList = |
| 79 | + new ArrayList<>( |
| 80 | + Arrays.asList( |
| 81 | + new NoopServerStreamTracerFactory(), new NoopServerStreamTracerFactory())); |
| 82 | + |
| 83 | + GlobalInterceptors.setInterceptorsTracers( |
| 84 | + clientInterceptorList, serverInterceptorList, serverStreamTracerFactoryList); |
| 85 | + |
| 86 | + assertThat(GlobalInterceptors.getClientInterceptors()).isEqualTo(clientInterceptorList); |
| 87 | + assertThat(GlobalInterceptors.getServerInterceptors()).isEqualTo(serverInterceptorList); |
| 88 | + assertThat(GlobalInterceptors.getServerStreamTracerFactories()) |
| 89 | + .isEqualTo(serverStreamTracerFactoryList); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static final class StaticTestingClassLoaderSetTwice implements Runnable { |
| 94 | + @Override |
| 95 | + public void run() { |
| 96 | + GlobalInterceptors.setInterceptorsTracers( |
| 97 | + new ArrayList<>(Arrays.asList(new NoopClientInterceptor())), |
| 98 | + null, |
| 99 | + new ArrayList<>(Arrays.asList(new NoopServerStreamTracerFactory()))); |
| 100 | + try { |
| 101 | + GlobalInterceptors.setInterceptorsTracers( |
| 102 | + null, new ArrayList<>(Arrays.asList(new NoopServerInterceptor())), null); |
| 103 | + fail("should have failed for calling setGlobalInterceptorsTracers() again"); |
| 104 | + } catch (IllegalStateException e) { |
| 105 | + assertThat(e).hasMessageThat().isEqualTo("Global interceptors and tracers are already set"); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static final class StaticTestingClassLoaderGetBeforeSetClientInterceptor |
| 111 | + implements Runnable { |
| 112 | + @Override |
| 113 | + public void run() { |
| 114 | + List<ClientInterceptor> clientInterceptors = GlobalInterceptors.getClientInterceptors(); |
| 115 | + assertThat(clientInterceptors).isEmpty(); |
| 116 | + |
| 117 | + try { |
| 118 | + GlobalInterceptors.setInterceptorsTracers( |
| 119 | + new ArrayList<>(Arrays.asList(new NoopClientInterceptor())), null, null); |
| 120 | + fail("should have failed for invoking set call after get is already called"); |
| 121 | + } catch (IllegalStateException e) { |
| 122 | + assertThat(e).hasMessageThat().isEqualTo("Set cannot be called after any get call"); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public static final class StaticTestingClassLoaderGetBeforeSetServerInterceptor |
| 128 | + implements Runnable { |
| 129 | + @Override |
| 130 | + public void run() { |
| 131 | + List<ServerInterceptor> serverInterceptors = GlobalInterceptors.getServerInterceptors(); |
| 132 | + assertThat(serverInterceptors).isEmpty(); |
| 133 | + |
| 134 | + try { |
| 135 | + GlobalInterceptors.setInterceptorsTracers( |
| 136 | + null, new ArrayList<>(Arrays.asList(new NoopServerInterceptor())), null); |
| 137 | + fail("should have failed for invoking set call after get is already called"); |
| 138 | + } catch (IllegalStateException e) { |
| 139 | + assertThat(e).hasMessageThat().isEqualTo("Set cannot be called after any get call"); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static final class StaticTestingClassLoaderGetBeforeSetServerStreamTracerFactory |
| 145 | + implements Runnable { |
| 146 | + @Override |
| 147 | + public void run() { |
| 148 | + List<ServerStreamTracer.Factory> serverStreamTracerFactories = |
| 149 | + GlobalInterceptors.getServerStreamTracerFactories(); |
| 150 | + assertThat(serverStreamTracerFactories).isEmpty(); |
| 151 | + |
| 152 | + try { |
| 153 | + GlobalInterceptors.setInterceptorsTracers( |
| 154 | + null, null, new ArrayList<>(Arrays.asList(new NoopServerStreamTracerFactory()))); |
| 155 | + fail("should have failed for invoking set call after get is already called"); |
| 156 | + } catch (IllegalStateException e) { |
| 157 | + assertThat(e).hasMessageThat().isEqualTo("Set cannot be called after any get call"); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private static class NoopClientInterceptor implements ClientInterceptor { |
| 163 | + @Override |
| 164 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 165 | + MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { |
| 166 | + return next.newCall(method, callOptions); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static class NoopServerInterceptor implements ServerInterceptor { |
| 171 | + @Override |
| 172 | + public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall( |
| 173 | + ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { |
| 174 | + return next.startCall(call, headers); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private static class NoopServerStreamTracerFactory extends ServerStreamTracer.Factory { |
| 179 | + @Override |
| 180 | + public ServerStreamTracer newServerStreamTracer(String fullMethodName, Metadata headers) { |
| 181 | + throw new UnsupportedOperationException(); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments