|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-2024 The gRPC-Spring 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 net.devh.boot.grpc.test.setup; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | + |
| 23 | +import java.util.concurrent.ExecutionException; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.springframework.boot.test.context.SpringBootTest; |
| 28 | +import org.springframework.test.annotation.DirtiesContext; |
| 29 | +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; |
| 30 | + |
| 31 | +import io.grpc.internal.testing.StreamRecorder; |
| 32 | +import io.grpc.stub.StreamObserver; |
| 33 | +import lombok.SneakyThrows; |
| 34 | +import lombok.extern.slf4j.Slf4j; |
| 35 | +import net.devh.boot.grpc.client.config.GrpcChannelProperties; |
| 36 | +import net.devh.boot.grpc.test.config.BaseAutoConfiguration; |
| 37 | +import net.devh.boot.grpc.test.config.ServiceConfiguration; |
| 38 | +import net.devh.boot.grpc.test.proto.SomeType; |
| 39 | + |
| 40 | +/** |
| 41 | + * These tests check the property {@link GrpcChannelProperties#getDefaultRequestTimeout()}}. |
| 42 | + */ |
| 43 | +public class DefaultRequestTimeoutSetupTests { |
| 44 | + |
| 45 | + @Slf4j |
| 46 | + @SpringBootTest(properties = { |
| 47 | + "grpc.client.GLOBAL.address=localhost:9090", |
| 48 | + "grpc.client.GLOBAL.defaultRequestTimeout=1s", |
| 49 | + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", |
| 50 | + }) |
| 51 | + @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) |
| 52 | + static class DefaultRequestTimeoutSetupTest extends AbstractSimpleServerClientTest { |
| 53 | + |
| 54 | + @Test |
| 55 | + @SneakyThrows |
| 56 | + @DirtiesContext |
| 57 | + void testServiceStubTimeoutEnabledAndSuccessful() { |
| 58 | + log.info("--- Starting test with unsuccessful and than successful call ---"); |
| 59 | + final StreamRecorder<SomeType> streamRecorder1 = StreamRecorder.create(); |
| 60 | + this.testServiceStub.echo(streamRecorder1); |
| 61 | + assertThrows(ExecutionException.class, () -> streamRecorder1.firstValue().get()); |
| 62 | + |
| 63 | + final StreamRecorder<SomeType> streamRecorder2 = StreamRecorder.create(); |
| 64 | + StreamObserver<SomeType> echo2 = testServiceStub.echo(streamRecorder2); |
| 65 | + echo2.onNext(SomeType.getDefaultInstance()); |
| 66 | + assertNull(streamRecorder2.getError()); |
| 67 | + assertNotNull(streamRecorder2.firstValue().get().getVersion()); |
| 68 | + log.info("--- Test completed --- "); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + @SneakyThrows |
| 73 | + @DirtiesContext |
| 74 | + void testServiceStubManuallyConfiguredDeadlineTakesPrecedenceOfTheConfigOne() { |
| 75 | + log.info( |
| 76 | + "--- Starting test that manually configured deadline takes precedence of the config default request timeout ---"); |
| 77 | + final StreamRecorder<SomeType> streamRecorder = StreamRecorder.create(); |
| 78 | + StreamObserver<SomeType> echo = |
| 79 | + this.testServiceStub.withDeadlineAfter(5L, TimeUnit.SECONDS).echo(streamRecorder); |
| 80 | + TimeUnit.SECONDS.sleep(2); |
| 81 | + echo.onNext(SomeType.getDefaultInstance()); |
| 82 | + assertNull(streamRecorder.getError()); |
| 83 | + assertNotNull(streamRecorder.firstValue().get().getVersion()); |
| 84 | + log.info("--- Test completed --- "); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + @Slf4j |
| 89 | + @SpringBootTest(properties = { |
| 90 | + "grpc.client.GLOBAL.address=localhost:9090", |
| 91 | + "grpc.client.GLOBAL.defaultRequestTimeout=0s", |
| 92 | + "grpc.client.GLOBAL.negotiationType=PLAINTEXT", |
| 93 | + }) |
| 94 | + @SpringJUnitConfig(classes = {ServiceConfiguration.class, BaseAutoConfiguration.class}) |
| 95 | + static class ZeroDefaultRequestTimeoutSetupTest extends AbstractSimpleServerClientTest { |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments