|
| 1 | +/* |
| 2 | + * Copyright 2024 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.testing.integration; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import io.grpc.ManagedChannel; |
| 22 | +import io.grpc.ServerInterceptors; |
| 23 | +import io.grpc.inprocess.InProcessChannelBuilder; |
| 24 | +import io.grpc.inprocess.InProcessServerBuilder; |
| 25 | +import io.grpc.internal.FakeClock; |
| 26 | +import io.grpc.internal.testing.StreamRecorder; |
| 27 | +import io.grpc.stub.StreamObserver; |
| 28 | +import io.grpc.testing.GrpcCleanupRule; |
| 29 | +import io.grpc.testing.integration.EmptyProtos.Empty; |
| 30 | +import io.grpc.testing.integration.Messages.ResponseParameters; |
| 31 | +import io.grpc.testing.integration.Messages.StreamingOutputCallRequest; |
| 32 | +import io.grpc.testing.integration.Messages.StreamingOutputCallResponse; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | +import org.junit.runners.JUnit4; |
| 38 | + |
| 39 | +/** Tests for ManagedChannelImpl that use a real transport. */ |
| 40 | +@RunWith(JUnit4.class) |
| 41 | +public final class ManagedChannelImplIntegrationTest { |
| 42 | + private static final String SERVER_NAME = ManagedChannelImplIntegrationTest.class.getName(); |
| 43 | + @Rule |
| 44 | + public final GrpcCleanupRule grpcCleanup = new GrpcCleanupRule(); |
| 45 | + |
| 46 | + @Test |
| 47 | + public void idleWhileRpcInTransport_exitsIdleForNewRpc() throws Exception { |
| 48 | + FakeClock fakeClock = new FakeClock(); |
| 49 | + grpcCleanup.register(InProcessServerBuilder.forName(SERVER_NAME) |
| 50 | + .directExecutor() |
| 51 | + .addService( |
| 52 | + ServerInterceptors.intercept( |
| 53 | + new TestServiceImpl(fakeClock.getScheduledExecutorService()), |
| 54 | + TestServiceImpl.interceptors())) |
| 55 | + .build() |
| 56 | + .start()); |
| 57 | + ManagedChannel channel = grpcCleanup.register(InProcessChannelBuilder.forName(SERVER_NAME) |
| 58 | + .directExecutor() |
| 59 | + .build()); |
| 60 | + |
| 61 | + TestServiceGrpc.TestServiceBlockingStub blockingStub = TestServiceGrpc.newBlockingStub(channel); |
| 62 | + TestServiceGrpc.TestServiceStub asyncStub = TestServiceGrpc.newStub(channel); |
| 63 | + StreamRecorder<StreamingOutputCallResponse> responseObserver = StreamRecorder.create(); |
| 64 | + StreamObserver<StreamingOutputCallRequest> requestObserver = |
| 65 | + asyncStub.fullDuplexCall(responseObserver); |
| 66 | + requestObserver.onNext(StreamingOutputCallRequest.newBuilder() |
| 67 | + .addResponseParameters(ResponseParameters.newBuilder() |
| 68 | + .setIntervalUs(Integer.MAX_VALUE)) |
| 69 | + .build()); |
| 70 | + try { |
| 71 | + channel.enterIdle(); |
| 72 | + assertThat(blockingStub |
| 73 | + .withDeadlineAfter(10, TimeUnit.SECONDS) |
| 74 | + .emptyCall(Empty.getDefaultInstance())) |
| 75 | + .isEqualTo(Empty.getDefaultInstance()); |
| 76 | + } finally { |
| 77 | + requestObserver.onError(new RuntimeException("cleanup")); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments