|
| 1 | +/* |
| 2 | + * Copyright © 2025 Apple Inc. and the ServiceTalk project 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 | +package io.servicetalk.grpc.netty; |
| 17 | + |
| 18 | +import io.servicetalk.concurrent.CompletableSource; |
| 19 | +import io.servicetalk.concurrent.api.Executor; |
| 20 | +import io.servicetalk.concurrent.api.ExecutorExtension; |
| 21 | +import io.servicetalk.concurrent.api.Processors; |
| 22 | +import io.servicetalk.concurrent.api.Publisher; |
| 23 | +import io.servicetalk.concurrent.api.Single; |
| 24 | +import io.servicetalk.concurrent.api.SourceAdapters; |
| 25 | +import io.servicetalk.concurrent.internal.RejectedSubscribeException; |
| 26 | +import io.servicetalk.grpc.api.DefaultGrpcClientMetadata; |
| 27 | +import io.servicetalk.grpc.api.GrpcClientMetadata; |
| 28 | +import io.servicetalk.grpc.api.GrpcServiceContext; |
| 29 | +import io.servicetalk.grpc.api.GrpcStatusException; |
| 30 | +import io.servicetalk.grpc.netty.TesterProto.TestRequest; |
| 31 | +import io.servicetalk.grpc.netty.TesterProto.TestResponse; |
| 32 | +import io.servicetalk.grpc.netty.TesterProto.Tester.ClientFactory; |
| 33 | +import io.servicetalk.grpc.netty.TesterProto.Tester.TesterClient; |
| 34 | +import io.servicetalk.grpc.netty.TesterProto.Tester.TesterService; |
| 35 | +import io.servicetalk.transport.api.ServerContext; |
| 36 | + |
| 37 | +import org.junit.jupiter.api.AfterEach; |
| 38 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 39 | +import org.junit.jupiter.params.ParameterizedTest; |
| 40 | +import org.junit.jupiter.params.provider.Arguments; |
| 41 | +import org.junit.jupiter.params.provider.MethodSource; |
| 42 | + |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.List; |
| 46 | +import java.util.concurrent.CountDownLatch; |
| 47 | +import java.util.concurrent.ExecutionException; |
| 48 | +import java.util.concurrent.Future; |
| 49 | +import java.util.concurrent.atomic.AtomicInteger; |
| 50 | +import javax.annotation.Nullable; |
| 51 | + |
| 52 | +import static io.servicetalk.concurrent.api.ExecutorExtension.withCachedExecutor; |
| 53 | +import static io.servicetalk.transport.netty.internal.AddressUtils.localAddress; |
| 54 | +import static io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort; |
| 55 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 56 | +import static org.hamcrest.Matchers.instanceOf; |
| 57 | +import static org.hamcrest.Matchers.is; |
| 58 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 59 | + |
| 60 | +class ConcurrentGrpcRequestTest { |
| 61 | + |
| 62 | + private enum AsyncVariant { |
| 63 | + TEST, |
| 64 | + TEST_REQUEST_STREAM, |
| 65 | + TEST_RESPONSE_STREAM, |
| 66 | + TEST_BI_DI_STREAM, |
| 67 | + BLOCKING_TEST, |
| 68 | + BLOCKING_TEST_REQUEST_STREAM, |
| 69 | + BLOCKING_TEST_RESPONSE_STREAM, |
| 70 | + BLOCKING_TEST_BI_DI_STREAM |
| 71 | + } |
| 72 | + |
| 73 | + @RegisterExtension |
| 74 | + static final ExecutorExtension<Executor> executorExtension = withCachedExecutor().setClassLevel(true); |
| 75 | + |
| 76 | + private final CountDownLatch receivedFirstRequest = new CountDownLatch(1); |
| 77 | + private final AtomicInteger receivedRequests = new AtomicInteger(); |
| 78 | + private final CompletableSource.Processor responseProcessor = Processors.newCompletableProcessor(); |
| 79 | + private final ServerContext serverCtx; |
| 80 | + |
| 81 | + ConcurrentGrpcRequestTest() throws Exception { |
| 82 | + serverCtx = GrpcServers.forAddress(localAddress(0)).listenAndAwait(new TesterService() { |
| 83 | + @Override |
| 84 | + public Single<TestResponse> test(GrpcServiceContext ctx, TestRequest request) { |
| 85 | + return testSingle(Publisher.from(request)); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Single<TestResponse> testRequestStream(GrpcServiceContext ctx, Publisher<TestRequest> request) { |
| 90 | + return testSingle(request); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Publisher<TestResponse> testResponseStream(GrpcServiceContext ctx, TestRequest request) { |
| 95 | + return testSingle(Publisher.from(request)).toPublisher(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public Publisher<TestResponse> testBiDiStream(GrpcServiceContext ctx, Publisher<TestRequest> request) { |
| 100 | + return testSingle(request).toPublisher(); |
| 101 | + } |
| 102 | + |
| 103 | + private Single<TestResponse> testSingle(Publisher<TestRequest> request) { |
| 104 | + receivedFirstRequest.countDown(); |
| 105 | + if (receivedRequests.incrementAndGet() == 1) { |
| 106 | + return SourceAdapters.fromSource(responseProcessor) |
| 107 | + .concat(request.ignoreElements()) |
| 108 | + .concat(Single.succeeded(TestResponse.newBuilder().setMessage("first").build())); |
| 109 | + } |
| 110 | + return Single.succeeded(TestResponse.newBuilder().setMessage("other").build()); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + @AfterEach |
| 116 | + void tearDown() throws Exception { |
| 117 | + serverCtx.close(); |
| 118 | + } |
| 119 | + |
| 120 | + private static List<Arguments> asyncVariants() { |
| 121 | + List<Arguments> arguments = new ArrayList<>(); |
| 122 | + for (AsyncVariant variant : AsyncVariant.values()) { |
| 123 | + arguments.add(Arguments.of(true, variant)); |
| 124 | + // Blocking calls without metadata always create a new underlying request, there is no risk |
| 125 | + if (!variant.name().startsWith("BLOCKING")) { |
| 126 | + arguments.add(Arguments.of(false, variant)); |
| 127 | + } |
| 128 | + } |
| 129 | + return arguments; |
| 130 | + } |
| 131 | + |
| 132 | + @ParameterizedTest(name = "{displayName} [{index}] withMetadata={0} variant={1}") |
| 133 | + @MethodSource("asyncVariants") |
| 134 | + void test(boolean withMetadata, AsyncVariant variant) throws Exception { |
| 135 | + GrpcClientMetadata metadata = withMetadata ? new DefaultGrpcClientMetadata() : null; |
| 136 | + try (TesterClient client = GrpcClients.forAddress(serverHostAndPort(serverCtx)).build(new ClientFactory())) { |
| 137 | + Single<TestResponse> firstSingle = newSingle(variant, client, metadata); |
| 138 | + Future<TestResponse> first = firstSingle.toFuture(); |
| 139 | + receivedFirstRequest.await(); |
| 140 | + Future<TestResponse> firstConcurrent = firstSingle.toFuture(); |
| 141 | + Future<TestResponse> secondConcurrent = newSingle(variant, client, metadata).toFuture(); |
| 142 | + |
| 143 | + responseProcessor.onComplete(); |
| 144 | + assertThat(first.get().getMessage(), is("first")); |
| 145 | + assertRejected(firstConcurrent); |
| 146 | + if (metadata != null) { |
| 147 | + assertRejected(secondConcurrent); |
| 148 | + } else { |
| 149 | + // Requests are independent when metadata is not shared between them |
| 150 | + assertThat(secondConcurrent.get().getMessage(), is("other")); |
| 151 | + } |
| 152 | + |
| 153 | + Future<TestResponse> firstSequential = firstSingle.toFuture(); |
| 154 | + assertThat(firstSequential.get().getMessage(), is("other")); |
| 155 | + Future<TestResponse> thirdSequential = newSingle(variant, client, metadata).toFuture(); |
| 156 | + assertThat(thirdSequential.get().getMessage(), is("other")); |
| 157 | + } |
| 158 | + assertThat(receivedRequests.get(), is(metadata != null ? 3 : 4)); |
| 159 | + } |
| 160 | + |
| 161 | + private static Single<TestResponse> newSingle(AsyncVariant variant, TesterClient client, |
| 162 | + @Nullable GrpcClientMetadata metadata) { |
| 163 | + switch (variant) { |
| 164 | + case TEST: |
| 165 | + return metadata == null ? |
| 166 | + client.test(newRequest()) : |
| 167 | + client.test(metadata, newRequest()); |
| 168 | + case TEST_REQUEST_STREAM: |
| 169 | + return metadata == null ? |
| 170 | + client.testRequestStream(newStreamingRequest()) : |
| 171 | + client.testRequestStream(metadata, newStreamingRequest()); |
| 172 | + case TEST_RESPONSE_STREAM: |
| 173 | + return (metadata == null ? |
| 174 | + client.testResponseStream(newRequest()) : |
| 175 | + client.testResponseStream(metadata, newRequest())) |
| 176 | + .firstOrError(); |
| 177 | + case TEST_BI_DI_STREAM: |
| 178 | + return (metadata == null ? |
| 179 | + client.testBiDiStream(newStreamingRequest()) : |
| 180 | + client.testBiDiStream(metadata, newStreamingRequest())) |
| 181 | + .firstOrError(); |
| 182 | + case BLOCKING_TEST: |
| 183 | + return executorExtension.executor().submit(() -> metadata == null ? |
| 184 | + client.asBlockingClient().test(newRequest()) : |
| 185 | + client.asBlockingClient().test(metadata, newRequest())); |
| 186 | + case BLOCKING_TEST_REQUEST_STREAM: |
| 187 | + return executorExtension.executor().submit(() -> metadata == null ? |
| 188 | + client.asBlockingClient().testRequestStream(newIterableRequest()) : |
| 189 | + client.asBlockingClient().testRequestStream(metadata, newIterableRequest())); |
| 190 | + case BLOCKING_TEST_RESPONSE_STREAM: |
| 191 | + return executorExtension.executor().submit(() -> (metadata == null ? |
| 192 | + client.asBlockingClient().testResponseStream(newRequest()) : |
| 193 | + client.asBlockingClient().testResponseStream(metadata, newRequest())) |
| 194 | + .iterator().next()); |
| 195 | + case BLOCKING_TEST_BI_DI_STREAM: |
| 196 | + return executorExtension.executor().submit(() -> (metadata == null ? |
| 197 | + client.asBlockingClient().testBiDiStream(newIterableRequest()) : |
| 198 | + client.asBlockingClient().testBiDiStream(metadata, newIterableRequest())) |
| 199 | + .iterator().next()); |
| 200 | + default: |
| 201 | + throw new AssertionError("Unexpected variant: " + variant); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + private static TestRequest newRequest() { |
| 206 | + return TestRequest.newBuilder().setName("foo").build(); |
| 207 | + } |
| 208 | + |
| 209 | + private static Publisher<TestRequest> newStreamingRequest() { |
| 210 | + return Publisher.from(newRequest()); |
| 211 | + } |
| 212 | + |
| 213 | + private static Iterable<TestRequest> newIterableRequest() { |
| 214 | + return Collections.singletonList(newRequest()); |
| 215 | + } |
| 216 | + |
| 217 | + private static void assertRejected(Future<?> future) { |
| 218 | + ExecutionException ee = assertThrows(ExecutionException.class, future::get); |
| 219 | + assertThat(ee.getCause(), is(instanceOf(GrpcStatusException.class))); |
| 220 | + GrpcStatusException gse = (GrpcStatusException) ee.getCause(); |
| 221 | + assertThat(gse.getCause(), is(instanceOf(RejectedSubscribeException.class))); |
| 222 | + } |
| 223 | +} |
0 commit comments