|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2020 Michael Zhang <[email protected]> |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 5 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 6 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 7 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 8 | + * |
| 9 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 10 | + * Software. |
| 11 | + * |
| 12 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 13 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 14 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 15 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +package net.devh.boot.grpc.test.config; |
| 19 | + |
| 20 | +import static net.devh.boot.grpc.common.util.InterceptorOrder.ORDER_LAST; |
| 21 | + |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | + |
| 24 | +import org.springframework.context.annotation.Configuration; |
| 25 | + |
| 26 | +import io.grpc.CallOptions; |
| 27 | +import io.grpc.Channel; |
| 28 | +import io.grpc.ClientCall; |
| 29 | +import io.grpc.ClientInterceptor; |
| 30 | +import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; |
| 31 | +import io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener; |
| 32 | +import io.grpc.ForwardingServerCall.SimpleForwardingServerCall; |
| 33 | +import io.grpc.Metadata; |
| 34 | +import io.grpc.MethodDescriptor; |
| 35 | +import io.grpc.ServerCall; |
| 36 | +import io.grpc.ServerCall.Listener; |
| 37 | +import io.grpc.ServerCallHandler; |
| 38 | +import io.grpc.ServerInterceptor; |
| 39 | +import io.grpc.Status; |
| 40 | +import net.devh.boot.grpc.client.interceptor.GrpcGlobalClientInterceptor; |
| 41 | +import net.devh.boot.grpc.client.interceptor.OrderedClientInterceptor; |
| 42 | +import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor; |
| 43 | +import net.devh.boot.grpc.server.interceptor.OrderedServerInterceptor; |
| 44 | + |
| 45 | +/** |
| 46 | + * Helper configuration that can be used to await the completion/closing of the next calls. |
| 47 | + * |
| 48 | + * @author Daniel Theuke ([email protected]) |
| 49 | + */ |
| 50 | +@Configuration |
| 51 | +public class AwaitableServerClientCallConfiguration { |
| 52 | + |
| 53 | + private static CountDownLatch serverCounter; |
| 54 | + private static CountDownLatch clientCounter; |
| 55 | + |
| 56 | + /** |
| 57 | + * A testing server interceptor, that allows awaiting the completion of the server call that is otherwise closed |
| 58 | + * asynchronously. |
| 59 | + * |
| 60 | + * @return A testing server interceptor bean. |
| 61 | + */ |
| 62 | + @GrpcGlobalServerInterceptor |
| 63 | + ServerInterceptor awaitableServerInterceptor() { |
| 64 | + return new OrderedServerInterceptor(new ServerInterceptor() { |
| 65 | + |
| 66 | + @Override |
| 67 | + public <ReqT, RespT> Listener<ReqT> interceptCall( |
| 68 | + final ServerCall<ReqT, RespT> call, |
| 69 | + final Metadata headers, |
| 70 | + final ServerCallHandler<ReqT, RespT> next) { |
| 71 | + |
| 72 | + if (serverCounter == null || serverCounter.getCount() == 0) { |
| 73 | + return next.startCall(call, headers); |
| 74 | + } else { |
| 75 | + final CountDownLatch thatCounter = serverCounter; |
| 76 | + return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) { |
| 77 | + |
| 78 | + @Override |
| 79 | + public void close(final Status status, final Metadata trailers) { |
| 80 | + super.close(status, trailers); |
| 81 | + thatCounter.countDown(); |
| 82 | + } |
| 83 | + |
| 84 | + }, headers); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + }, ORDER_LAST); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * A testing client interceptor, that allows awaiting the completion of the client call that is otherwise closed |
| 93 | + * asynchronously. |
| 94 | + * |
| 95 | + * @return A testing client interceptor bean. |
| 96 | + */ |
| 97 | + @GrpcGlobalClientInterceptor |
| 98 | + ClientInterceptor awaitableClientInterceptor() { |
| 99 | + return new OrderedClientInterceptor(new ClientInterceptor() { |
| 100 | + |
| 101 | + @Override |
| 102 | + public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall( |
| 103 | + final MethodDescriptor<ReqT, RespT> method, |
| 104 | + final CallOptions callOptions, |
| 105 | + final Channel next) { |
| 106 | + |
| 107 | + if (clientCounter == null || clientCounter.getCount() == 0) { |
| 108 | + return next.newCall(method, callOptions); |
| 109 | + } else { |
| 110 | + final CountDownLatch thatCounter = clientCounter; |
| 111 | + return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { |
| 112 | + |
| 113 | + @Override |
| 114 | + public void start(final Listener<RespT> responseListener, final Metadata headers) { |
| 115 | + super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) { |
| 116 | + |
| 117 | + @Override |
| 118 | + public void onClose(final Status status, final Metadata trailers) { |
| 119 | + super.onClose(status, trailers); |
| 120 | + thatCounter.countDown(); |
| 121 | + } |
| 122 | + |
| 123 | + }, headers); |
| 124 | + } |
| 125 | + |
| 126 | + }; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + }, ORDER_LAST); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Returns a {@link CountDownLatch} that will be used in the next server calls and can be used to await the |
| 135 | + * {@link ServerCall#close(Status, Metadata) ServerCall close}. This method must be called before the call is |
| 136 | + * started. |
| 137 | + * |
| 138 | + * @param count The number of call closes to await. |
| 139 | + * @return The counter used to await the server call close. |
| 140 | + */ |
| 141 | + public static CountDownLatch awaitNextServerCallCloses(final int count) { |
| 142 | + final CountDownLatch newCounter = new CountDownLatch(count); |
| 143 | + serverCounter = newCounter; |
| 144 | + return newCounter; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns a {@link CountDownLatch} that will be used in the next client calls and can be used to await the |
| 149 | + * {@link io.grpc.ClientCall.Listener#onClose(Status, Metadata) ClientCall close}. This method must be called before |
| 150 | + * the call is started. |
| 151 | + * |
| 152 | + * @param count The number of call closes to await. |
| 153 | + * @return The counter used to await the client call close. |
| 154 | + */ |
| 155 | + public static CountDownLatch awaitNextClientCallCloses(final int count) { |
| 156 | + final CountDownLatch newCounter = new CountDownLatch(count); |
| 157 | + clientCounter = newCounter; |
| 158 | + return newCounter; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Returns a {@link CountDownLatch} that will be used in the next server and client calls and can be used to await |
| 163 | + * the respective closes. This method must be called before the call is started. |
| 164 | + * |
| 165 | + * @param count The number of call closes to await. |
| 166 | + * @return The counter used to await the client call close. |
| 167 | + * @see #awaitNextClientCallCloses(int) |
| 168 | + * @see #awaitNextServerCallCloses(int) |
| 169 | + */ |
| 170 | + public static CountDownLatch awaitNextServerAndClientCallCloses(final int count) { |
| 171 | + final CountDownLatch newCounter = new CountDownLatch(2 * count); |
| 172 | + serverCounter = newCounter; |
| 173 | + clientCounter = newCounter; |
| 174 | + return newCounter; |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments